Spaces:
Runtime error
Runtime error
Added beautiful output image
Browse files
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 =
|
| 48 |
-
img = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR)
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
def create_image(images):
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
total_height = sum([img.shape[0] for img in images])
|
| 58 |
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
def check(avatars_zip, photos_zip):
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
| 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 |
|