-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_tracker1.py
More file actions
113 lines (100 loc) · 3.9 KB
/
Copy pathmulti_tracker1.py
File metadata and controls
113 lines (100 loc) · 3.9 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
from imutils.video import VideoStream
import multiprocessing
import imutils
import argparse
import cv2
import time
from stranger_danger import hsv
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str,
help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="kcf",
help="OpenCV object tracker type")
args = vars(ap.parse_args())
OPENCV_OBJECT_TRACKERS = {
"csrt": cv2.TrackerCSRT_create,
"kcf": cv2.TrackerKCF_create,
"boosting": cv2.TrackerBoosting_create,
"mil": cv2.TrackerMIL_create,
"tld": cv2.TrackerTLD_create,
"medianflow": cv2.TrackerMedianFlow_create,
"mosse": cv2.TrackerMOSSE_create
}
redUpper = (9, 255, 255)
redLower = (0, 121, 0)
# initialize OpenCV's special multi-object tracker
trackers = cv2.MultiTracker_create()
if not args.get("video", False):
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
# otherwise, grab a reference to the video file
else:
vs = cv2.VideoCapture(args["video"])
# loop over frames from the video stream
while True:
# grab the current frame, then handle if we are using a
# VideoStream or VideoCapture object
# check to see if we have reached the end of the stream
frame = vs.read()
frame = frame[1] if args.get("video", False) else frame
mask = cv2.inRange(hsv, redLower, redUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
# c = max(cnts, key=cv2.contourArea)
for c in cnts:
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# only proceed if the radius meets a minimum size
if radius > 10:
# draw the circle and centroid on the frame,
# then update the list of tracked points
cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
cv2.imshow("multi", frame)
if frame is None:
break
# resize the frame (so we can process it faster)
frame = imutils.resize(frame, width=600)
(success, boxes) = trackers.update(frame)
#
# # loop over the bounding boxes and draw then on the frame
# for box in boxes:
# (x, y, w, h) = [int(v) for v in box]
# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.imshow("Frame", frame)
# key = cv2.waitKey(1) & 0xFF
#
# # if the 's' key is selected, we are going to "select" a bounding
# # box to track
# if key == ord("s"):
# # select the bounding box of the object we want to track (make
# # sure you press ENTER or SPACE after selecting the ROI)
# box = cv2.selectROI("Frame", frame, fromCenter=False,
# showCrosshair=True)
# # create a new object tracker for the bounding box and add it
# # to our multi-object tracker
# tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
# trackers.add(tracker, frame, box)
# if key == ord("q"):
# break
# # if we are using a webcam, release the pointer
# if not args.get("video", False):
# vs.stop()
# # otherwise, release the file pointer
#
# else:
# vs.release()
# # close all windows
# cv2.destroyAllWindows()