File size: 4,608 Bytes
8674a39
754959c
 
cabb75b
754959c
cabb75b
754959c
 
 
 
8bd49c3
cabb75b
 
 
 
0065721
 
cabb75b
 
 
cf2462a
cabb75b
754959c
cabb75b
152845e
cf2462a
8674a39
 
cabb75b
cf2462a
02f63e8
cabb75b
 
 
 
02f63e8
cabb75b
 
 
 
 
 
02f63e8
cabb75b
cf2462a
cabb75b
cf2462a
cabb75b
 
 
cf2462a
 
cabb75b
 
 
 
754959c
cabb75b
754959c
 
 
cabb75b
754959c
8674a39
 
754959c
 
 
02f63e8
754959c
cf2462a
 
152845e
cabb75b
cf2462a
 
 
cabb75b
cf2462a
cabb75b
 
 
cf2462a
8bd49c3
8674a39
 
754959c
 
 
 
8bd49c3
754959c
8bd49c3
 
 
 
 
 
 
 
cf2462a
8bd49c3
 
 
cabb75b
8bd49c3
cabb75b
 
 
cf2462a
cabb75b
cf2462a
8674a39
152845e
cf2462a
 
02f63e8
cf2462a
152845e
cf2462a
 
 
8674a39
cf2462a
 
 
 
 
 
 
8674a39
cf2462a
 
 
 
8bd49c3
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from PIL import Image
from io import BytesIO
import numpy as np
import base64
import uuid
import pymongo
import certifi
from deepface import DeepFace
import gradio as gr
import json

# =======================
# MongoDB setup
# =======================
client = pymongo.MongoClient(
    "mongodb+srv://username:password@cluster0.n8pboqq.mongodb.net/?retryWrites=true&w=majority",
    tlsCAFile=certifi.where()
)
db = client["faceDB"]
face_collection = db["faceImg"]

# =======================
# Upload ảnh + MongoDB
# =======================
def upload_image(image, name):
    try:
        if isinstance(image, Image.Image):
            image = np.array(image)

        image_id = str(uuid.uuid4())
        pil_image = Image.fromarray(image)
        buffer = BytesIO()
        pil_image.save(buffer, format="PNG")
        img_bytes = buffer.getvalue()
        img_base64 = base64.b64encode(img_bytes).decode('utf-8')

        doc = {
            "_id": image_id,
            "name": name,
            "image": img_base64
        }
        face_collection.insert_one(doc)

        return f"✅ Upload thành công!\nID: {image_id}\nTên: {name}", image_id
    except Exception as e:
        return f"❌ Lỗi upload: {str(e)}", None

# =======================
# Nhận diện khuôn mặt
# =======================
def recognize_face(image, image_id):
    try:
        doc = face_collection.find_one({"_id": image_id})
        if not doc:
            return "❌ Ảnh ID không tồn tại"

        # Decode base64 → lưu tạm
        img_bytes = base64.b64decode(doc['image'])
        temp_path = f"temp_{image_id}.png"
        with open(temp_path, "wb") as f:
            f.write(img_bytes)

        # Lưu input ảnh tạm
        if isinstance(image, Image.Image):
            image = np.array(image)
        pil_input = Image.fromarray(image)
        input_path = f"input_{image_id}.png"
        pil_input.save(input_path)

        result = DeepFace.verify(img1_path=input_path, img2_path=temp_path)
        output = {
            "Verified": result["verified"],
            "Khoảng cách": round(result["distance"], 4),
            "Tên ảnh gốc": doc['name']
        }
        return json.dumps(output, ensure_ascii=False, indent=2)
    except Exception as e:
        return f"❌ Lỗi nhận diện: {str(e)}"

# =======================
# Phân tích khuôn mặt
# =======================
def analyze_face(image):
    try:
        if isinstance(image, Image.Image):
            image = np.array(image)
        pil_image = Image.fromarray(image)
        temp_path = f"analyze_{uuid.uuid4().hex}.png"
        pil_image.save(temp_path)

        result = DeepFace.analyze(
            img_path=temp_path,
            actions=["age", "gender", "emotion", "race"],
            enforce_detection=False
        )
        info = result[0]
        output = {
            "Tuổi ước tính": info["age"],
            "Giới tính": "Nam" if info["dominant_gender"] == "Man" else "Nữ",
            "Cảm xúc chính": info["dominant_emotion"],
            "Chủng tộc": info["dominant_race"]
        }
        return json.dumps(output, ensure_ascii=False, indent=2)
    except Exception as e:
        return f"❌ Lỗi phân tích: {str(e)}"

# =======================
# Gradio interface
# =======================
with gr.Blocks() as demo:
    gr.Markdown("## 📁 Upload ảnh vào MongoDB")
    with gr.Row():
        upload_input = gr.Image(label="Upload ảnh", type="numpy")
        name_input = gr.Textbox(label="Tên người trong ảnh")
        upload_btn = gr.Button("Upload")
        upload_output = gr.Textbox(label="Kết quả Upload")
        image_id_store = gr.State()

    upload_btn.click(upload_image, inputs=[upload_input, name_input], outputs=[upload_output, image_id_store])

    gr.Markdown("## 🆔 Nhận diện khuôn mặt")
    with gr.Row():
        recognize_input = gr.Image(label="Ảnh cần nhận diện", type="numpy")
        recognize_btn = gr.Button("Nhận diện")
        recognize_output = gr.Textbox(label="Kết quả nhận diện")

    recognize_btn.click(recognize_face, inputs=[recognize_input, image_id_store], outputs=recognize_output)

    gr.Markdown("## 🔍 Phân tích khuôn mặt")
    with gr.Row():
        analyze_input = gr.Image(label="Ảnh cần phân tích", type="numpy")
        analyze_btn = gr.Button("Phân tích")
        analyze_output = gr.Textbox(label="Kết quả phân tích")

    analyze_btn.click(analyze_face, inputs=analyze_input, outputs=analyze_output)

if __name__ == "__main__":
    demo.launch()