Skip to content

Commit 1ea0dce

Browse files
authored
Add files via upload
1 parent c08bea4 commit 1ea0dce

File tree

5 files changed

+419
-0
lines changed

5 files changed

+419
-0
lines changed

03-07-2019-at-night/a.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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()

03-07-2019-at-night/b.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from imutils.video import VideoStream
2+
import numpy as np
3+
import imutils
4+
import time
5+
import cv2
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/soccer_02.mp4'
22+
cap = cv2.VideoCapture(video)
23+
24+
tracker_type = "kcf"
25+
tracker = OPENCV_OBJECT_TRACKERS["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+
box = cv2.selectROI("Frame", np.copy(frame), fromCenter=False, showCrosshair=True)
49+
50+
if box:
51+
# create a new object tracker for the bounding box and add it to our multi-object tracker
52+
trackers.add(tracker, np.copy(frame), box)
53+
54+
55+
# loop over the bounding boxes and draw then on the frame
56+
for box in boxes:
57+
(x, y, w, h) = [int(v) for v in box]
58+
cv2.rectangle(np.copy(frame), (x, y), (x + w, y + h), (0, 255, 0), 2)
59+
60+
61+
# Display tracker type on frame
62+
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
63+
64+
# Display FPS on frame
65+
cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
66+
67+
# show the output frame
68+
cv2.imshow("Frame", frame)
69+
key = cv2.waitKey(1) & 0xFF
70+
71+
72+
# if the `q` key was pressed, break from the loop
73+
if key == ord("q"):
74+
break
75+
76+
77+
cap.release()
78+
cv2.destroyAllWindows()

03-07-2019-at-night/select_Roi01.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import cv2
2+
import numpy as np
3+
4+
cropping = False
5+
x_start, y_start, x_end, y_end = 0, 0, 0, 0
6+
7+
image = cv2.imread('videos/baykar_makina_logo.jpg')
8+
oriImage = image.copy()
9+
10+
11+
def mouse_crop(event, x, y, flags, param):
12+
13+
global x_start, y_start, x_end, y_end, cropping
14+
15+
# if the left click of mouse was DOWN, start RECORDING
16+
# (x, y) coordinates and indicate that cropping is being
17+
if event == cv2.EVENT_LBUTTONDOWN:
18+
x_start, y_start, x_end, y_end = x, y, x, y
19+
cropping = True
20+
21+
# Mouse is Moving
22+
elif event == cv2.EVENT_MOUSEMOVE:
23+
if cropping == True:
24+
x_end, y_end = x, y
25+
26+
# if the left click of mouse button was released
27+
elif event == cv2.EVENT_LBUTTONUP:
28+
# record the ending (x, y) coordinates
29+
x_end, y_end = x, y
30+
cropping = False # cropping is finished
31+
32+
refPoint = [(x_start, y_start), (x_end, y_end)]
33+
34+
if len(refPoint) == 2: #when two points were found
35+
roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
36+
cv2.imshow("Cropped", roi)
37+
38+
cv2.namedWindow("image")
39+
cv2.setMouseCallback("image", mouse_crop)
40+
41+
while True:
42+
43+
i = image.copy()
44+
45+
if not cropping:
46+
cv2.imshow("image", image)
47+
48+
elif cropping:
49+
cv2.rectangle(i, (x_start, y_start), (x_end, y_end), (255, 0, 0), 2)
50+
#i[y_start:y_end, x_start:x_end]
51+
cv2.imshow("image", i)
52+
53+
key = cv2.waitKey(1) & 0xFF
54+
55+
if key == ord("q"):
56+
break
57+
58+
# close all open windows
59+
cv2.destroyAllWindows()

03-07-2019-at-night/select_Roi02.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import cv2
2+
import numpy as np
3+
4+
if __name__ == '__main__' :
5+
6+
# Read image
7+
im = cv2.imread("videos/baykar_makina_logo.jpg")
8+
9+
# Select ROI
10+
rects = []
11+
r = cv2.selectROI("Frame", im, rects, fromCenter=False, showCrosshair=False)
12+
13+
# Crop image
14+
imCrop = im[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]
15+
16+
# Display cropped image
17+
cv2.imshow("Image", imCrop)
18+
cv2.waitKey(0)

0 commit comments

Comments
 (0)