szili2011 commited on
Commit
747ca92
·
verified ·
1 Parent(s): 725d475

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from deepface import DeepFace
3
+ import cv2
4
+ import mediapipe as mp
5
+ import matplotlib.pyplot as plt
6
+ from PIL import Image
7
+ import numpy as np
8
+
9
+ # Initialize MediaPipe Pose for body bounding box detection
10
+ mp_pose = mp.solutions.pose
11
+
12
+ def analyze_images(img1, img2):
13
+ # Face comparison with DeepFace
14
+ face_result = DeepFace.verify(img1, img2, model_name='VGG-Face')
15
+ is_same_person = face_result['verified']
16
+ similarity_score = face_result['distance']
17
+
18
+ # Convert images to OpenCV format
19
+ img1_cv = cv2.cvtColor(np.array(img1), cv2.COLOR_RGB2BGR)
20
+ img2_cv = cv2.cvtColor(np.array(img2), cv2.COLOR_RGB2BGR)
21
+
22
+ # Body analysis with MediaPipe
23
+ def get_body_info(image):
24
+ with mp_pose.Pose(static_image_mode=True) as pose:
25
+ results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
26
+ if results.pose_landmarks:
27
+ landmarks = results.pose_landmarks.landmark
28
+ # Calculate bounding box height and width
29
+ x_values = [landmark.x for landmark in landmarks]
30
+ y_values = [landmark.y for landmark in landmarks]
31
+ width = max(x_values) - min(x_values)
32
+ height = max(y_values) - min(y_values)
33
+ aspect_ratio = height / width if width > 0 else 0
34
+ return aspect_ratio, width, height
35
+ return None, None, None
36
+
37
+ aspect_ratio1, width1, height1 = get_body_info(img1_cv)
38
+ aspect_ratio2, width2, height2 = get_body_info(img2_cv)
39
+
40
+ # Create a chart to display face and body similarities
41
+ labels = ['Face Similarity', 'Body Aspect Ratio', 'Body Width', 'Body Height']
42
+ values1 = [similarity_score, aspect_ratio1, width1, height1]
43
+ values2 = [similarity_score, aspect_ratio2, width2, height2]
44
+
45
+ x = np.arange(len(labels))
46
+ width = 0.35
47
+
48
+ fig, ax = plt.subplots()
49
+ ax.bar(x - width/2, values1, width, label='Image 1')
50
+ ax.bar(x + width/2, values2, width, label='Image 2')
51
+
52
+ ax.set_ylabel('Scores')
53
+ ax.set_title('Comparison of Face and Body Features')
54
+ ax.set_xticks(x)
55
+ ax.set_xticklabels(labels)
56
+ ax.legend()
57
+
58
+ # Save and return the chart
59
+ plt.tight_layout()
60
+ plt_path = "comparison_chart.png"
61
+ plt.savefig(plt_path)
62
+ plt.close(fig)
63
+
64
+ return is_same_person, plt_path
65
+
66
+ # Set up Gradio interface
67
+ iface = gr.Interface(
68
+ fn=analyze_images,
69
+ inputs=[gr.inputs.Image(type="pil"), gr.inputs.Image(type="pil")],
70
+ outputs=[gr.outputs.Textbox(label="Same Person?"), gr.outputs.Image(label="Comparison Chart")],
71
+ title="Face and Body Similarity Analyzer",
72
+ description="Upload two images to analyze if they contain the same person and compare body features."
73
+ )
74
+
75
+ iface.launch()