-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_detection_picam.py
More file actions
171 lines (143 loc) · 6.27 KB
/
object_detection_picam.py
File metadata and controls
171 lines (143 loc) · 6.27 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
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2 as cv
import numpy as np
import os
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
#Setting up frames
openingKernel = cv.getStructuringElement(cv.MORPH_RECT, (10, 10)) # Make a kernel for closing (noise removal)
openingKernelRed = cv.getStructuringElement(cv.MORPH_RECT, (2, 2))
# define range of blue color in HSV
# Range for green in HSV
# lower = 40 40 40
# upper = 80 255 255
# TODO: Tweek these values to detect all ranges of color properly
# Values calculated from: https://alloyui.com/examples/color-picker/hsv
# Limits for green range
lower_green = np.array([37, 80, 40])
upper_green = np.array([95, 255, 255])
# Limits for blue range
lower_blue = np.array([100, 80, 50])
upper_blue = np.array([125, 255, 255])
# Limits for yellow range
lower_yellow = np.array([20, 80, 40])
upper_yellow = np.array([35, 255, 255])
# Limits for red
lower_red = np.array([170, 80, 40])
upper_red = np.array([180, 255, 255])
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
os.system('clear')
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
frame = frame.array
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # Take each frame
# Threshold the HSV image to get only desired colors
maskGreen = cv.inRange(hsv, lower_green, upper_green)
maskBlue = cv.inRange(hsv, lower_blue, upper_blue)
maskYellow = cv.inRange(hsv, lower_yellow, upper_yellow)
maskRed = cv.inRange(hsv, lower_red, upper_red)
# # Clear noise from mask by opening it, then apply gaussian blur
maskGreen = cv.morphologyEx(maskGreen, cv.MORPH_OPEN, openingKernel)
maskGreen = cv.GaussianBlur(maskGreen, (5, 5), 0)
maskBlue = cv.morphologyEx(maskBlue, cv.MORPH_OPEN, openingKernel)
maskBlue = cv.GaussianBlur(maskBlue, (5, 5), 0)
maskYellow = cv.morphologyEx(maskYellow, cv.MORPH_OPEN, openingKernel)
maskYellow = cv.GaussianBlur(maskYellow, (5, 5), 0)
maskRed = cv.morphologyEx(maskRed, cv.MORPH_OPEN, openingKernelRed)
maskRed = cv.GaussianBlur(maskRed, (5, 5), 0)
# Using threshold incase the image is not binary, can be removed
_, threshGreen = cv.threshold(maskGreen, 100, 255, cv.THRESH_BINARY)
_, threshBlue = cv.threshold(maskBlue, 100, 255, cv.THRESH_BINARY)
_, threshYellow = cv.threshold(maskYellow, 100, 255, cv.THRESH_BINARY)
_, threshRed = cv.threshold(maskRed, 100, 255, cv.THRESH_BINARY)
_,contoursGreen, _ = cv.findContours(threshGreen, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
_,contoursBlue, _ = cv.findContours(threshBlue, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
_,contoursYellow, _ = cv.findContours(threshYellow, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
_,contoursRed, _ = cv.findContours(threshRed, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Bitwise-AND mask and original image
mask = cv.bitwise_or(maskBlue, maskGreen)
mask = cv.bitwise_or(mask, maskYellow)
mask=cv.bitwise_or(mask, maskRed)
res = cv.bitwise_and(frame, frame, mask=mask)
numGreen = len(contoursGreen)
numRed=len(contoursRed)
numBlue=len(contoursBlue)
numYellow=len(contoursYellow)
if(numBlue>5):
print("Bottle: True")
else:
print("Bottle:False")
if(numRed>400):
print("Box: True")
else:
print("Box:False")
if(numGreen>4):
print("Ball: True")
else:
print("Ball:False")
if(numYellow>2):
print("Disc: True")
else:
print("Disc:False")
# for c in contoursGreen:
# # To avoid false-positives, the green object should be sufficiently big
# if cv.contourArea(c) > 700:
# # Calculate center of mass to put the text
# M = cv.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
# cv.putText(frame, "green ball", (cX - 10, cY - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# cv.drawContours(res, [c], -1, (255, 0, 255), 3)
# for c in contoursBlue:
# # To avoid false-positives, the green object should be sufficiently big
# if cv.contourArea(c) > 1000:
# # Calculate center of mass to put the text
# M = cv.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
# cv.putText(frame, "bottle", (cX - 10, cY - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# cv.drawContours(res, [c], -1, (255, 0, 255), 3)
# for c in contoursYellow:
# # To avoid false-positives, the green object should be sufficiently big
# if cv.contourArea(c) > 1000:
# # Calculate center of mass to put the text
# M = cv.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
# cv.putText(frame, "yellow disk", (cX - 10, cY - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# cv.drawContours(res, [c], -1, (255, 0, 255), 3)
# for c in contoursRed:
# # To avoid false-positives, the green object should be sufficiently big
# if cv.contourArea(c) > 1000:
# # Calculate center of mass to put the text
# M = cv.moments(c)
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
# cv.circle(frame, (cX, cY), 7, (255, 255, 255), -1)
# cv.putText(frame, "red box", (cX - 10, cY - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# cv.drawContours(res, [c], -1, (255, 0, 255), 3)
# # Show the image and the result
# cv.imshow('res', res)
# cv.imshow('blue', maskBlue)
# cv.imshow('green', maskGreen)
# cv.imshow('yellow', maskYellow)
# cv.imshow('red', maskRed)
# cv.imshow('res', res)
# EXIT if 'esc' key is pressed
k = cv.waitKey(5) & 0xFF
#clear the stream for the next frame
rawCapture.truncate(0)
if k == 27:
break