-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHBIOC_python.py
More file actions
321 lines (291 loc) · 7.9 KB
/
HBIOC_python.py
File metadata and controls
321 lines (291 loc) · 7.9 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from datetime import datetime
import cv2
import os
from PIL import Image
import picamera
import numpy as np
import io
current_time = datetime.now()
time_string = current_time.strftime('%Y%m%d_%H%M%S')
h=360
w=640
count_out=0
count_in=0
before_down=0
before_up=0
before_obj_count=0
for _ in range(100):
up=0
down=0
with picamera.PiCamera() as camera:
camera.resolution=(w,h)
image_stream = io.BytesIO()
camera.capture(image_stream, format='jpeg')
image_stream.seek(0)
image = Image.open(image_stream)
pixel_array = np.array(image)
blue=pixel_array[:,:,2]
iswhite=(blue<75).astype(int)
iswhite[0,:]=0
iswhite[-1,:]=0
iswhite[:,0]=0
iswhite[:,-1]=0
for i in range(1,h-1):
for j in range(1,w-1):
if iswhite[i,j]:
check_four=iswhite[i-1,j]+iswhite[i+1,j]+iswhite[i,j-1]+iswhite[i,j+1]
if check_four<2:
iswhite[i,j]=0
i=i-1
j=j-1
line_pixel=np.zeros((h,w),dtype=int)
for i in range(1,h-1):
for j in range(1,w-1):
if iswhite[i,j]:
check_four=iswhite[i-1,j]+iswhite[i+1,j]+iswhite[i,j-1]+iswhite[i,j+1]
if check_four>1 and check_four<4:
line_pixel[i,j]=1
check=np.zeros((h,w),dtype=int)
obj_pixel=np.zeros((h, w), dtype=np.uint8)
obj_count=0
for i in range(1,h-1):
for j in range(1,w-1):
if check[i,j]:
continue
check[i,j]=1
if line_pixel[i,j]:
bee_size=1
first_coor_h=i
first_coor_w=j
obj_count=obj_count+1
obj_pixel[first_coor_h,first_coor_w]=obj_count
if line_pixel[first_coor_h+1,first_coor_w-1]:
last_coor_h=first_coor_h+1
last_coor_w=first_coor_w-1
line_case=1
elif line_pixel[first_coor_h+1,first_coor_w]:
last_coor_h=first_coor_h+1
last_coor_w=first_coor_w
line_case=2
else:
continue
sum_coor_h=first_coor_h+last_coor_h
sum_coor_w=first_coor_w+last_coor_w
bee_size=2
check[last_coor_h,last_coor_w]=1
obj_pixel[last_coor_h,last_coor_w]=obj_count
while 1:
if line_case==1:
if line_pixel[last_coor_h-1,last_coor_w-1]:
last_coor_h-=1
last_coor_w-=1
line_case=7
elif line_pixel[last_coor_h,last_coor_w-1]:
last_coor_w-=1
line_case=8
elif line_pixel[last_coor_h+1,last_coor_w-1]:
last_coor_h+=1
last_coor_w-=1
line_case=1
elif line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
line_case=2
elif line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
last_coor_w+=1
line_case=3
else:
break
elif line_case==2:
if line_pixel[last_coor_h,last_coor_w-1]:
last_coor_w-=1
line_case=8
elif line_pixel[last_coor_h+1,last_coor_w-1]:
last_coor_h+=1
last_coor_w-=1
line_case=1
elif line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
line_case=2
elif line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
last_coor_w+=1
line_case=3
elif line_pixel[last_coor_h,last_coor_w+1]:
last_coor_w+=1
line_case=4
else:
break
elif line_case==3:
if line_pixel[last_coor_h+1,last_coor_w-1]:
last_coor_h+=1
last_coor_w-=1
line_case=1
elif line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
line_case=2
elif line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
last_coor_w+=1
line_case=3
elif line_pixel[last_coor_h,last_coor_w+1]:
last_coor_w+=1
line_case=4
elif line_pixel[last_coor_h-1,last_coor_w+1]:
last_coor_h-=1
last_coor_w+=1
line_case=5
else:
break
elif line_case==4:
if line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
line_case=2
elif line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
last_coor_w+=1
line_case=3
elif line_pixel[last_coor_h,last_coor_w+1]:
last_coor_w+=1
line_case=4
elif line_pixel[last_coor_h-1,last_coor_w+1]:
last_coor_h-=1
last_coor_w+=1
line_case=5
elif line_pixel[last_coor_h-1,last_coor_w]:
last_coor_h-=1
line_case=6
else:
break
elif line_case==5:
if line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
last_coor_w+=1
line_case=3
elif line_pixel[last_coor_h,last_coor_w+1]:
last_coor_w+=1
line_case=4
elif line_pixel[last_coor_h-1,last_coor_w+1]:
last_coor_h-=1
last_coor_w+=1
line_case=5
elif line_pixel[last_coor_h-1,last_coor_w]:
last_coor_h-=1
line_case=6
elif line_pixel[last_coor_h-1,last_coor_w-1]:
last_coor_h-=1
last_coor_w-=1
line_case=7
else:
break
elif line_case==6:
if line_pixel[last_coor_h,last_coor_w+1]:
last_coor_w+=1
line_case=4
elif line_pixel[last_coor_h-1,last_coor_w+1]:
last_coor_h-=1
last_coor_w+=1
line_case=5
elif line_pixel[last_coor_h-1,last_coor_w]:
last_coor_h-=1
line_case=6
elif line_pixel[last_coor_h-1,last_coor_w-1]:
last_coor_h-=1
last_coor_w-=1
line_case=7
elif line_pixel[last_coor_h,last_coor_w-1]:
last_coor_w-=1
line_case=8
else:
break
elif line_case==7:
if line_pixel[last_coor_h-1,last_coor_w+1]:
last_coor_h-=1
last_coor_w+=1
line_case=5
elif line_pixel[last_coor_h-1,last_coor_w]:
last_coor_h-=1
line_case=6
elif line_pixel[last_coor_h-1,last_coor_w-1]:
last_coor_h-=1
last_coor_w-=1
line_case=7
elif line_pixel[last_coor_h,last_coor_w-1]:
last_coor_w-=1
line_case=8
elif line_pixel[last_coor_h+1,last_coor_w-1]:
last_coor_h+=1
last_coor_w-=1
line_case=1
else:
break
elif line_case==8:
if line_pixel[last_coor_h-1,last_coor_w]:
last_coor_h-=1
line_case=6
elif line_pixel[last_coor_h-1,last_coor_w-1]:
last_coor_h-=1
last_coor_w-=1
line_case=7
elif line_pixel[last_coor_h,last_coor_w-1]:
last_coor_w-=1
line_case=8
elif line_pixel[last_coor_h+1,last_coor_w-1]:
last_coor_h+=1
last_coor_w-=1
line_case=1
elif line_pixel[last_coor_h+1,last_coor_w+1]:
last_coor_h+=1
line_case=2
else:
break
if check[last_coor_h,last_coor_w]:
break
check[last_coor_h,last_coor_w]=1
bee_size+=1
obj_pixel[last_coor_h,last_coor_w]=obj_count
sum_coor_h+=last_coor_h
sum_coor_w+=last_coor_h
if bee_size<50:
for check_h in range(first_coor_h,h-1):
for check_w in range(1,w-1):
if obj_pixel[check_h,check_w]==obj_count:
obj_pixel[check_h,check_w]=0
iswhite[check_h,check_w]=0
obj_count-=1
continue
outline_pixel=bee_size
for check_h in range(first_coor_h+1,h-2):
for check_w in range(2,w-2):
if iswhite[check_h,check_w]==1 and obj_pixel[check_h,check_w]==0:
if obj_pixel[check_h,check_w-1]==obj_count:
obj_pixel[check_h,check_w]=obj_count
check[check_h,check_w]=1
bee_size+=1
elif obj_pixel[check_h-1,check_w]==obj_count:
obj_pixel[check_h,check_w]=obj_count
check[check_h,check_w]=1
bee_size+=1
if outline_pixel==bee_size:
for check_h in range(first_coor_h,h-1):
for check_w in range(1,w-1):
if obj_pixel[check_h,check_w]==obj_count:
obj_pixel[check_h,check_w]=0
obj_count-=1
continue
avg_coor_w=sum_coor_w/outline_pixel
avg_coor_h=sum_coor_h/outline_pixel
if avg_coor_h<150:
down+=1
else:
up+=1
if obj_count==before_obj_count:
minus=down-before_down
if minus<0:
count_in-=minus
else:
count_out+=minus
before_down=down
before_up=up
before_obj_count=obj_count
print('in : '+str(count_in)+' / out : '+str(count_out))