huy00001 commited on
Commit
152845e
·
verified ·
1 Parent(s): 1681685

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -7,16 +7,19 @@ 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)
@@ -25,12 +28,13 @@ def upload_image(image):
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
 
@@ -43,14 +47,16 @@ def recognize_face(image, image_id):
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)
 
54
  }
55
  return json.dumps(output, ensure_ascii=False, indent=2)
56
  except Exception as e:
@@ -82,11 +88,12 @@ 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
 
91
  gr.Markdown("## 🆔 Nhận diện khuôn mặt")
92
  with gr.Row():
 
7
  from pydrive2.auth import GoogleAuth
8
  from pydrive2.drive import GoogleDrive
9
 
10
+ # --- Google Drive setup với Service Account ---
11
  gauth = GoogleAuth()
12
+ gauth.ServiceAuth(settings_file="service_account.json") # file JSON của service account
13
  drive = GoogleDrive(gauth)
14
 
15
+ # ID folder public trên Google Drive
16
+ FOLDER_ID = "1MwGdJbVbLNSLnmHrR-mGlH8EAJ3gy50R"
17
+
18
+ # Dictionary lưu mapping image_id -> {'file_id':..., 'name':...}
19
  image_drive_map = {}
20
 
21
  # 1️⃣ Upload ảnh lên Google Drive
22
+ def upload_image(image, name):
23
  try:
24
  if isinstance(image, Image.Image):
25
  image = np.array(image)
 
28
  pil_image = Image.fromarray(image)
29
  pil_image.save(filename) # tạm lưu local
30
 
31
+ gfile = drive.CreateFile({'title': filename, 'parents':[{'id': FOLDER_ID}]})
32
  gfile.SetContentFile(filename)
33
  gfile.Upload()
34
 
35
+ image_drive_map[image_id] = {'file_id': gfile['id'], 'name': name}
36
+ file_link = f"https://drive.google.com/file/d/{gfile['id']}/view?usp=sharing"
37
+ return f"Ảnh lưu thành công với ID: {image_id}\nTên: {name}\nLink: {file_link}", image_id
38
  except Exception as e:
39
  return f"Lỗi khi upload: {str(e)}", None
40
 
 
47
  image = np.array(image)
48
 
49
  # Download ảnh từ Drive
50
+ file_id = image_drive_map[image_id]['file_id']
51
+ stored_name = image_drive_map[image_id]['name']
52
  gfile = drive.CreateFile({'id': file_id})
53
  gfile.GetContentFile(f"temp_{image_id}.png")
54
 
55
  result = DeepFace.verify(img1_path=image, img2_path=f"temp_{image_id}.png")
56
  output = {
57
  "Verified": result["verified"],
58
+ "Khoảng cách": round(result["distance"], 4),
59
+ "Tên ảnh gốc": stored_name
60
  }
61
  return json.dumps(output, ensure_ascii=False, indent=2)
62
  except Exception as e:
 
88
  gr.Markdown("## 📁 Upload ảnh lên Google Drive")
89
  with gr.Row():
90
  upload_input = gr.Image(label="Upload ảnh", type="numpy")
91
+ name_input = gr.Textbox(label="Tên người trong ảnh")
92
  upload_btn = gr.Button("Upload")
93
  upload_output = gr.Textbox(label="Kết quả Upload")
94
  image_id_store = gr.State()
95
 
96
+ upload_btn.click(upload_image, inputs=[upload_input, name_input], outputs=[upload_output, image_id_store])
97
 
98
  gr.Markdown("## 🆔 Nhận diện khuôn mặt")
99
  with gr.Row():