Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from deepface import DeepFace
|
| 3 |
-
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
import base64
|
|
|
|
| 8 |
import pymongo
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# =======================
|
| 12 |
# MongoDB setup
|
| 13 |
# =======================
|
| 14 |
-
import certifi
|
| 15 |
-
|
| 16 |
client = pymongo.MongoClient(
|
| 17 |
"mongodb+srv://username:password@cluster0.n8pboqq.mongodb.net/?retryWrites=true&w=majority",
|
| 18 |
tlsCAFile=certifi.where()
|
|
@@ -20,9 +19,8 @@ client = pymongo.MongoClient(
|
|
| 20 |
db = client["faceDB"]
|
| 21 |
face_collection = db["faceImg"]
|
| 22 |
|
| 23 |
-
|
| 24 |
# =======================
|
| 25 |
-
# Upload ảnh +
|
| 26 |
# =======================
|
| 27 |
def upload_image(image, name):
|
| 28 |
try:
|
|
@@ -36,7 +34,6 @@ def upload_image(image, name):
|
|
| 36 |
img_bytes = buffer.getvalue()
|
| 37 |
img_base64 = base64.b64encode(img_bytes).decode('utf-8')
|
| 38 |
|
| 39 |
-
# Lưu vào MongoDB
|
| 40 |
doc = {
|
| 41 |
"_id": image_id,
|
| 42 |
"name": name,
|
|
@@ -57,15 +54,20 @@ def recognize_face(image, image_id):
|
|
| 57 |
if not doc:
|
| 58 |
return "❌ Ảnh ID không tồn tại"
|
| 59 |
|
| 60 |
-
# Decode base64
|
| 61 |
img_bytes = base64.b64decode(doc['image'])
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
|
|
|
| 65 |
if isinstance(image, Image.Image):
|
| 66 |
image = np.array(image)
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
result = DeepFace.verify(img1_path=
|
| 69 |
output = {
|
| 70 |
"Verified": result["verified"],
|
| 71 |
"Khoảng cách": round(result["distance"], 4),
|
|
@@ -82,8 +84,12 @@ def analyze_face(image):
|
|
| 82 |
try:
|
| 83 |
if isinstance(image, Image.Image):
|
| 84 |
image = np.array(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
result = DeepFace.analyze(
|
| 86 |
-
img_path=
|
| 87 |
actions=["age", "gender", "emotion", "race"],
|
| 88 |
enforce_detection=False
|
| 89 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from PIL import Image
|
| 2 |
+
from io import BytesIO
|
| 3 |
+
import numpy as np
|
| 4 |
import base64
|
| 5 |
+
import uuid
|
| 6 |
import pymongo
|
| 7 |
+
import certifi
|
| 8 |
+
from deepface import DeepFace
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import json
|
| 11 |
|
| 12 |
# =======================
|
| 13 |
# MongoDB setup
|
| 14 |
# =======================
|
|
|
|
|
|
|
| 15 |
client = pymongo.MongoClient(
|
| 16 |
"mongodb+srv://username:password@cluster0.n8pboqq.mongodb.net/?retryWrites=true&w=majority",
|
| 17 |
tlsCAFile=certifi.where()
|
|
|
|
| 19 |
db = client["faceDB"]
|
| 20 |
face_collection = db["faceImg"]
|
| 21 |
|
|
|
|
| 22 |
# =======================
|
| 23 |
+
# Upload ảnh + MongoDB
|
| 24 |
# =======================
|
| 25 |
def upload_image(image, name):
|
| 26 |
try:
|
|
|
|
| 34 |
img_bytes = buffer.getvalue()
|
| 35 |
img_base64 = base64.b64encode(img_bytes).decode('utf-8')
|
| 36 |
|
|
|
|
| 37 |
doc = {
|
| 38 |
"_id": image_id,
|
| 39 |
"name": name,
|
|
|
|
| 54 |
if not doc:
|
| 55 |
return "❌ Ảnh ID không tồn tại"
|
| 56 |
|
| 57 |
+
# Decode base64 → lưu tạm
|
| 58 |
img_bytes = base64.b64decode(doc['image'])
|
| 59 |
+
temp_path = f"temp_{image_id}.png"
|
| 60 |
+
with open(temp_path, "wb") as f:
|
| 61 |
+
f.write(img_bytes)
|
| 62 |
|
| 63 |
+
# Lưu input ảnh tạm
|
| 64 |
if isinstance(image, Image.Image):
|
| 65 |
image = np.array(image)
|
| 66 |
+
pil_input = Image.fromarray(image)
|
| 67 |
+
input_path = f"input_{image_id}.png"
|
| 68 |
+
pil_input.save(input_path)
|
| 69 |
|
| 70 |
+
result = DeepFace.verify(img1_path=input_path, img2_path=temp_path)
|
| 71 |
output = {
|
| 72 |
"Verified": result["verified"],
|
| 73 |
"Khoảng cách": round(result["distance"], 4),
|
|
|
|
| 84 |
try:
|
| 85 |
if isinstance(image, Image.Image):
|
| 86 |
image = np.array(image)
|
| 87 |
+
pil_image = Image.fromarray(image)
|
| 88 |
+
temp_path = f"analyze_{uuid.uuid4().hex}.png"
|
| 89 |
+
pil_image.save(temp_path)
|
| 90 |
+
|
| 91 |
result = DeepFace.analyze(
|
| 92 |
+
img_path=temp_path,
|
| 93 |
actions=["age", "gender", "emotion", "race"],
|
| 94 |
enforce_detection=False
|
| 95 |
)
|