|
import numpy as np |
|
import torchvision |
|
import PIL |
|
import cv2 |
|
|
|
|
|
def crop(image): |
|
img_cv = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) |
|
height, width, _ = image.shape |
|
|
|
|
|
pixels_to_remove = 5 |
|
|
|
|
|
image = image[pixels_to_remove:height - pixels_to_remove, pixels_to_remove:width - pixels_to_remove] |
|
|
|
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
threshold_value = 75 |
|
ret, binary_image = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY) |
|
image = cv2.cvtColor(binary_image, cv2.COLOR_GRAY2RGB) |
|
|
|
|
|
edges = cv2.Canny(binary_image, 50, 150) |
|
|
|
|
|
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
area_threshold = 200 |
|
|
|
|
|
for contour in contours: |
|
|
|
area = cv2.contourArea(contour) |
|
|
|
|
|
if area < area_threshold: |
|
cv2.drawContours(image, [contour], 0, (255, 255, 255), thickness=cv2.FILLED) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
|
|
black_pixels = np.column_stack(np.where(gray_image == 0)) |
|
|
|
top_pixel = black_pixels[np.argmin(black_pixels[:, 0])] |
|
|
|
bottom_pixel = black_pixels[np.argmax(black_pixels[:, 0])] |
|
|
|
left_pixel = black_pixels[np.argmin(black_pixels[:, 1])] |
|
|
|
right_pixel = black_pixels[np.argmax(black_pixels[:, 1])] |
|
|
|
|
|
width = right_pixel[1] - left_pixel[1] |
|
height = bottom_pixel[0] - top_pixel[0] |
|
|
|
rectangle_top_left = (top_pixel[0], left_pixel[1]) |
|
|
|
rectangle_image = image[rectangle_top_left[0]:rectangle_top_left[0] + height, |
|
rectangle_top_left[1]:rectangle_top_left[1] + width] |
|
|
|
|
|
original_image = rectangle_image |
|
|
|
height, width, _ = original_image.shape |
|
|
|
|
|
side_length = max(width, height) + 6 |
|
|
|
|
|
square_image = np.ones((side_length, side_length, 3), dtype=np.uint8) * 255 |
|
|
|
|
|
x_offset = (side_length - width) // 2 |
|
y_offset = (side_length - height) // 2 |
|
|
|
|
|
square_image[y_offset:y_offset + height, x_offset:x_offset + width] = original_image |
|
return square_image |
|
|
|
def web_input(image): |
|
image_tmp = crop(image) |
|
image_transforms = torchvision.transforms.Compose([torchvision.transforms.ToTensor()]) |
|
input_img = PIL.Image.fromarray(image_tmp) |
|
input_img = input_img.resize((100, 100), PIL.Image.LANCZOS) |
|
wd_new, ht_new = input_img.size |
|
wd_new = int(16 * np.ceil(wd_new / 16.0)) |
|
ht_new = int(16 * np.ceil(ht_new / 16.0)) |
|
input_img = input_img.resize((wd_new, ht_new), PIL.Image.LANCZOS) |
|
return image_transforms(input_img).unsqueeze(0), image_tmp |
|
|