Spaces:
Runtime error
Runtime error
generativeai
commited on
Commit
•
57fa999
1
Parent(s):
e38bafd
Update app.py
Browse files
app.py
CHANGED
@@ -9,65 +9,70 @@ import numpy as np
|
|
9 |
load_dotenv()
|
10 |
|
11 |
def find_face_encodings(image):
|
12 |
-
|
13 |
-
|
14 |
|
15 |
def face_similarity(img1, img2, compare_image_folder=""):
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
def convert_pil_to_open_cv(pil_image):
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
def check_image_similarity(main_image_folder, compare_image_folder):
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
|
51 |
|
52 |
-
def compare_photoshoot_crops(photo_shoot_id, main_image_folder):
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
62 |
-
|
|
|
|
|
63 |
|
64 |
iface = gr.Interface(
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
71 |
)
|
72 |
|
73 |
iface.launch()
|
|
|
9 |
load_dotenv()
|
10 |
|
11 |
def find_face_encodings(image):
|
12 |
+
face_enc = face_recognition.face_encodings(image)
|
13 |
+
return face_enc[0]
|
14 |
|
15 |
def face_similarity(img1, img2, compare_image_folder=""):
|
16 |
+
image_1 = find_face_encodings(img1)
|
17 |
+
image_2 = find_face_encodings(img2)
|
18 |
|
19 |
+
# checking both images are same
|
20 |
+
is_same = face_recognition.compare_faces([image_1], image_2)[0]
|
21 |
+
accuracy = 0
|
22 |
+
if is_same:
|
23 |
+
# finding the distance level between images
|
24 |
+
distance = face_recognition.face_distance([image_1], image_2)
|
25 |
+
distance = round(distance[0] * 100)
|
26 |
|
27 |
+
# calcuating accuracy level between images
|
28 |
+
accuracy = 100 - round(distance)
|
29 |
+
|
30 |
+
return {"is_same": "true" if is_same else "false", "accuracy": accuracy, "error": "false", "compare_image": compare_image_folder}
|
31 |
|
32 |
def convert_pil_to_open_cv(pil_image):
|
33 |
+
open_cv_image = np.array(pil_image)
|
34 |
+
open_cv_image = open_cv_image[:, :, ::-1].copy()
|
35 |
+
return open_cv_image
|
36 |
|
37 |
def check_image_similarity(main_image_folder, compare_image_folder):
|
38 |
+
main_image = AwsService.get_image_from_s3(os.environ.get('AWS_S3_BUCKET'), main_image_folder)
|
39 |
+
compare_image = AwsService.get_image_from_s3(os.environ.get('AWS_S3_BUCKET'), compare_image_folder)
|
40 |
|
41 |
+
result = False
|
42 |
|
43 |
+
try:
|
44 |
+
result = face_similarity(convert_pil_to_open_cv(main_image["pil"]), convert_pil_to_open_cv(compare_image["pil"]), compare_image_folder)
|
45 |
+
except Exception as e:
|
46 |
+
print("Error on execution: ", e)
|
47 |
+
finally:
|
48 |
+
if not result:
|
49 |
+
result = {"is_same": "false", "accuracy": 0, "error": "true", "compare_image": compare_image_folder}
|
50 |
|
51 |
+
return result
|
52 |
|
53 |
+
def compare_photoshoot_crops(photo_shoot_id, main_image_folder, compare_image_folder):
|
54 |
+
if compare_image_key == "":
|
55 |
+
folder = "PhotoShoots/" + str(photo_shoot_id) + "/Croppeds"
|
56 |
+
files = AwsService.get_files_from_s3(os.environ.get('AWS_S3_BUCKET'), folder)
|
57 |
+
results = []
|
58 |
|
59 |
+
for file in files:
|
60 |
+
if main_image_folder != file['Key']:
|
61 |
+
result = check_image_similarity(main_image_folder, file['Key'])
|
62 |
+
results.append(result)
|
63 |
|
64 |
+
return results
|
65 |
+
else:
|
66 |
+
return check_image_similarity(main_image_folder, compare_image_folder)
|
67 |
|
68 |
iface = gr.Interface(
|
69 |
+
fn=compare_photoshoot_crops,
|
70 |
+
inputs=[
|
71 |
+
gr.Textbox(lines=1, placeholder="Photo Shoot ID"),
|
72 |
+
gr.Textbox(lines=1, placeholder="Main Image Key"),
|
73 |
+
gr.Textbox(lines=1, placeholder="Compare Image Key")
|
74 |
+
],
|
75 |
+
outputs="text"
|
76 |
)
|
77 |
|
78 |
iface.launch()
|