-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.py
148 lines (114 loc) · 4.29 KB
/
final.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
import cv2
import numpy as np
from pynput.mouse import Button, Controller
import wx
mouse = Controller()
# To get the screen size
app = wx.App(False)
(sx, sy) = wx.GetDisplaySize()
# To set the HSV values of ranges
lower_blue = np.array([110, 100, 100])
upper_blue = np.array([130, 255, 255])
lower_red = np.array([170, 120, 70])
upper_red = np.array([180, 255, 255])
# To resize the image according to the screen size
imw = 1000
imh = int(imw * (sy / sx))
mousePress = False
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
img = cv2.resize(img, (imw, imh))
# Conversion to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Detection using HSV segmentation
red_mask = cv2.inRange(hsv, lower_red, upper_red)
cv2.imshow("red_init", red_mask)
blue_mask = cv2.inRange(hsv, lower_blue, upper_blue)
cv2.imshow("blue_init", blue_mask)
# MIX MASK
mix = cv2.bitwise_or(red_mask, blue_mask)
cv2.imshow("mix_init", mix)
mix = cv2.GaussianBlur(mix, (7, 7), 0)
_, mix = cv2.threshold(mix, 50, 255, cv2.THRESH_BINARY)
# Blurs
red_blur = cv2.medianBlur(red_mask, 11)
blue_blur = cv2.medianBlur(blue_mask, 11)
mix_blur = cv2.medianBlur(mix, 11)
cv2.imshow("red_blur", red_blur)
cv2.imshow("blue_blur", blue_blur)
cv2.imshow("mix_blur", mix_blur)
# Detect Contours
mix_cont, heir = cv2.findContours(
mix_blur, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
red_cont, heir = cv2.findContours(
red_blur, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
blue_cont, heir = cv2.findContours(
blue_blur, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if len(mix_cont) == 2 and len(red_cont) == 1 and len(blue_cont) == 1:
red_hull = cv2.convexHull(red_cont[0], False)
img = cv2.drawContours(img, [red_hull], -1, (0, 0, 255), 1)
blue_hull = cv2.convexHull(blue_cont[0], False)
img = cv2.drawContours(img, [blue_hull], -1, (255, 0, 0), 1)
M_red = cv2.moments(red_hull)
flag1 = False
if mousePress:
mouse.release(Button.left)
mousePress = False
# Detect the centroid
if M_red["m00"] != 0:
flag1 = True
c1X = int(M_red["m10"] / M_red["m00"])
c1Y = int(M_red["m01"] / M_red["m00"])
img = cv2.circle(img, (c1X, c1Y), 3, (0, 0, 255), -1)
M_blue = cv2.moments(blue_hull)
flag2 = False
# Detect the centroid
if M_blue["m00"] != 0:
flag2 = True
c2X = int(M_blue["m10"] / M_blue["m00"])
c2Y = int(M_blue["m01"] / M_blue["m00"])
img = cv2.circle(img, (c2X, c2Y), 3, (255, 0, 0), -1)
if flag1 and flag2:
cX = int((c1X+c2X)/2)
cY = int((c1Y+c2Y)/2)
mouseLoc = (sx - (sx * (cX/imw)), sy*(cY/imh))
mouse.position = mouseLoc
elif len(mix_cont) == 1 and len(red_cont) == 1 and len(blue_cont) == 1:
red_hull = cv2.convexHull(red_cont[0], False)
img = cv2.drawContours(img, [red_hull], -1, (0, 0, 255), 1)
blue_hull = cv2.convexHull(blue_cont[0], False)
img = cv2.drawContours(img, [blue_hull], -1, (255, 0, 0), 1)
if not(mousePress):
mouse.press(Button.left)
mousePress = True
M_red = cv2.moments(red_hull)
flag1 = False
# Detect the centroid
if M_red["m00"] != 0:
flag1 = True
c1X = int(M_red["m10"] / M_red["m00"])
c1Y = int(M_red["m01"] / M_red["m00"])
img = cv2.circle(img, (c1X, c1Y), 3, (0, 0, 255), -1)
M_blue = cv2.moments(blue_hull)
flag2 = False
# Detect the centroid
if M_blue["m00"] != 0:
flag2 = True
c2X = int(M_blue["m10"] / M_blue["m00"])
c2Y = int(M_blue["m01"] / M_blue["m00"])
img = cv2.circle(img, (c2X, c2Y), 3, (255, 0, 0), -1)
if flag1 and flag2:
cX = int((c1X+c2X)/2)
cY = int((c1Y+c2Y)/2)
mouseLoc = (sx - (sx * (cX/imw)), sy*(cY/imh))
mouse.position = mouseLoc
cv2.imshow("img", img)
# cv2.imshow("mix", mix_blur)
# cv2.imshow("red", red_blur)
# cv2.imshow("blue", blue_blur)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()