import gradio as gr from deepface import DeepFace import json def analyze_image(image): try: # Phân tích ảnh result = DeepFace.analyze( img_path=image, 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"] # "Độ tin cậy": round(info["face_confidence"], 2) # bỏ nếu không muốn hiển thị } return json.dumps(output, ensure_ascii=False, indent=2) except Exception as e: return f"Lỗi: {str(e)}" # Tạo giao diện Gradio demo = gr.Interface( fn=analyze_image, inputs=gr.Image(type="numpy", label="📸 Tải ảnh khuôn mặt của bạn"), outputs=gr.Textbox(label="Kết quả phân tích"), title="Phân tích khuôn mặt với DeepFace", description="Upload ảnh để nhận diện tuổi, giới tính, cảm xúc và chủng tộc." ) if __name__ == "__main__": demo.launch()