-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.py
155 lines (122 loc) · 4.71 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import numpy as np
import re
import pickle
import os
from tqdm import tqdm as tqdm
from PIL import Image
def read_pk(fn):
with open(fn, 'rb') as fd:
ret = pickle.load(fd)
return ret
def write_pk(obj, fn):
with open(fn, 'wb') as fd:
pickle.dump(obj, fd)
def get_files(dr, ext='jpg|jpeg|bmp|png'):
rex = re.compile(r'^.*\.({})$'.format(ext), re.I)
return [os.path.join(dr, base) for base in os.listdir(dr) if rex.match(base)]
def rename_files(files, imagedir):
# todo: don't rename files that are already sha256?
import phashlib as ph
newfiles = []
hashes = []
for fromfile in tqdm(files, total=len(files)):
suffix = fromfile.split('.')[-1]
sha256hash = ph.sha256_checksum(fromfile)
tofile = sha256hash + '.' + suffix
tofile = os.path.join(imagedir, tofile)
newfiles.append(tofile)
hashes.append(sha256hash)
os.rename(fromfile, tofile)
return newfiles, hashes
def preprocess_image_for_cropping(img):
img = img - np.mean(img)
img /= np.std(img)
#img = img[:,:,0]/3 - img[:,:,1]/3 - img[:,:,2]/3
img = np.std(img, axis=2)
# convert image to some grayscale mush
# for i in range(1,2):
# img = img[:, :, 0]/3 - img[:, :, i]/3
#plt.imshow(img, cmap='gray')
# plt.colorbar()
# plt.show()
return img
def crop_images(df, imagedir, size):
cropped_folder = os.path.join(imagedir, 'cropped/')
if not os.path.exists(cropped_folder):
os.makedirs(os.path.dirname(cropped_folder), exist_ok=True)
if 'cropped_filename' not in df:
df['cropped_filename'] = None
for file in tqdm(df.index, total=len(df.index)):
try:
original_filename = df.loc[file]['filename']
pil_img = Image.open(original_filename)
pil_img = pil_img.convert('RGB')
# old: rename cropped files to hash
#fhash = df.loc[file]['hash']
# new: original filename
fname = df.loc[file]['name']
cropped_fname = os.path.join(imagedir, 'cropped/', fname + '.jpg')
pil_img.thumbnail((size, size), Image.ANTIALIAS)
img = np.array(pil_img)
origimg = img.copy()
croplines_x, croplines_y = get_crop_bbox(img)
w, h = pil_img.size
if len(croplines_x) is not 2:
croplines_x = [0, w]
print("couldn't crop {} in x-axis".format(file))
if len(croplines_y) is not 2:
croplines_y = [0, h]
print("couldn't crop {} in y-axis".format(file))
#plot_croplines(croplines_x, croplines_y, img)
pil_img = pil_img.crop(
(croplines_x[0], croplines_y[0], croplines_x[1], croplines_y[1]))
pil_img.save(cropped_fname)
df.loc[file]['cropped_filename'] = cropped_fname
except:
print("Couldn't crop {}, dropping file from table".format(
file))
df = df.drop(file)
continue
# plt.imshow(pil_img)
# plt.show()
return df
def get_crop_bbox(img):
img = preprocess_image_for_cropping(img)
yrange, xrange = img.shape[:2]
croplines_x = []
croplines_y = []
mean_x = [[], []]
for x in range(xrange):
# extract cross sections to analyze
filterline = np.abs(img[:, x])
# interpolate line
boxwidth = 10
box = np.ones(boxwidth)/boxwidth
filterline = np.convolve(filterline, box, mode='same')
filter_threshold = np.mean(img)/10
# find pixels where threshold is crossed
threshold_crossings = np.where(
np.array(filterline) > filter_threshold)[0]
# take mean of the found borders across image
if len(threshold_crossings) > 0:
croplines_x.append(
[threshold_crossings[0], threshold_crossings[-1]])
if len(croplines_x) > 1:
mean_x = np.median(np.array(croplines_x), axis=0).astype(int)
mean_y = [[], []]
for y in range(yrange):
# extract cross sections to analyze
filterline = np.abs(img[y, :])
# interpolate line
boxwidth = 10
box = np.ones(boxwidth)/boxwidth
filterline = np.convolve(filterline, box, mode='same')
filter_threshold = np.mean(img)/10
threshold_crossings = np.where(
np.array(filterline) > filter_threshold)[0]
if len(threshold_crossings) > 0:
croplines_y.append(
[threshold_crossings[0], threshold_crossings[-1]])
if len(croplines_y) > 1:
mean_y = np.median(np.array(croplines_y), axis=0).astype(int)
return mean_y, mean_x # threshold crossings on y axis are x values to crop and vice versa