-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-processing.py
executable file
·205 lines (178 loc) · 5.71 KB
/
post-processing.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python3
################################################ LIBRARIES ################################################
import cv2 as cv
import numpy as np
import os
from os import walk
import sys
################################################ VARIABLES ################################################
# Folder where folders and pictures will be created
baseFolder = "/home/pi/Desktop/KISSS_Capture/"
# Mirroring (1:enable 0:disable)
mirroring = 1
# Rotating (1:enable 0:disable)
rotating = 1
degrees = 90
# Nextcloud (1:enable 0:disable)
clouding = 1
remote_path = "Photos/DIAPOS/"
# Counter
j = 0
################################################ FUNCTIONS ################################################
def detect_left_x_mid(treeshold, myimg):
left = 0
h,w,c = myimg.shape
mid_h = int(h/2)
# Detecting first photo pixel from the middle to avoid the circle light
for i in range(0, w):
color = myimg[(mid_h,i)]
avg = np.mean(myimg[(mid_h,i)])
if avg >= treeshold :
if left == 0 :
left = i
break
if i <= left :
left = i
break
for i in range(left, 0, -1):
a = False
for j in range(0, h):
color = myimg[(j,i)]
avg = np.mean(myimg[(j,i)])
if avg >= treeshold :
if i <= left :
left = i
a = True
break
if a == False:
break
return left
def detect_right_x_mid(treeshold, myimg):
right = 0
h,w,c = myimg.shape
mid_h = int(h/2)
for i in range(w-1, -1, -1):
color = myimg[(mid_h,i)]
avg = np.mean(myimg[(mid_h,i)])
if avg >= treeshold :
if right == 0 :
right = i
break
if i >= right :
right = i
break
for i in range(right, w):
a = False
for j in range(0, h):
color = myimg[(j,i)]
avg = np.mean(myimg[(j,i)])
if avg >= treeshold :
if i >= right :
right = i
a = True
break
if a == False:
break
return right
def detect_top_y_mid(treeshold, myimg, left, right):
top = 0
h,w,c = myimg.shape
mid_w = int(w/2)
for i in range(0, h):
color = myimg[(i,mid_w)]
avg = np.mean(myimg[(i,mid_w)])
if avg >= treeshold :
top = i
break
for i in range(top, -1, -1):
a = False
for j in range(left, right+1):
color = myimg[(i,j)]
avg = np.mean(myimg[(i,j)])
if avg >= treeshold :
if j <= top :
top = j
a = True
break
if a == False:
break
return top
def detect_bottom_y_mid(treeshold, myimg, left, right):
bottom = 0
h,w,c = myimg.shape
mid_w = int(w/2)
for i in range(h-1, -1, -1):
color = myimg[(i,mid_w)]
avg = np.mean(myimg[(i,mid_w)])
if avg >= treeshold :
bottom = i
break
for i in range(bottom, h):
a = False
for j in range(left, right+1):
color = myimg[(i,j)]
avg = np.mean(myimg[(i,j)])
if avg >= treeshold :
if i >= bottom :
bottom = i
a = True
break
if a == False:
break
return bottom
def cropper(img_source, dst_folder, img, lx, rx, ty, by):
crop_img=img[ty:by, lx:rx]
if mirroring == 1:
print("Flipping...")
crop_img=cv.flip(crop_img, 1) # mirror
if rotating == 1:
print("Rotating...")
crop_img=cv.rotate(crop_img, cv.ROTATE_180)
# Write final image
cv.imwrite(dst_folder+'/'+img_source,crop_img)
# Push to nexcloud
if clouding == 1:
print("Clouding...")
if j == 1: # first image let's create folder
os.system("./cloudmanager.sh mkdir "+remote_path+"/"+boxnbr)
os.system("./cloudmanager.sh send "+dst_folder+"/"+img_source+" "+remote_path+"/"+boxnbr+"/")
def getImages():
f = []
for (dirpath, dirnames, filenames) in walk(sourceFolder):
for file in sorted(filenames) :
f.append(sourceFolder+"/"+file)
break
return f
################################################ MAIN ################################################
if __name__ == '__main__': # Program start from here
# Argument
if len(sys.argv) != 2:
print("Missing box number parameter")
quit()
boxnbr = str(sys.argv[1])
sourceFolder = baseFolder+'/'+boxnbr+'/'
destFolder = baseFolder+'/'+boxnbr+'/processed/'
# Check if folders already exists
if not os.path.exists(sourceFolder):
print(sourceFolder+" doesn't exists !")
quit()
else:
if not os.path.exists(destFolder):
os.makedirs(destFolder)
else:
print(destFolder+" already exists !")
quit()
# cropping
treeshold = 55
images = getImages()
j=1
for i in images:
print(str(j)+"/"+str(len(images))+" picture : "+i)
print("Cropping...")
myimg = cv.imread(i)
left_x = detect_left_x_mid(treeshold, myimg)
right_x = detect_right_x_mid(treeshold, myimg)
top_y = detect_top_y_mid(treeshold, myimg, left_x, right_x)
bottom_y = detect_bottom_y_mid(treeshold, myimg, left_x, right_x)
cropper(i.split("/")[-1], destFolder, myimg, left_x, right_x,top_y, bottom_y)
j+=1