huy00001 commited on
Commit
923dc9a
·
verified ·
1 Parent(s): 8674a39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -14
app.py CHANGED
@@ -1,25 +1,20 @@
1
  import gradio as gr
2
  from deepface import DeepFace
3
- import os
4
- import uuid
5
  import numpy as np
6
  from PIL import Image
7
  import json
 
8
 
9
- # Thư mục lưu ảnh upload
10
- UPLOAD_FOLDER = "uploads"
11
- if not os.path.exists(UPLOAD_FOLDER):
12
- os.makedirs(UPLOAD_FOLDER)
13
 
14
  # 1️⃣ API: Upload ảnh
15
  def upload_image(image):
16
  try:
17
- # Chuyển PIL sang numpy nếu cần
18
  if isinstance(image, Image.Image):
19
  image = np.array(image)
20
  image_id = str(uuid.uuid4())
21
- save_path = os.path.join(UPLOAD_FOLDER, f"{image_id}.png")
22
- Image.fromarray(image).save(save_path)
23
  return f"Ảnh lưu thành công với ID: {image_id}", image_id
24
  except Exception as e:
25
  return f"Lỗi khi upload: {str(e)}", None
@@ -29,10 +24,10 @@ def recognize_face(image, image_id):
29
  try:
30
  if isinstance(image, Image.Image):
31
  image = np.array(image)
32
- target_path = os.path.join(UPLOAD_FOLDER, f"{image_id}.png")
33
- if not os.path.exists(target_path):
34
  return "Ảnh ID không tồn tại"
35
- result = DeepFace.verify(img1_path=image, img2_path=target_path)
 
36
  output = {
37
  "Verified": result["verified"],
38
  "Khoảng cách": round(result["distance"], 4)
@@ -62,14 +57,14 @@ def analyze_face(image):
62
  except Exception as e:
63
  return f"Lỗi khi phân tích: {str(e)}"
64
 
65
- # Tạo giao diện Gradio
66
  with gr.Blocks() as demo:
67
  gr.Markdown("## 📁 Upload ảnh")
68
  with gr.Row():
69
  upload_input = gr.Image(label="Upload ảnh", type="numpy")
70
  upload_btn = gr.Button("Upload")
71
  upload_output = gr.Textbox(label="Kết quả Upload")
72
- image_id_store = gr.State()
73
 
74
  upload_btn.click(upload_image, inputs=upload_input, outputs=[upload_output, image_id_store])
75
 
 
1
  import gradio as gr
2
  from deepface import DeepFace
 
 
3
  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
 
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)
 
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