-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrop_images.py
136 lines (102 loc) · 3.14 KB
/
crop_images.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
#
# Crop 40x40 parts out of records/ images for neural network training.
# Save the crops into records_crop/label/
#
import os
import csv
import cv2
IMAGE_SIZE = 40
(winX, winY) = (IMAGE_SIZE, IMAGE_SIZE)
f = open('database.csv', 'rb')
reader = csv.reader(f, delimiter=';')
counter = 0
for row in reader:
special_label = None
# activate this because of memory problem (??? cv2.imread returns None), first do one half then the other
if counter < 5000:
counter += 1
continue
if len(row) == 3:
filename, dbX, dbY = row
elif len(row) == 4:
filename, dbX, dbY, special_label = row
else:
print('row length is wrong, leave out')
print row
continue
if not filename or dbX is None or dbY is None:
print ('something is missing at ' + row)
continue
dbX = int(dbX); dbY = int(dbY)
path = 'records/' + filename
if not os.path.isfile(path):
print ('file missing: ' + filename)
continue
img = cv2.imread(path)
if img is None:
print ('"' + filename + '" is None ? ')
continue
if (img.shape != (72, 128, 3)):
print ('wrong shape for image '+filename)
continue
# hand
if dbX >= 0 and dbY >= 0:
# special face files
if filename.find('img_116.6.7_21') == 0:
# we will take a 72x72 part and resize it to 40x40
print ('special face image detected')
x = dbX / 5 - 36
y = 0
crop = img[y : y + 72, x : x + 72]
crop = cv2.resize(crop, (40, 40))
cv2.imwrite('records_crop/0/'+filename, crop)
y += 15
crop = img[y : y + 72, x : x + 72]
crop = cv2.resize(crop, (40, 40))
cv2.imwrite('records_crop/0/noFace_'+filename, crop)
continue
x = dbX / 5 - winX/2
y = dbY / 5 - winY/2
if x >= 128 - winX: x = 128 - winX
if y >= 72 - winY: y = 72 - winY
if x < 0: x = 0
if y < 0: y = 0
crop = img[y : y + winY, x : x + winX]
if special_label is None:
cv2.imwrite('records_crop/1/'+filename, crop)
# try go get rid of arm
if y < 72 - 1.7 * winY:
y2 = y + int(winY * 0.7)
crop = img[y2 : y2 + winY, x : x + winX]
# cv2.imshow('no arm', crop)
# cv2.waitKey(100)
cv2.imwrite('records_crop/0/noArm_'+filename, crop)
# augmentation
if y > winY/2:
y2 = y - int(winY/4)
crop = img[y2 : y2 + winY, x : x + winX]
cv2.imwrite('records_crop/1/handOnBottom'+filename, crop)
elif special_label == 'fist':
print ('fist image detected')
cv2.imwrite('records_crop/2/'+filename, crop)
# no hand
else:
if filename.find('img_116.6.6_19.12') == 0:
print ('face image detected')
x = 39
y = 15
crop = img[y : y + winY, x : x + winX]
cv2.imwrite('records_crop/0/'+filename, crop)
x = 56
y = 20
crop = img[y : y + winY, x : x + winX]
cv2.imwrite('records_crop/0/3_'+filename, crop)
else:
x = 44
y = 16
crop = img[y : y + winY, x : x + winX]
cv2.imwrite('records_crop/0/'+filename, crop)
x = 84
y = 30
crop = img[y : y + winY, x : x + winX]
cv2.imwrite('records_crop/0/2_'+filename, crop)