Skip to content

Commit c08bea4

Browse files
authored
Add files via upload
1 parent 4bf8016 commit c08bea4

4 files changed

Lines changed: 333 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from imutils.video import VideoStream
2+
import imutils
3+
import time
4+
import cv2
5+
6+
7+
# OpenCV object tracker implementations
8+
OPENCV_OBJECT_TRACKERS = {
9+
#"csrt": cv2.TrackerCSRT_create,
10+
"kcf": cv2.TrackerKCF_create,
11+
"boosting": cv2.TrackerBoosting_create,
12+
"mil": cv2.TrackerMIL_create,
13+
"tld": cv2.TrackerTLD_create,
14+
"medianflow": cv2.TrackerMedianFlow_create,
15+
#"mosse": cv2.TrackerMOSSE_create
16+
}
17+
18+
# initialize OpenCV's special multi-object tracker
19+
trackers = cv2.MultiTracker_create()
20+
21+
video = 'videos/los_angeles.mp4'
22+
cap = cv2.VideoCapture(video)
23+
24+
25+
while True:
26+
27+
frame = cap.read()
28+
frame = frame[1]
29+
30+
# check to see if we have reached the end of the stream
31+
if frame is None:
32+
break
33+
34+
# resize the frame (so we can process it faster)
35+
frame = imutils.resize(frame, width=600)
36+
37+
# grab the updated bounding box coordinates (if any) for each object that is being tracked
38+
(success, boxes) = trackers.update(frame)
39+
40+
# loop over the bounding boxes and draw then on the frame
41+
for box in boxes:
42+
(x, y, w, h) = [int(v) for v in box]
43+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
44+
45+
# show the output frame
46+
cv2.imshow("Frame", frame)
47+
key = cv2.waitKey(1) & 0xFF
48+
49+
# if the 's' key is selected, we are going to "select" a bounding
50+
# box to track
51+
if key == ord("s"):
52+
# select the bounding box of the object we want to track (make
53+
# sure you press ENTER or SPACE after selecting the ROI)
54+
box = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True)
55+
56+
57+
# create a new object tracker for the bounding box and add it to our multi-object tracker
58+
tracker = OPENCV_OBJECT_TRACKERS["kcf"]()
59+
trackers.add(tracker, frame, box)
60+
61+
# if the `q` key was pressed, break from the loop
62+
elif key == ord("q"):
63+
break
64+
65+
66+
cap.release()
67+
cv2.destroyAllWindows()
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import cv2
2+
import sys
3+
4+
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
5+
6+
if __name__ == '__main__' :
7+
8+
# Set up tracker.
9+
# Instead of MIL, you can also use
10+
11+
tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
12+
tracker_type = tracker_types[2]
13+
14+
if int(minor_ver) < 3:
15+
tracker = cv2.Tracker_create(tracker_type)
16+
else:
17+
if tracker_type == 'BOOSTING':
18+
tracker = cv2.TrackerBoosting_create()
19+
if tracker_type == 'MIL':
20+
tracker = cv2.TrackerMIL_create()
21+
if tracker_type == 'KCF':
22+
tracker = cv2.TrackerKCF_create()
23+
if tracker_type == 'TLD':
24+
tracker = cv2.TrackerTLD_create()
25+
if tracker_type == 'MEDIANFLOW':
26+
tracker = cv2.TrackerMedianFlow_create()
27+
if tracker_type == 'GOTURN':
28+
tracker = cv2.TrackerGOTURN_create()
29+
if tracker_type == 'MOSSE':
30+
tracker = cv2.TrackerMOSSE_create()
31+
if tracker_type == "CSRT":
32+
tracker = cv2.TrackerCSRT_create()
33+
34+
# Read video
35+
video = 'videos/traffic.mp4'
36+
cap = cv2.VideoCapture(video)
37+
38+
# Exit if video not opened.
39+
if not cap.isOpened():
40+
print ("Could not open video")
41+
sys.exit()
42+
43+
# Read first frame.
44+
ok, frame = cap.read()
45+
if not ok:
46+
print ('Cannot read video file')
47+
sys.exit()
48+
49+
# Define an initial bounding box
50+
bbox = (287, 23, 86, 320)
51+
52+
# Uncomment the line below to select a different bounding box
53+
bbox = cv2.selectROI(frame, False)
54+
55+
# Initialize tracker with first frame and bounding box
56+
ok = tracker.init(frame, bbox)
57+
58+
while True:
59+
# Read a new frame
60+
ok, frame = cap.read()
61+
if not ok:
62+
break
63+
64+
# Start timer
65+
timer = cv2.getTickCount()
66+
67+
# Update tracker
68+
ok, bbox = tracker.update(frame)
69+
70+
# Calculate Frames per second (FPS)
71+
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
72+
73+
# Draw bounding box
74+
if ok:
75+
# Tracking success
76+
p1 = (int(bbox[0]), int(bbox[1]))
77+
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
78+
cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
79+
else :
80+
# Tracking failure
81+
cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
82+
83+
# Display tracker type on frame
84+
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
85+
86+
# Display FPS on frame
87+
cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
88+
89+
# Display result
90+
cv2.imshow("Tracking", frame)
91+
92+
# Exit if ESC pressed
93+
k = cv2.waitKey(1) & 0xff
94+
if k == 27 : break
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# https://www.learnopencv.com/object-tracking-using-opencv-cpp-python/
2+
3+
from imutils.video import VideoStream
4+
import imutils
5+
import time
6+
import cv2
7+
8+
# OpenCV object tracker implementations
9+
OPENCV_OBJECT_TRACKERS = {
10+
#"csrt": cv2.TrackerCSRT_create,
11+
"kcf": cv2.TrackerKCF_create,
12+
"boosting": cv2.TrackerBoosting_create,
13+
"mil": cv2.TrackerMIL_create,
14+
"tld": cv2.TrackerTLD_create,
15+
"medianflow": cv2.TrackerMedianFlow_create,
16+
#"mosse": cv2.TrackerMOSSE_create
17+
}
18+
19+
# initialize OpenCV's special multi-object tracker
20+
trackers = cv2.MultiTracker_create()
21+
22+
video = 'videos/traffic.mp4'
23+
cap = cv2.VideoCapture(video)
24+
25+
tracker_type = "kcf"
26+
27+
while True:
28+
29+
frame = cap.read()
30+
frame = frame[1]
31+
32+
# check to see if we have reached the end of the stream
33+
if frame is None:
34+
break
35+
36+
# resize the frame (so we can process it faster)
37+
frame = imutils.resize(frame, width=800)
38+
39+
# Start timer
40+
timer = cv2.getTickCount()
41+
42+
# grab the updated bounding box coordinates (if any) for each object that is being tracked
43+
(success, boxes) = trackers.update(frame)
44+
45+
# Calculate Frames per second (FPS)
46+
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
47+
48+
49+
# loop over the bounding boxes and draw then on the frame
50+
for box in boxes:
51+
(x, y, w, h) = [int(v) for v in box]
52+
if success:
53+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
54+
55+
else:
56+
# Tracking failure
57+
cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
58+
59+
60+
# Display tracker type on frame
61+
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
62+
63+
# Display FPS on frame
64+
cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
65+
66+
# show the output frame
67+
cv2.imshow("Frame", frame)
68+
key = cv2.waitKey(1) & 0xFF
69+
70+
# if the 's' key is selected, we are going to "select" a bounding
71+
# box to track
72+
if key == ord("s"):
73+
# select the bounding box of the object we want to track (make
74+
# sure you press ENTER or SPACE after selecting the ROI)
75+
box = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True)
76+
success = True
77+
78+
79+
# create a new object tracker for the bounding box and add it to our multi-object tracker
80+
tracker = OPENCV_OBJECT_TRACKERS["kcf"]()
81+
trackers.add(tracker, frame, box)
82+
83+
# if the `q` key was pressed, break from the loop
84+
elif key == ord("q"):
85+
break
86+
87+
88+
cap.release()
89+
cv2.destroyAllWindows()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'''
2+
Using Correlation Trackers in Dlib, you can track any object in a video stream without needing to train a custom object detector.
3+
Check out the tutorial at: http://www.codesofinterest.com/2018/02/track-any-object-in-video-with-dlib.html
4+
'''
5+
import numpy as np
6+
import cv2
7+
import dlib
8+
9+
# this variable will hold the coordinates of the mouse click events.
10+
mousePoints = []
11+
12+
def mouseEventHandler(event, x, y, flags, param):
13+
# references to the global mousePoints variable
14+
global mousePoints
15+
16+
# if the left mouse button was clicked, record the starting coordinates.
17+
if event == cv2.EVENT_LBUTTONDOWN:
18+
mousePoints = [(x, y)]
19+
20+
# when the left mouse button is released, record the ending coordinates.
21+
elif event == cv2.EVENT_LBUTTONUP:
22+
mousePoints.append((x, y))
23+
24+
# create the video capture.
25+
video_capture = cv2.VideoCapture(0)
26+
27+
# create a named window in OpenCV and attach the mouse event handler to it.
28+
cv2.namedWindow("Webcam stream")
29+
cv2.setMouseCallback("Webcam stream", mouseEventHandler)
30+
31+
# initialize the correlation tracker.
32+
tracker = dlib.correlation_tracker()
33+
34+
# this is the variable indicating whether to track the object or not.
35+
tracked = False
36+
37+
while True:
38+
# start capturing the video stream.
39+
ret, frame = video_capture.read()
40+
41+
if ret:
42+
image = frame
43+
44+
# if we have two sets of coordinates from the mouse event, draw a rectangle.
45+
if len(mousePoints) == 2:
46+
cv2.rectangle(image, mousePoints[0], mousePoints[1], (0, 255, 0), 2)
47+
dlib_rect = dlib.rectangle(mousePoints[0][0], mousePoints[0][1], mousePoints[1][0], mousePoints[1][1])
48+
49+
# tracking in progress, update the correlation tracker and get the object position.
50+
if tracked == True:
51+
tracker.update(image)
52+
track_rect = tracker.get_position()
53+
x = int(track_rect.left())
54+
y = int(track_rect.top())
55+
x1 = int(track_rect.right())
56+
y1 = int(track_rect.bottom())
57+
cv2.rectangle(image, (x, y), (x1, y1), (0, 0, 255), 2)
58+
59+
# show the current frame.
60+
cv2.imshow("Webcam stream", image)
61+
62+
# capture the keyboard event in the OpenCV window.
63+
ch = 0xFF & cv2.waitKey(1)
64+
65+
# press "r" to stop tracking and reset the points.
66+
if ch == ord("r"):
67+
mousePoints = []
68+
tracked = False
69+
70+
# press "t" to start tracking the currently selected object/area.
71+
if ch == ord("t"):
72+
if len(mousePoints) == 2:
73+
tracker.start_track(image, dlib_rect)
74+
tracked = True
75+
mousePoints = []
76+
77+
# press "q" to quit the program.
78+
if ch == ord('q'):
79+
break
80+
81+
# cleanup.
82+
video_capture.release()
83+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)