Brasd99 commited on
Commit
bd5a570
·
1 Parent(s): 15c8991

Added beautiful output image

Browse files
Files changed (1) hide show
  1. app.py +65 -35
app.py CHANGED
@@ -44,49 +44,79 @@ def load_images_from_zip(zip_path):
44
  with zipfile.ZipFile(zip_path, 'r') as zip_file:
45
  for file_name in zip_file.namelist():
46
  with zip_file.open(file_name) as file:
47
- img_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8)
48
- img = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR)
49
- images.append(img)
50
- return [img for img in images if img is not None]
 
51
 
52
  def create_image(images):
53
- if not images:
54
- return np.zeros((1, 1, 3), dtype=np.uint8)
55
-
56
- max_width = max([img.shape[1] for img in images])
57
- total_height = sum([img.shape[0] for img in images])
58
 
59
- new_image = np.zeros((total_height, max_width, 3), dtype=np.uint8)
60
 
61
- y_offset = 0
62
- for img in images:
63
- new_image[y_offset:y_offset+img.shape[0], :img.shape[1], :] = img
64
- y_offset += img.shape[0]
65
 
66
- new_image = cv2.resize(new_image, (0, 0), fx=0.5, fy=0.5)
 
67
 
68
- return new_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  def check(avatars_zip, photos_zip):
71
- avatars = load_images_from_zip(avatars_zip.name)
72
- photos = load_images_from_zip(photos_zip.name)
73
- not_found_faces = []
74
- for photo in photos:
75
- photo = cv2.cvtColor(photo, cv2.COLOR_RGB2BGR)
76
- input_faces = find_faces(photo)
77
- for input_face in input_faces:
78
- avatars_checked = 0
79
- for avatar in avatars:
80
- avatar = cv2.cvtColor(avatar, cv2.COLOR_RGB2BGR)
81
- distance = find_distance(avatar, input_face)
82
- if distance > FACE_DIST_TRESH:
83
- break
84
- else:
85
- avatars_checked+=1
86
- if avatars_checked == len(avatars):
87
- not_found_faces.append(input_face)
88
-
89
- return create_image(not_found_faces)
 
 
90
 
91
  title = '<h1 style="text-align:center">FootballChecker</h1>'
92
 
 
44
  with zipfile.ZipFile(zip_path, 'r') as zip_file:
45
  for file_name in zip_file.namelist():
46
  with zip_file.open(file_name) as file:
47
+ img_bytes = file.read()
48
+ img = cv2.imdecode(np.frombuffer(img_bytes, np.uint8), cv2.IMREAD_COLOR)
49
+ if img is not None:
50
+ images.append(img)
51
+ return images
52
 
53
  def create_image(images):
54
+ table_width = 800
55
+ row_height = 100
56
+ margin = 10
57
+ text_margin = 20
 
58
 
59
+ table_height = text_margin + margin + (row_height + margin) * len(images)
60
 
61
+ font = cv2.FONT_HERSHEY_SIMPLEX
62
+ font_scale = 0.5
63
+ color = (255, 255, 255)
64
+ thickness = 2
65
 
66
+ table = np.zeros((table_height, table_width, 3), np.uint8)
67
+ id_col_width = 100
68
 
69
+ id_x = 10
70
+ img_x = id_col_width + 10
71
+ y = text_margin
72
+
73
+ cv2.putText(table, "Image ID", (id_x, y), font, font_scale, color, thickness)
74
+ cv2.putText(table, "Face", (img_x, y), font, font_scale, color, thickness)
75
+
76
+ y += margin
77
+
78
+ for i, img in enumerate(images):
79
+ height, width = img.shape[:2]
80
+ new_width = int(width * row_height / height)
81
+ if img_x + new_width > table_width:
82
+ new_width = table_width - img_x
83
+ img_resized = cv2.resize(img, (new_width, row_height))
84
+ cv2.putText(table, str(i), (id_x, y + margin), font, font_scale, color, thickness)
85
+ table[y:y+row_height, img_x:img_x+new_width] = img_resized
86
+
87
+ y += row_height + margin
88
+
89
+ for col in range(table.shape[1]-1, -1, -1):
90
+ if not np.any(table[:, col]):
91
+ continue
92
+ else:
93
+ break
94
+
95
+ table_cropped = table[:, :col+1+id_x]
96
+ return table_cropped
97
 
98
  def check(avatars_zip, photos_zip):
99
+ avatars = [cv2.cvtColor(img, cv2.COLOR_RGB2BGR) for img in load_images_from_zip(avatars_zip.name)]
100
+ photos = [cv2.cvtColor(img, cv2.COLOR_RGB2BGR) for img in load_images_from_zip(photos_zip.name)]
101
+
102
+ input_avatars_faces = [find_faces(avatar) for avatar in avatars]
103
+ input_avatars_faces = [face for faces in input_avatars_faces for face in faces]
104
+
105
+ avatars_faces_count = len(input_avatars_faces)
106
+
107
+ not_found_faces = []
108
+
109
+ for photo in photos:
110
+ input_faces = find_faces(photo)
111
+ for input_face in input_faces:
112
+ for i in range(avatars_faces_count):
113
+ distance = find_distance(input_avatars_faces[i], input_face)
114
+ if distance <= FACE_DIST_TRESH:
115
+ break
116
+ elif i + 1 == avatars_faces_count:
117
+ not_found_faces.append(input_face)
118
+
119
+ return create_image(not_found_faces)
120
 
121
  title = '<h1 style="text-align:center">FootballChecker</h1>'
122