-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelmet_detection.py
More file actions
173 lines (154 loc) · 5.56 KB
/
helmet_detection.py
File metadata and controls
173 lines (154 loc) · 5.56 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 19 12:54:30 2019
@author: kota421
"""
import cv2
from color_filter import redColorFilter_simple as color_filter
from noise_cancel import noise_cancel
from region_of_interest import region_of_interest, mask4ROI
from move2object import showMeHelmet, moveDrone
from matplotlib import pyplot as plt
def isColoredPixel(x,y,image):
try:
return image[x][y] > 0
except IndexError:
return False
def test(img):
"""
ASSUMES:
-img: numpy.array, cv2 image in BGR,
RETURNS:
-img_ROI: numpy.array, cv2 image in BGR, image that shows ONLY around
the biggest red region in img
-centerX: int, x coordinate of the center of the ROI in img_ROI
-centerY: int, y coordinate of the center of the ROI in img_ROI
"""
#color filtering
_,img_red = color_filter(img)
# noise canceling
img_noiseless = noise_cancel(img_red)
# resize
img_noiseless_small = cv2.resize(img_noiseless,(200,100))
img_small = cv2.resize(img,(200,100))
# region of interest
mask,centerX,centerY,(h,w) = mask4ROI(img_noiseless_small,isColoredPixel)
img_ROI = region_of_interest(img_small,mask)
return img_ROI,centerX,centerY,(h,w)
def test_verbose(img):
"""
Returns picutres in the process.
ASSUMES:
-img: numpy.array, cv2 image in BGR,
RETURNS:
-img_ROI: numpy.array, cv2 image in BGR, image that shows ONLY around
the biggest red region in img
-centerX: int, x coordinate of the center of the ROI in img_ROI
-centerY: int, y coordinate of the center of the ROI in img_ROI
"""
#color filtering
_,img_red = color_filter(img)
# noise canceling
img_noiseless = noise_cancel(img_red)
# resize
img_noiseless_small = cv2.resize(img_noiseless,(200,100))
img_small = cv2.resize(img,(200,100))
# region of interest
mask,centerX,centerY,(h,w) = mask4ROI(img_noiseless_small,isColoredPixel)
img_ROI = region_of_interest(img_small,mask)
return img_red, img_noiseless,img_ROI,centerX,centerY
###---------RUN TEST with a single image---------###
## Test image
# =============================================================================
# img = cv2.imread("test_images/test_2.png",1)
# img_small = cv2.resize(img,(200,100))
# img_ROI,centerX,centerY,(h,w) = test(img)
# IMG_SHAPE = img_small.shape[0],img_small.shape[1]
# plt.imshow(img_ROI)
# showMeHelmet(IMG_SHAPE,centerX,centerY,h,w)
# moveDrone(IMG_SHAPE,centerX,centerY,h,w)
# =============================================================================
# =============================================================================
# # position
# if centerX:
# print('helmetX =',centerX, 'helmetY=',centerY)
# print('The helmet is off from the center of the image by')
# print('x =',(centerX-(0.5*img_small.shape[1]))*100/img_small.shape[1],'%,',
# 'y =',(centerY-(0.5*img_small.shape[0]))*100/img_small.shape[0],'%')
# else:
# print('No helmet found in the image.')
# ## Show
# cv2.imshow('original',img_small)
# cv2.waitKey(0)
# cv2.imshow('ROI',img_ROI)
# cv2.waitKey(0)
# =============================================================================
###---------RUN TEST with many images---------###
def test_pics(n):
for i in range(1,12):
try:
name = "test_images/test_"+str(i)+".png"
img = cv2.imread(name,1)
img_small = cv2.resize(img,(200,100))
except:
name = "test_images/test_"+str(i)+".jpg"
img = cv2.imread(name,1)
img_small = cv2.resize(img,(200,100))
## Test image
img_noise,img_noiseless,img_ROI, centerX,centerY = test_verbose(img_small)
# position
if centerX:
print('helmetX =',centerX, 'helmetY=',centerY)
print('The helmet is off from the center of the image by')
print('x =',(centerX-(0.5*img_small.shape[1]))*100/img_small.shape[1],'%,',
'y =',(centerY-(0.5*img_small.shape[0]))*100/img_small.shape[0],'%')
else:
print('No helmet found in the image.')
## Show
cv2.imshow('original',img_small)
cv2.waitKey(0)
cv2.imshow('noise',img_noise)
cv2.waitKey(0)
cv2.imshow('noiseless',img_noiseless)
cv2.waitKey(0)
cv2.imshow('ROI',img_ROI)
cv2.waitKey(0)
## Uncomment line below to test many images
test_pics(11)
###---------RUN TEST with a video---------###
def test_video():
cap = cv2.VideoCapture(0)
while True:
# read a frame
_, frame = cap.read()
#===== OPERATION ========#
result,_,_,_ = test(frame)
cv2.imshow('result',result)
# Press ESC key to escape
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
## Uncomment line below to test a video
#test_video()
def test_video2():
cap = cv2.VideoCapture(0)
while True:
try:
# read a frame
_, frame = cap.read()
#===== OPERATION ========#
img_ROI,centerX,centerY,(h,w) = test(frame)
img_small = cv2.resize(frame,(200,100))
IMG_SHAPE = img_small.shape[0],img_small.shape[1]
plt.imshow(img_ROI)
showMeHelmet(IMG_SHAPE,centerX,centerY,h,w)
moveDrone(IMG_SHAPE,centerX,centerY,h,w)
# Press ESC key to escape
except KeyboardInterrupt:
print('interrupted')
cap.release()
cv2.destroyAllWindows()
break
#test_video2()