Repository navigation
Expand file tree
/
Copy pathShape_Tracker_Webcam.py
More file actions
58 lines (45 loc) · 1.77 KB
/
Copy pathShape_Tracker_Webcam.py
File metadata and controls
58 lines (45 loc) · 1.77 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
import cv2
import imutils
import numpy as np
cap = cv2.VideoCapture(1)
# Check if the webcam is opened correctly
if not cap.isOpened():
raise IOError("Cannot open webcam")
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, None, fx=2.25, fy=1.75, interpolation=cv2.INTER_AREA)
# convert the frame to grayscale, blur it, and threshold it
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
# extract contours from the webcam frame
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
# loop over the contours and draw them on the webcam frame
for c in cnts:
cv2.drawContours(frame, [c], -1, (0, 0, 255), 2)
# display the total number of shapes on the image
text = "I found {} total shapes".format(len(cnts))
cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# define the list of boundaries
boundaries = [
([17, 15, 100], [50, 56, 200]),
#([86, 31, 4], [220, 88, 50]),
#([25, 146, 190], [62, 174, 250]),
#([103, 86, 65], [145, 133, 128])
]
# loop over the boundaries
for (lower, upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")
# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(frame, lower, upper)
output = cv2.bitwise_and(frame, frame, mask = mask)
cv2.imshow("Window", frame)
a = cv2.waitKey(1)
if a == 27:
break
cap.release()
cv2.destroyAllWindows()