forked from shihchengyen/auto_nav
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjectDetect.py
186 lines (159 loc) · 5.72 KB
/
objectDetect.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
'''
Object detection and tracking with OpenCV
==> Turning a LED on detection and
==> Real Time tracking with Pan-Tilt servos
Based on original tracking object code developed by Adrian Rosebrock
Visit original post: https://www.pyimagesearch.com/2016/05/09/opencv-rpi-gpio-and-gpio-zero-on-the-raspberry-pi/
Developed by Marcelo Rovai - MJRoBot.org @ 9Feb2018
'''
# import the necessary packages
from __future__ import print_function
from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2
import os
import RPi.GPIO as GPIO
#define Servos GPIOs
panServo = 27
tiltServo = 37
triggerSolenoid = 40
# initialize LED GPIO
redLed = 21
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(redLed, GPIO.OUT)
GPIO.setup(triggerSolenoid, GPIO.OUT)
#p=GPIO.start(32)
#position servos
def positionServo (servo, angle):
os.system("python angleServoCtrl.py " + str(servo) + " " + str(angle))
print("[INFO] Positioning servo at GPIO {0} to {1} degrees\n".format(servo, angle))
# position servos to present object at center of the frame
def mapServoPosition (x, y):
global panAngle
global tiltAngle
if (x < 220):
panAngle += 10
if panAngle > 140:
panAngle = 140
positionServo (panServo, panAngle)
if (x > 280):
panAngle -= 10
if panAngle < 40:
panAngle = 40
positionServo (panServo, panAngle)
if (y < 270)
tiltAngle += 5
if tiltAngle > 130:
tiltAngle = 130
positionServo (tiltServo, tiltAngle)
if (y > 300):
tiltAngle -= 5
if tiltAngle < 40:
tiltAngle = 40
positionServo (tiltServo, tiltAngle)
# initialize the video stream and allow the camera sensor to warmup
print("[INFO] waiting for camera to warmup...")
vs = VideoStream(0).start()
time.sleep(2.0)
# define the lower and upper boundaries of the object
# to be tracked in the HSV color space
colorLower = (164, 100, 100)
colorUpper = (184, 255, 255)
# Start with LED off and solenoid off
GPIO.output(redLed, GPIO.LOW)
GPIO.output(triggerSolenoid, GPIO.LOW)
ledOn = False
# Initialize angle servos at 90-90 position
global panAngle
panAngle = 90
global tiltAngle
tiltAngle = 90
# positioning Pan/Tilt servos at initial position
positionServo (panServo, panAngle)
positionServo (tiltServo, tiltAngle)
# loop over the frames from the video stream
while True:
# grab the next frame from the video stream, Invert 180o, resize the
# frame, and convert it to the HSV color space
frame = vs.read()
frame = imutils.resize(frame, width=500)
frame = imutils.rotate(frame, angle=180)
frame = cv2.flip(frame,-1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# construct a mask for the object color, then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
mask = cv2.inRange(hsv, colorLower, colorUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
global panAngle
panAngle = 90
global tiltAngle
tiltAngle = 90
# positioning Pan/Tilt servos at initial position
positionServo (panServo, panAngle)
positionServo (tiltServo, tiltAngle)
# loop over the frames from the video stream
while True:
# grab the next frame from the video stream, Invert 180o, resize the
# frame, and convert it to the HSV color space
frame = vs.read()
frame = imutils.resize(frame, width=500)
frame = imutils.rotate(frame, angle=180)
frame = cv2.flip(frame,-1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# construct a mask for the object color, then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
mask = cv2.inRange(hsv, colorLower, colorUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# find contours in the mask and initialize the current
# (x, y) center of the object
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# only proceed if the radius meets a minimum size
if radius > 10:
# draw the circle and centroid on the frame,
# then update the list of tracked points
cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
# position Servo at center of circle
mapServoPosition(int(x), int(y))
if (tiltAngle == 120):
print("\n Target Acquired FIRE FIRE FIRE \n")
GPIO.output(triggerSolenoid, GPIO.HIGH)
# if the led is not already on, turn the LED on
if not ledOn:
GPIO.output(redLed, GPIO.HIGH)
ledOn = True
# if the ball is not detected, turn the LED off
elif ledOn:
GPIO.output(redLed, GPIO.LOW)
ledOn = False
# show the frame to our screen
cv2.imshow("Frame", frame)
# if [ESC] key is pressed, stop the loop
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
# do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff \n")
#positionServo (panServo, 90)
positionServo (tiltServo, 90)
GPIO.cleanup()