|
from __future__ import division
|
|
from PIL import Image
|
|
import argparse
|
|
import torch
|
|
import torchvision
|
|
import torch.nn as nn
|
|
import numpy as np
|
|
import torchvision.utils as vutils
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
import cv2
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
def remove_the_blackborder(image_p):
|
|
image = cv2.imread(image_p,cv2.IMREAD_COLOR)
|
|
img = cv2.medianBlur(image, 5)
|
|
b = cv2.threshold(img, 3, 255, cv2.THRESH_BINARY)
|
|
binary_image = b[1]
|
|
binary_image = cv2.cvtColor(binary_image, cv2.COLOR_BGR2GRAY)
|
|
try:
|
|
edges_y, edges_x = np.where(binary_image == 255)
|
|
bottom = min(edges_y)
|
|
top = max(edges_y)
|
|
height = top - bottom
|
|
|
|
left = min(edges_x)
|
|
right = max(edges_x)
|
|
height = top - bottom
|
|
width = right - left
|
|
|
|
res_image = image[bottom:bottom + height, left:left + width]
|
|
|
|
except:
|
|
res_image=image
|
|
print(image_p)
|
|
return res_image
|
|
|
|
|
|
|
|
def load_image(image_path, transform=None, max_size=None, shape=None):
|
|
|
|
|
|
image = Image.open(image_path)
|
|
|
|
if max_size:
|
|
scale = max_size / max(image.size)
|
|
size = np.array(image.size) * scale
|
|
|
|
image = image.resize(size.astype(int), Image.ANTIALIAS)
|
|
|
|
if shape:
|
|
image = image.resize(shape, Image.LANCZOS)
|
|
|
|
if transform:
|
|
|
|
image = transform(image).unsqueeze(0)
|
|
|
|
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|