1
+ from imutils .video import VideoStream
2
+ import numpy as np
3
+ import imutils
4
+ import time
5
+ import cv2
6
+
7
+ cropping = False
8
+ x_start , y_start , x_end , y_end = 0 , 0 , 0 , 0
9
+
10
+ # OpenCV object tracker implementations
11
+ OPENCV_OBJECT_TRACKERS = {
12
+ #"csrt": cv2.TrackerCSRT_create,
13
+ "kcf" : cv2 .TrackerKCF_create ,
14
+ "boosting" : cv2 .TrackerBoosting_create ,
15
+ "mil" : cv2 .TrackerMIL_create ,
16
+ "tld" : cv2 .TrackerTLD_create ,
17
+ "medianflow" : cv2 .TrackerMedianFlow_create ,
18
+ #"mosse": cv2.TrackerMOSSE_create
19
+ }
20
+
21
+ # initialize OpenCV's special multi-object tracker
22
+ trackers = cv2 .MultiTracker_create ()
23
+
24
+ video = 'C:\\ Users\\ enesa\\ Desktop\\ openCV\\ Object Tracking\\ videos\\ traffic.mp4'
25
+ cap = cv2 .VideoCapture (video )
26
+
27
+ tracker_type = "kcf"
28
+
29
+ if cap .isOpened ():
30
+ ret , frame = cap .read ()
31
+ frame = imutils .resize (frame , width = 800 )
32
+ oriFrame = np .copy (frame )
33
+ else :
34
+ ret = False
35
+
36
+ def mouse_crop (event , x , y , flags , param ):
37
+
38
+ global x_start , y_start , x_end , y_end , cropping
39
+
40
+ # if the left click of mouse was DOWN, start RECORDING
41
+ # (x, y) coordinates and indicate that cropping is being
42
+ if event == cv2 .EVENT_LBUTTONDOWN :
43
+ x_start , y_start , x_end , y_end = x , y , x , y
44
+ cropping = True
45
+
46
+ # Mouse is Moving
47
+ elif event == cv2 .EVENT_MOUSEMOVE :
48
+ if cropping == True :
49
+ x_end , y_end = x , y
50
+
51
+ # if the left click of mouse button was released
52
+ elif event == cv2 .EVENT_LBUTTONUP :
53
+ # record the ending (x, y) coordinates
54
+ x_end , y_end = x , y
55
+ cropping = False # cropping is finished
56
+
57
+ #refPoint = [(x_start, y_start), (x_end, y_end)]
58
+
59
+ #if len(refPoint) == 2: #when two points were found
60
+ #roi = oriFrame[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
61
+ #cv2.imshow("Cropped", roi)
62
+
63
+ cv2 .namedWindow ("Object Tracking" )
64
+ cv2 .setMouseCallback ("Object Tracking" , mouse_crop )
65
+
66
+
67
+ while True :
68
+
69
+ frame = cap .read ()
70
+ frame = frame [1 ]
71
+
72
+ # check to see if we have reached the end of the stream
73
+ if frame is None :
74
+ break
75
+
76
+ # resize the frame (so we can process it faster)
77
+ frame = imutils .resize (frame , width = 800 )
78
+ i = np .copy (frame )
79
+ # Start timer
80
+ timer = cv2 .getTickCount ()
81
+
82
+ # grab the updated bounding box coordinates (if any) for each object that is being tracked
83
+ #(success, boxes) = trackers.update(frame)
84
+
85
+ # Calculate Frames per second (FPS)
86
+ fps = cv2 .getTickFrequency () / (cv2 .getTickCount () - timer );
87
+
88
+
89
+ # loop over the bounding boxes and draw then on the frame
90
+ # for box in boxes:
91
+ # (x, y, w, h) = [int(v) for v in box]
92
+ # cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
93
+
94
+
95
+ if not cropping :
96
+ cv2 .imshow ("Object Tracking" , frame )
97
+
98
+ elif cropping :
99
+ cv2 .rectangle (i , (x_start , y_start ), (x_end , y_end ), (255 , 0 , 0 ), 2 )
100
+ cv2 .imshow ("Object Tracking" , i )
101
+
102
+ roi = i [y_start :y_end , x_start :x_end ]
103
+ cv2 .imshow ("Cropped" , roi )
104
+
105
+
106
+
107
+ # Display tracker type on frame
108
+ #cv2.putText(frame, tracker_type + " Tracker", (10,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
109
+
110
+ # Display FPS on frame
111
+ #cv2.putText(frame, "FPS : " + str(int(fps)), (10,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
112
+
113
+ # show the output frame
114
+ #cv2.imshow("Frame", frame)
115
+ key = cv2 .waitKey (10 ) & 0xFF
116
+
117
+ # if the 's' key is selected, we are going to "select" a bounding
118
+ # box to track
119
+ # if key == ord("s"):
120
+ # # select the bounding box of the object we want to track (make
121
+ # # sure you press ENTER or SPACE after selecting the ROI)
122
+ # box = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True)
123
+
124
+ # # create a new object tracker for the bounding box and add it to our multi-object tracker
125
+ # tracker = OPENCV_OBJECT_TRACKERS["kcf"]()
126
+ # trackers.add(tracker, frame, box)
127
+
128
+ # if the `q` key was pressed, break from the loop
129
+ if key == ord ("q" ):
130
+ break
131
+
132
+
133
+ cap .release ()
134
+ cv2 .destroyAllWindows ()
0 commit comments