-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMobile_Object_Tracking.py
More file actions
161 lines (124 loc) · 3.53 KB
/
Copy pathMobile_Object_Tracking.py
File metadata and controls
161 lines (124 loc) · 3.53 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
#Mobile Object Detection and Tracking
import cv2
import numpy as np
#import RPi.GPIO as GPIO
#import time
#GPIO.setmode(GPIO.BCM)
#GPIO.cleanup()
global area
def pinset():
#Motor 1 Pins
m1e1 = 4 #ENABLE
m1in1= 5
m1in2= 6
#Motor 2 Pins
m2e1 = 14 #ENABLE
m2in1= 15
m2in2= 16
def setup():
GPIO.setup(m1in1, GPIO.OUT)
GPIO.setup(m1in2, GPIO.OUT)
GPIO.setup(m1e1, GPIO.OUT)
GPIO.setup(m2e1, GPIO.OUT)
GPIO.setup(m2in1, GPIO.OUT)
GPIO.setup(m2in2, GPIO.OUT)
GPIO.output(m2e1, 1)
GPIO.output(m2e1, 1)
def stop():
GPIO.output(m1in1, 0)
GPIO.output(m1in2, 0)
GPIO.output(m2in1, 0)
GPIO.output(m2in2, 0)
def left():
GPIO.output(m1in1, 0)
GPIO.output(m1in2, 1)
GPIO.output(m2in1, 0)
GPIO.output(m2in2, 0)
def right():
GPIO.output(m1in1, 0)
GPIO.output(m1in2, 0)
GPIO.output(m2in1, 0)
GPIO.output(m2in2, 1)
def forward():
GPIO.output(m1in1, 0)
GPIO.output(m1in2, 1)
GPIO.output(m2in1, 0)
GPIO.output(m2in2, 1)
def backward():
GPIO.output(m1in1, 1)
GPIO.output(m1in2, 0)
GPIO.output(m2in1, 1)
GPIO.output(m2in2, 0)
def diff_left():
GPIO.output(m1in1, 1)
GPIO.output(m1in2, 0)
GPIO.output(m2in1, 0)
GPIO.output(m2in2, 1)
def diff_right():
GPIO.output(m1in1, 0)
GPIO.output(m1in2, 1)
GPIO.output(m2in1, 1)
GPIO.output(m2in2, 0)
def back_right():
GPIO.output(m1in1, 0)
GPIO.output(m1in2, 0)
GPIO.output(m2in1, 1)
GPIO.output(m2in2, 0)
def back_left():
GPIO.output(m1in1, 1)
GPIO.output(m1in2, 0)
GPIO.output(m2in1, 0)
GPIO.output(m2in2, 0)
#DETECTION
while True:
capture = cv2.VideoCapture(https://ipaddress, cv2.CAP_DSHOW); #Enter pi camera location
capture.set(3, 480) #Frame height and width
capture.set(4, 640)
_, frm = capture.read()
rows, cols, _ = frm.shape
#SETTING AXIS FOR HORIZONTAL TRACKING
x_medium = int(cols / 2 )
center = int(cols / 2)
#gray = cv2.cvtColor(frm, cv2.COLOR_BGR2GRAY)
#DETECTING YELLOW
hsv = cv2.cvtColor(frm, cv2.COLOR_BGR2HSV)
lowb = np.array([20, 100, 100], dtype=np.uint8)
highb = np.array([44, 255, 255], dtype=np.uint8)
mask = cv2.inRange(hsv, lowb, highb)
contours, heirachy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x:cv2.contourArea(x), reverse=True)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
R = cv2.rectangle(frm, (x, y), (x + w, y + h), (0, 255, 0), 2) #Bounding Box
x_medium = int((x + x + w) / 2) #Tracks only horizontal movement
area = h*w
break
cv2.line(frm, (x_medium, 0), (x_medium, 480), (0, 255, 0), 2)
cv2.imshow("Mask", mask)
cv2.imshow("Video", frm)
#print(area)
#print(x_medium)
#print(center)
if x_medium >=270 and x_medium <= 370 and area >=20000 and area <=25000:
print("Stop")
if x_medium >=270 and x_medium <= 370 and area <=20000:
print("Foward")
if x_medium >=270 and x_medium <= 370 and area >25000:
print("Backward")
if x_medium >370 and area >=20000 and area <=25000:
print("Differential Right")
if x_medium <270 and area >=20000 and area <=25000:
print("Differential Left")
if x_medium <270 and area <20000:
print("Left")
if x_medium >370 and area <20000:
print("Right")
if x_medium <270 and area >25000:
print("Back Left")
if x_medium >370 and area >25000:
print("Back Right")
key = cv2.waitKey(1)
if key == 27: #ESC KEY
break
capture.release()
cv2.destroyAllWindows()