-
Notifications
You must be signed in to change notification settings - Fork 0
/
Steering-Wheel.py
161 lines (129 loc) · 4.27 KB
/
Steering-Wheel.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
import numpy as np
import cv2
import math
import serial
import time
arduino = serial.Serial(port='COM4', baudrate=115200, timeout=.1)
def write_read(x):
arduino.write(bytes(x, 'utf-8'))
time.sleep(0.05)
cam = cv2.VideoCapture(0)
#Global variables
rhmin,rsmin,rvmin,ghmin,gsmin,gvmin=0,0,0,0,0,0
rhmax,rsmax,rvmax,ghmax,gsmax,gvmax=180,255,255,180,255,255
def rhmin_change(val):
global rhmin
rhmin=val
def rhmax_change(val):
global rhmax
rhmax=val
def rsmin_change(val):
global rsmin
rsmin=val
def rsmax_change(val):
global rsmax
rsmax=val
def rvmin_change(val):
global rvmin
rvmin=val
def rvmax_change(val):
global rvmax
rvmax=val
def ghmin_change(val):
global ghmin
ghmin=val
def ghmax_change(val):
global ghmax
ghmax=val
def gsmin_change(val):
global gsmin
gsmin=val
def gsmax_change(val):
global gsmax
gsmax=val
def gvmin_change(val):
global gvmin
gvmin=val
def gvmax_change(val):
global gvmax
gvmax=val
cv2.namedWindow("Set Values", cv2.WINDOW_NORMAL)
#Trackbars for Red colour
cv2.createTrackbar('rhmin','Set Values',90,180,rhmin_change)
cv2.createTrackbar('rhmax','Set Values',180,180,rhmax_change)
cv2.createTrackbar('rsmin','Set Values',100,255,rsmin_change)
cv2.createTrackbar('rsmax','Set Values',255,255,rsmax_change)
cv2.createTrackbar('rvmin','Set Values',100,255,rvmin_change)
cv2.createTrackbar('rvmax','Set Values',255,255,rvmax_change)
#Trackbars for Green Colour
cv2.createTrackbar('ghmin','Set Values',30,180,ghmin_change)
cv2.createTrackbar('ghmax','Set Values',120,180,ghmax_change)
cv2.createTrackbar('gsmin','Set Values',95,255,gsmin_change)
cv2.createTrackbar('gsmax','Set Values',255,255,gsmax_change)
cv2.createTrackbar('gvmin','Set Values',92,255,gvmin_change)
cv2.createTrackbar('gvmax','Set Values',255,255,gvmax_change)
z="X"
z=input("Enter S to start and X to brake:")
while(1):
ret, frame = cam.read()
frame = cv2.flip(frame, 1)
hsvFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Set range for red color and
# define mask
#90, 100, 100
red_lower = np.array([rhmin, rsmin, rvmin], np.uint8)
red_upper = np.array([rhmax, rsmax, rvmax], np.uint8)
red_mask = cv2.inRange(hsvFrame, red_lower, red_upper)
# Set range for green color and
# define mask
# 30-120, 95, 92
green_lower = np.array([ghmin, gsmin, gvmin], np.uint8)
green_upper = np.array([ghmax, gsmax, gvmax], np.uint8)
green_mask = cv2.inRange(hsvFrame, green_lower, green_upper)
# Morphological Transform, Dilation
# for each color and bitwise_and operator
# between imageFrame and mask determines
# to detect only that particular color
kernel = np.ones((5, 5), "uint8")
# For red color
red_mask = cv2.dilate(red_mask, kernel)
res_red = cv2.bitwise_and(frame, frame, mask = red_mask)
# For green color
green_mask = cv2.dilate(green_mask, kernel)
res_green = cv2.bitwise_and(frame, frame, mask = green_mask)
# Creating contour to track red color
contours, hierarchy = cv2.findContours(red_mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
rx,ry,gx,gy=0,0,0,0
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area > 300):
(rx,ry), r = cv2.minEnclosingCircle(contours[0])
cv2.circle(frame, (int(rx),int(ry)), 1, (0,0,0), 3)
# Creating contour to track green color
contours, hierarchy = cv2.findContours(green_mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area > 300):
(gx,gy), r = cv2.minEnclosingCircle(contours[0])
cv2.circle(frame, (int(gx),int(gy)), 1, (0,0,0), 3)
n,m,l= frame.shape
q='P'
if(z=='X'):
write_read('X')
if(z=="S"):
if(ry-gy>=int(n/2) and q!='F'):
write_read('F')
q='F'
elif(ry-gy<int(n/2) and q!='B'):
write_read('B')
q='B'
if(rx-gx>40 and q!='R'):
write_read('R')
q='R'
if(gx-rx>40 and q!='R'):
write_read('L')
q='L'
cv2.imshow("Multiple Color Detection in Real-TIme", frame)
cv2.waitKey(100)
cap.release()
cv2.destroyAllWindows()