huy00001 commited on
Commit
02f63e8
·
verified ·
1 Parent(s): 56e474c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -13
app.py CHANGED
@@ -4,30 +4,50 @@ import numpy as np
4
  from PIL import Image
5
  import json
6
  import uuid
 
 
7
 
8
- # --- Lưu ảnh tạm trong session (dictionary) ---
9
- image_storage = {}
 
 
10
 
11
- # 1️⃣ API: Upload ảnh
 
 
 
12
  def upload_image(image):
13
  try:
14
  if isinstance(image, Image.Image):
15
  image = np.array(image)
16
  image_id = str(uuid.uuid4())
17
- image_storage[image_id] = image
 
 
 
 
 
 
 
 
18
  return f"Ảnh lưu thành công với ID: {image_id}", image_id
19
  except Exception as e:
20
  return f"Lỗi khi upload: {str(e)}", None
21
 
22
- # 2️⃣ API: Nhận diện khuôn mặt
23
  def recognize_face(image, image_id):
24
  try:
 
 
25
  if isinstance(image, Image.Image):
26
  image = np.array(image)
27
- if image_id not in image_storage:
28
- return "Ảnh ID không tồn tại"
29
- stored_image = image_storage[image_id]
30
- result = DeepFace.verify(img1_path=image, img2_path=stored_image)
 
 
 
31
  output = {
32
  "Verified": result["verified"],
33
  "Khoảng cách": round(result["distance"], 4)
@@ -36,7 +56,7 @@ def recognize_face(image, image_id):
36
  except Exception as e:
37
  return f"Lỗi khi nhận diện: {str(e)}"
38
 
39
- # 3️⃣ API: Phân tích khuôn mặt
40
  def analyze_face(image):
41
  try:
42
  if isinstance(image, Image.Image):
@@ -57,14 +77,14 @@ def analyze_face(image):
57
  except Exception as e:
58
  return f"Lỗi khi phân tích: {str(e)}"
59
 
60
- # --- Tạo giao diện Gradio ---
61
  with gr.Blocks() as demo:
62
- gr.Markdown("## 📁 Upload ảnh")
63
  with gr.Row():
64
  upload_input = gr.Image(label="Upload ảnh", type="numpy")
65
  upload_btn = gr.Button("Upload")
66
  upload_output = gr.Textbox(label="Kết quả Upload")
67
- image_id_store = gr.State() # lưu image_id
68
 
69
  upload_btn.click(upload_image, inputs=upload_input, outputs=[upload_output, image_id_store])
70
 
 
4
  from PIL import Image
5
  import json
6
  import uuid
7
+ from pydrive2.auth import GoogleAuth
8
+ from pydrive2.drive import GoogleDrive
9
 
10
+ # --- Google Drive setup ---
11
+ gauth = GoogleAuth()
12
+ gauth.LocalWebserverAuth() # mở trình duyệt để login
13
+ drive = GoogleDrive(gauth)
14
 
15
+ # Dictionary lưu mapping image_id -> Google Drive file_id
16
+ image_drive_map = {}
17
+
18
+ # 1️⃣ Upload ảnh lên Google Drive
19
  def upload_image(image):
20
  try:
21
  if isinstance(image, Image.Image):
22
  image = np.array(image)
23
  image_id = str(uuid.uuid4())
24
+ filename = f"{image_id}.png"
25
+ pil_image = Image.fromarray(image)
26
+ pil_image.save(filename) # tạm lưu local
27
+
28
+ gfile = drive.CreateFile({'title': filename})
29
+ gfile.SetContentFile(filename)
30
+ gfile.Upload()
31
+
32
+ image_drive_map[image_id] = gfile['id']
33
  return f"Ảnh lưu thành công với ID: {image_id}", image_id
34
  except Exception as e:
35
  return f"Lỗi khi upload: {str(e)}", None
36
 
37
+ # 2️⃣ Nhận diện khuôn mặt
38
  def recognize_face(image, image_id):
39
  try:
40
+ if image_id not in image_drive_map:
41
+ return "Ảnh ID không tồn tại"
42
  if isinstance(image, Image.Image):
43
  image = np.array(image)
44
+
45
+ # Download ảnh từ Drive
46
+ file_id = image_drive_map[image_id]
47
+ gfile = drive.CreateFile({'id': file_id})
48
+ gfile.GetContentFile(f"temp_{image_id}.png")
49
+
50
+ result = DeepFace.verify(img1_path=image, img2_path=f"temp_{image_id}.png")
51
  output = {
52
  "Verified": result["verified"],
53
  "Khoảng cách": round(result["distance"], 4)
 
56
  except Exception as e:
57
  return f"Lỗi khi nhận diện: {str(e)}"
58
 
59
+ # 3️⃣ Phân tích khuôn mặt
60
  def analyze_face(image):
61
  try:
62
  if isinstance(image, Image.Image):
 
77
  except Exception as e:
78
  return f"Lỗi khi phân tích: {str(e)}"
79
 
80
+ # --- Gradio interface ---
81
  with gr.Blocks() as demo:
82
+ gr.Markdown("## 📁 Upload ảnh lên Google Drive")
83
  with gr.Row():
84
  upload_input = gr.Image(label="Upload ảnh", type="numpy")
85
  upload_btn = gr.Button("Upload")
86
  upload_output = gr.Textbox(label="Kết quả Upload")
87
+ image_id_store = gr.State()
88
 
89
  upload_btn.click(upload_image, inputs=upload_input, outputs=[upload_output, image_id_store])
90