def get_cropped_image(image_file_name, df_row):
"""Returns a cropped image."""
image = cv2.imread(image_file_name)
x = df_row["bbox_x_top_left"]
y = df_row["bbox_y_top_left"]
width = df_row["bbox_x_bot_right"] - x + 1
height = df_row["bbox_y_bot_right"] - y + 1
cropped_image = image[y:y+height, x:x+width]
return cropped_image
num_images = 100
num_rows, num_cols = 10, 10
input_dir = "faces/"
output_dir = "cropped_faces/"
extension = ".jpg"
bbox_cols = [
"bbox_x_top_left",
"bbox_y_top_left",
"bbox_x_bot_right",
"bbox_y_bot_right",
]
f, axarr = plt.subplots(num_rows, num_cols, figsize=(20, 20))
curr_row = 0
for i in range(num_images):
# Get image info
df_row = df.iloc[i]
image_id = df_row["id"]
# Get cropped image
image_file_name = f"{input_dir}{image_id}{extension}"
cropped_image = get_cropped_image(image_file_name, df_row)
# find the column by taking the current index modulo 3
col = i % num_rows
# plot on relevant subplot
axarr[col, curr_row].imshow(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB))
axarr[col, curr_row].axis('off')
if col == num_cols - 1:
# we have finished the current row, so increment row counter
curr_row += 1
plt.savefig(f"{output_dir}face_thumbnails.jpg")