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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -2,7 +2,8 @@ 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
@@ -10,12 +11,15 @@ 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
@@ -23,11 +27,12 @@ def upload_image(image):
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)
@@ -39,6 +44,8 @@ def recognize_face(image, image_id):
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"],
@@ -59,16 +66,16 @@ def analyze_face(image):
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
 
@@ -76,7 +83,7 @@ with gr.Blocks() as demo:
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
 
 
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
 
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
 
27
  # 2️⃣ API: Nhận diện khuôn mặt
28
  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)
 
44
  # 3️⃣ API: Phân tích khuôn mặt
45
  def analyze_face(image):
46
  try:
47
+ if isinstance(image, Image.Image):
48
+ image = np.array(image)
49
  result = DeepFace.analyze(
50
  img_path=image,
51
  actions=["age", "gender", "emotion", "race"],
 
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
 
76
  gr.Markdown("## 🆔 Nhận diện khuôn mặt")
77
  with gr.Row():
78
+ recognize_input = gr.Image(label="Ảnh cần nhận diện", type="numpy")
79
  recognize_btn = gr.Button("Nhận diện")
80
  recognize_output = gr.Textbox(label="Kết quả nhận diện")
81
 
 
83
 
84
  gr.Markdown("## 🔍 Phân tích khuôn mặt")
85
  with gr.Row():
86
+ analyze_input = gr.Image(label="Ảnh cần phân tích", type="numpy")
87
  analyze_btn = gr.Button("Phân tích")
88
  analyze_output = gr.Textbox(label="Kết quả phân tích")
89