NickKolok commited on
Commit
0f57432
1 Parent(s): c875782

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  from deepface import DeepFace
3
-
4
  import cv2
5
  import numpy as np
6
 
@@ -30,16 +29,20 @@ def calculate_similarity(image1, image2):
30
  face2 = crop_face(image2)
31
 
32
  # Convert cropped images to RGB format for DeepFace
33
- face1 = cv2.cvtColor(face1, cv2.COLOR_BGR2RGB)
34
- face2 = cv2.cvtColor(face2, cv2.COLOR_BGR2RGB)
35
 
36
  # Analyze the two cropped images for facial similarity
37
- result = DeepFace.verify(face1, face2, model_name='VGG-Face', enforce_detection=False)
38
  similarity_percentage = (1 - result['distance']) * 100 # Convert distance to similarity percentage
39
- return similarity_percentage
40
- except Exception as e:
41
- return str(e)
42
 
 
 
 
 
 
 
 
43
 
44
  # Create a Gradio interface
45
  iface = gr.Interface(
@@ -48,9 +51,13 @@ iface = gr.Interface(
48
  gr.inputs.Image(type="filepath", label="Image 1"),
49
  gr.inputs.Image(type="filepath", label="Image 2")
50
  ],
51
- outputs=gr.outputs.Textbox(label="Similarity Percentage"),
 
 
 
 
52
  title="Face Similarity Checker",
53
- description="Upload two images of faces to check their similarity."
54
  )
55
 
56
  # Launch the interface
 
1
  import gradio as gr
2
  from deepface import DeepFace
 
3
  import cv2
4
  import numpy as np
5
 
 
29
  face2 = crop_face(image2)
30
 
31
  # Convert cropped images to RGB format for DeepFace
32
+ face1_rgb = cv2.cvtColor(face1, cv2.COLOR_BGR2RGB)
33
+ face2_rgb = cv2.cvtColor(face2, cv2.COLOR_BGR2RGB)
34
 
35
  # Analyze the two cropped images for facial similarity
36
+ result = DeepFace.verify(face1_rgb, face2_rgb, model_name='VGG-Face', enforce_detection=False)
37
  similarity_percentage = (1 - result['distance']) * 100 # Convert distance to similarity percentage
 
 
 
38
 
39
+ # Encode images to display in Gradio
40
+ _, face1_encoded = cv2.imencode('.png', face1)
41
+ _, face2_encoded = cv2.imencode('.png', face2)
42
+
43
+ return similarity_percentage, face1_encoded.tobytes(), face2_encoded.tobytes()
44
+ except Exception as e:
45
+ return str(e), None, None
46
 
47
  # Create a Gradio interface
48
  iface = gr.Interface(
 
51
  gr.inputs.Image(type="filepath", label="Image 1"),
52
  gr.inputs.Image(type="filepath", label="Image 2")
53
  ],
54
+ outputs=[
55
+ gr.outputs.Textbox(label="Similarity Percentage"),
56
+ gr.outputs.Image(label="Cropped Face 1"),
57
+ gr.outputs.Image(label="Cropped Face 2"),
58
+ ],
59
  title="Face Similarity Checker",
60
+ description="Upload two images of faces to check their similarity. The faces will be detected and cropped automatically."
61
  )
62
 
63
  # Launch the interface