-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
133 lines (109 loc) · 4.09 KB
/
main.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
import cv2
import numpy as np
import os
def get_image_name():
dirs = os.listdir()
imgDirs = []
for dir in dirs:
if dir.find("virtual_paint_image") == 0:
imgDirs.append(dir)
num = []
for dir in imgDirs:
try:
num.append(int(dir[19:-4]))
except ValueError:
pass
try:
number = max(num)
except ValueError:
number = 0
return "virtual_paint_image" + str(number+1) + ".jpg"
cap = cv2.VideoCapture(0)
blue_points = []
yellow_points = []
orange_points = []
lst = []
blue_lower = np.array([100, 80, 0])
blue_upper = np.array([140,255, 255])
yellow_lower = np.array([10, 70, 120])
yellow_upper = np.array([30, 255, 255])
orange_lower = np.array([0, 90, 20])
orange_upper = np.array([10, 255, 255])
while True:
succeed, img = cap.read()
imgResult = img.copy()
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask_blue = cv2.inRange(imgHSV, blue_lower, blue_upper)
blue_contours, heirarchy = cv2.findContours(mask_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
mask_yellow = cv2.inRange(imgHSV, yellow_lower, yellow_upper)
yellow_contours, heirarchy = cv2.findContours(mask_yellow, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mask_orange = cv2.inRange(imgHSV, orange_lower, orange_upper)
orange_contours, heirarchy = cv2.findContours(mask_orange, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for c in blue_contours:
area = cv2.contourArea(c)
if area >= 500:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(img, (x, y), (x+w, y+h), (255,0,0), 3)
cv2.circle(imgResult, (x+w//2, y), 10, (255,0,0), cv2.FILLED)
blue_points.append((x+w//2, y))
lst.append((x+w//2, y))
print(area)
for c in yellow_contours:
area = cv2.contourArea(c)
if area >= 500:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,255), 3)
cv2.circle(imgResult, (x+w//2, y), 10, (0,255,255), cv2.FILLED)
yellow_points.append((x+w//2, y))
lst.append((x+w//2, y))
print(area)
for c in orange_contours:
area = cv2.contourArea(c)
if area >= 500:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(img, (x, y), (x+w, y+h), (0,0,255), 3)
cv2.circle(imgResult, (x+w//2, y), 10, (0,0,255), cv2.FILLED)
orange_points.append((x+w//2, y))
lst.append((x+w//2, y))
print(area)
for point in blue_points:
cv2.circle(imgResult, point, 10, (255,0,0), cv2.FILLED)
for point in yellow_points:
cv2.circle(imgResult, point, 10, (0,255,255), cv2.FILLED)
for point in orange_points:
cv2.circle(imgResult, point, 10, (0,0,255), cv2.FILLED)
# cv2.imshow("Frame", img)
# cv2.imshow("Mask Yellow", mask_yellow)
# cv2.imshow("Maks Orange", mask_orange)
# cv2.imshow("Maks Blue", mask_blue)
imgResult = cv2.flip(imgResult, 1)
cv2.imshow("Virtual Paint", imgResult)
k = cv2.waitKey(10)
if k == ord('c'):
blue_points = []
yellow_points = []
orange_points = []
if k == ord('z'):
pt = lst[-1]
if pt in blue_points:
blue_points.remove(pt)
if pt in yellow_points:
yellow_points.remove(pt)
if pt in orange_points:
orange_points.remove(pt)
lst.remove(pt)
if k == ord('s'):
name = get_image_name()
cv2.imwrite(name, imgResult)
if k == ord('w'):
img = np.zeros_like(img)
for point in blue_points:
cv2.circle(img, point, 10, (255,0,0), cv2.FILLED)
for point in yellow_points:
cv2.circle(img, point, 10, (0,255,255), cv2.FILLED)
for point in orange_points:
cv2.circle(img, point, 10, (0,255,0), cv2.FILLED)
name = get_image_name()
cv2.imwrite(name, img)
if k == ord('q'):
break