huy00001 commited on
Commit
cf2462a
·
verified ·
1 Parent(s): bbfa2cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -12
app.py CHANGED
@@ -1,10 +1,44 @@
1
  import gradio as gr
2
  from deepface import DeepFace
 
 
 
3
  import json
4
 
5
- def analyze_image(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  try:
7
- # Phân tích ảnh
8
  result = DeepFace.analyze(
9
  img_path=image,
10
  actions=["age", "gender", "emotion", "race"],
@@ -15,21 +49,38 @@ def analyze_image(image):
15
  "Tuổi ước tính": info["age"],
16
  "Giới tính": "Nam" if info["dominant_gender"] == "Man" else "Nữ",
17
  "Cảm xúc chính": info["dominant_emotion"],
18
- "Chủng tộc": info["dominant_race"],
19
- "Độ tin cậy": round(info["face_confidence"], 2)
20
  }
21
  return json.dumps(output, ensure_ascii=False, indent=2)
22
  except Exception as e:
23
- return f"Lỗi: {str(e)}"
24
 
25
  # Tạo giao diện Gradio
26
- demo = gr.Interface(
27
- fn=analyze_image,
28
- inputs=gr.Image(type="numpy", label="📸 Tải ảnh khuôn mặt của bạn"),
29
- outputs=gr.Textbox(label="Kết quả phân tích"),
30
- title="Phân tích khuôn mặt với DeepFace",
31
- description="Upload ảnh để nhận diện tuổi, giới tính, cảm xúc và chủng tộc."
32
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  if __name__ == "__main__":
35
  demo.launch()
 
1
  import gradio as gr
2
  from deepface import DeepFace
3
+ import os
4
+ import uuid
5
+ import shutil
6
  import json
7
 
8
+ # Thư mục lưu ảnh upload
9
+ UPLOAD_FOLDER = "uploads"
10
+ if not os.path.exists(UPLOAD_FOLDER):
11
+ os.makedirs(UPLOAD_FOLDER)
12
+
13
+ # 1️⃣ API: Push ảnh lên server
14
+ def upload_image(image):
15
+ try:
16
+ image_id = str(uuid.uuid4())
17
+ save_path = os.path.join(UPLOAD_FOLDER, f"{image_id}.png")
18
+ image.save(save_path) # nếu image là PIL.Image
19
+ return f"Ảnh lưu thành công với ID: {image_id}", image_id
20
+ except Exception as e:
21
+ return f"Lỗi khi upload: {str(e)}", None
22
+
23
+ # 2️⃣ API: Nhận diện khuôn mặt
24
+ def recognize_face(image, image_id):
25
+ try:
26
+ target_path = os.path.join(UPLOAD_FOLDER, f"{image_id}.png")
27
+ if not os.path.exists(target_path):
28
+ return "Ảnh ID không tồn tại"
29
+ result = DeepFace.verify(img1_path=image, img2_path=target_path)
30
+ # result là dict có 'verified': True/False, 'distance': float
31
+ output = {
32
+ "Verified": result["verified"],
33
+ "Khoảng cách": round(result["distance"], 4)
34
+ }
35
+ return json.dumps(output, ensure_ascii=False, indent=2)
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
  result = DeepFace.analyze(
43
  img_path=image,
44
  actions=["age", "gender", "emotion", "race"],
 
49
  "Tuổi ước tính": info["age"],
50
  "Giới tính": "Nam" if info["dominant_gender"] == "Man" else "Nữ",
51
  "Cảm xúc chính": info["dominant_emotion"],
52
+ "Chủng tộc": info["dominant_race"]
 
53
  }
54
  return json.dumps(output, ensure_ascii=False, indent=2)
55
  except Exception as e:
56
+ return f"Lỗi khi phân tích: {str(e)}"
57
 
58
  # Tạo giao diện Gradio
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("## 📁 Upload ảnh")
61
+ with gr.Row():
62
+ upload_input = gr.Image(label="Upload ảnh", type="pil")
63
+ upload_btn = gr.Button("Upload")
64
+ upload_output = gr.Textbox(label="Kết quả Upload")
65
+ image_id_store = gr.State() # lưu image_id
66
+
67
+ upload_btn.click(upload_image, inputs=upload_input, outputs=[upload_output, image_id_store])
68
+
69
+ gr.Markdown("## 🆔 Nhận diện khuôn mặt")
70
+ with gr.Row():
71
+ recognize_input = gr.Image(label="Ảnh cần nhận diện", type="pil")
72
+ recognize_btn = gr.Button("Nhận diện")
73
+ recognize_output = gr.Textbox(label="Kết quả nhận diện")
74
+
75
+ recognize_btn.click(recognize_face, inputs=[recognize_input, image_id_store], outputs=recognize_output)
76
+
77
+ gr.Markdown("## 🔍 Phân tích khuôn mặt")
78
+ with gr.Row():
79
+ analyze_input = gr.Image(label="Ảnh cần phân tích", type="pil")
80
+ analyze_btn = gr.Button("Phân tích")
81
+ analyze_output = gr.Textbox(label="Kết quả phân tích")
82
+
83
+ analyze_btn.click(analyze_face, inputs=analyze_input, outputs=analyze_output)
84
 
85
  if __name__ == "__main__":
86
  demo.launch()