generativeai commited on
Commit
57fa999
1 Parent(s): e38bafd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -43
app.py CHANGED
@@ -9,65 +9,70 @@ import numpy as np
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
- return {"is_same": "true" if is_same else "false", "accuracy": accuracy, "error": "false", "compare_image": compare_image_folder}
 
30
 
31
  def convert_pil_to_open_cv(pil_image):
32
- open_cv_image = np.array(pil_image)
33
- open_cv_image = open_cv_image[:, :, ::-1].copy()
34
- return open_cv_image
35
 
36
  def check_image_similarity(main_image_folder, compare_image_folder):
37
- main_image = AwsService.get_image_from_s3(os.environ.get('AWS_S3_BUCKET'), main_image_folder)
38
- compare_image = AwsService.get_image_from_s3(os.environ.get('AWS_S3_BUCKET'), compare_image_folder)
39
 
40
- result = False
41
 
42
- try:
43
- result = face_similarity(convert_pil_to_open_cv(main_image["pil"]), convert_pil_to_open_cv(compare_image["pil"]), compare_image_folder)
44
- except Exception as e:
45
- print("Error on execution: ", e)
46
- finally:
47
- if not result:
48
- result = {"is_same": "false", "accuracy": 0, "error": "true", "compare_image": compare_image_folder}
49
 
50
- return result
51
 
52
- def compare_photoshoot_crops(photo_shoot_id, main_image_folder):
53
- folder = "PhotoShoots/" + str(photo_shoot_id) + "/Croppeds"
54
- files = AwsService.get_files_from_s3(os.environ.get('AWS_S3_BUCKET'), folder)
55
- results = []
 
56
 
57
- for file in files:
58
- if main_image_folder != file['Key']:
59
- result = check_image_similarity(main_image_folder, file['Key'])
60
- results.append(result)
61
 
62
- return results
 
 
63
 
64
  iface = gr.Interface(
65
- fn=compare_photoshoot_crops,
66
- inputs=[
67
- gr.Textbox(lines=1, placeholder="Photo Shoot ID"),
68
- gr.Textbox(lines=1, placeholder="Main Image Key")
69
- ],
70
- outputs="text"
 
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()