Object Tracker Implementation

parent 0e1c226a
import cv2
import sys
import csv
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
frameNo = 0
if __name__ == '__main__' :
# Set up tracker.
# Instead of MIL, you can also use
tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
tracker_type = tracker_types[7]
if int(minor_ver) < 3:
tracker = cv2.Tracker_create(tracker_type)
else:
if tracker_type == 'BOOSTING':
tracker = cv2.TrackerBoosting_create()
if tracker_type == 'MIL':
tracker = cv2.TrackerMIL_create()
if tracker_type == 'KCF':
tracker = cv2.TrackerKCF_create()
if tracker_type == 'TLD':
tracker = cv2.TrackerTLD_create()
if tracker_type == 'MEDIANFLOW':
tracker = cv2.TrackerMedianFlow_create()
if tracker_type == 'GOTURN':
tracker = cv2.TrackerGOTURN_create()
if tracker_type == 'MOSSE':
tracker = cv2.TrackerMOSSE_create()
if tracker_type == "CSRT":
tracker = cv2.TrackerCSRT_create()
# Read video
video = cv2.VideoCapture("EYEDIAP/EYEDIAP/1_A_FT_M/1_A_FT_M.mov")
# VIDEO_STREAM = 'eyediap1.mov'
VIDEO_STREAM_OUT = 'output_eyediap1_ot2.mp4'
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# video = cv2.VideoCapture(VIDEO_STREAM)
ok, frame = video.read()
# frame = cv2.resize(frame, (640,360), interpolation=cv2.INTER_AREA) #original
# frame = cv2.resize(frame, (640,480), interpolation=cv2.INTER_AREA) #edited to resize
# frame = cv2.resize(frame, (1120,790), interpolation=cv2.INTER_AREA) #full screen size
writer = cv2.VideoWriter(VIDEO_STREAM_OUT, fourcc, 30, (frame.shape[1],frame.shape[0]), True)
# Exit if video not opened.
if not video.isOpened():
# print 'Could not open video'
sys.exit()
# Read first frame.
ok, frame = video.read()
if not ok:
# print 'Cannot read video file'
sys.exit()
# Define an initial bounding box
#bbox = (287, 23, 86, 320)
# Uncomment the line below to select a different bounding box
bbox = cv2.selectROI(frame, False)
# Initialize tracker with first frame and bounding box
ok = tracker.init(frame, bbox)
print(frame.shape)
#Writing data to the csv file
with open('1_A_FT_M.csv', mode='w') as object_track_file:
OT_writer = csv.writer(object_track_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
OT_writer.writerow(['Frame_number','x_displacement','y_displacement', 'Tracker_x1', 'Tracker_y1', 'Tracker_x2', 'Tracker_y2'])
while True:
#incrementing the frame count
frameNo = frameNo + 1
# Read a new frame
ok, frame = video.read()
if not ok:
break
# Start timer
timer = cv2.getTickCount()
# Update tracker
ok, bbox = tracker.update(frame)
# Calculate Frames per second (FPS)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
# Draw bounding box
if ok:
# Tracking success
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
x1 = int(bbox[0])
y1 = int(bbox[1])
x2 = int(bbox[0] + bbox[2])
y2 = int(bbox[1] + bbox[3])
x1y1 = x1,y1
x2y2 = x2,y2
cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
#centroid Calculation
avgX = (p1[0]+p2[0])/2
avgY = (p1[1]+p2[1])/2
print("Frame No : " + str(int(frameNo)))
print("FPS : " + str(int(fps)),"----", p1, " , ", p2)
print("X displacement = ", avgX)
print("Y displacement = ", avgY)
# print('x1y1:',x1y1, '.....', 'x2y2:', x2y2)
print("-----------------------------------------------")
# Writing data to the csv file
OT_writer.writerow([frameNo, avgX, avgY, x1, y1, x2, y2])
else :
# Tracking failure
cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
# Display tracker type on frame
cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
# cv2.putText(frameNo, "FRAME No : " + str(int(frameNo)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
# Display FPS on frame
cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
# Display result
cv2.imshow("Tracking", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27 : break
cv2.destroyAllWindows()
writer.release()
Splitting data in csv to different columns
https://support.microsoft.com/en-us/office/split-text-into-different-columns-with-the-convert-text-to-columns-wizard-30b14928-5550-41f5-97ca-7a3e9c363ed7
1) Select the cell or column that contains the text you want to split.
2) Select Data > Text to Columns.
3) In the Convert Text to Columns Wizard, select Delimited > Next.
4) Select the Delimiters for your data. For example, Comma and Space. You can see a preview of your data in the Data preview window.
5) Select Next.
6) Select the Destination in your worksheet which is where you want the split data to appear.
7) Select Finish.
\ No newline at end of file
#Link to tutorial: https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/
import pandas as pd
read_file = pd.read_csv (r'EYEDIAP\EYEDIAP\2_A_FT_M\ball_tracking.txt')
read_file.to_csv (r'csv_files\Plotting Graphs\2_A_FT_M\ball_tracking.csv', index=None)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Frame_number,x_displacement,y_displacement
1,207.0,335.5
2,212.5,335.5
3,217.5,336.0
4,223.0,335.5
5,228.5,337.0
6,234.5,338.0
7,240.0,338.5
8,245.0,339.5
9,250.5,340.0
10,256.5,341.0
11,263.5,342.0
12,269.5,342.0
13,275.0,342.5
14,280.5,343.0
15,288.0,345.5
16,293.5,346.0
17,298.5,347.0
18,306.0,347.5
19,311.0,349.5
20,318.5,350.0
21,326.0,351.5
22,332.5,352.0
23,339.0,352.5
24,345.5,353.0
25,352.5,353.0
26,359.5,353.0
27,367.0,352.5
28,374.0,353.5
29,381.0,352.5
30,388.5,354.0
31,396.0,355.5
32,404.0,354.5
33,411.0,355.5
34,418.5,357.0
35,425.5,357.0
36,433.0,357.5
37,440.5,359.0
38,448.0,358.5
39,455.5,361.0
40,464.0,361.5
41,471.5,362.0
42,478.5,362.0
43,487.5,362.0
44,494.5,361.0
45,503.5,359.0
46,511.5,356.0
47,519.0,353.5
48,528.5,353.0
49,537.5,352.0
50,546.5,352.0
51,555.5,353.0
52,566.5,352.0
53,575.0,353.5
54,585.0,353.5
55,594.5,351.0
56,604.5,349.0
57,612.5,344.0
58,618.5,336.0
59,626.5,331.5
60,634.5,324.5
61,638.5,315.5
62,639.5,308.5
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
## REFERENCE
# LINK: https://pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
#Original Code
# import the necessary packages
from collections import namedtuple
import numpy as np
import cv2
# define the `Detection` object
Detection = namedtuple("Detection", ["image_path", "gt", "pred"])
def bb_intersection_over_union(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle (The interArea variable represents the numerator in the Intersection over Union calculation)
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
# compute the area of both the prediction and ground-truth rectangles
# (To compute the denominator we first need to derive the area of both the predicted bounding box and the ground-truth bounding box)
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
# return the intersection over union value
return iou
# define the list of example detections
examples = [
Detection("IoU_Sample_Images/car1_1.png", [64, 83, 223, 125], [77, 83, 203, 124]),
Detection("IoU_Sample_Images/car2.png", [58, 96, 247, 155], [53, 98, 227, 156])]
# Detection("image_0075.jpg", [31, 69, 201, 125], [18, 63, 235, 135]),
# Detection("image_0090.jpg", [50, 72, 197, 121], [54, 72, 198, 120]),
# Detection("image_0120.jpg", [35, 51, 196, 110], [36, 60, 180, 108])]
# loop over the example detections
for detection in examples:
# load the image
image = cv2.imread(detection.image_path)
# draw the ground-truth bounding box along with the predicted
# bounding box
cv2.rectangle(image, tuple(detection.gt[:2]),
tuple(detection.gt[2:]), (0, 255, 0), 2)
cv2.rectangle(image, tuple(detection.pred[:2]),
tuple(detection.pred[2:]), (0, 0, 255), 2)
# compute the intersection over union and display it
iou = bb_intersection_over_union(detection.gt, detection.pred)
cv2.putText(image, "IoU: {:.4f}".format(iou), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
print("{}: {:.4f}".format(detection.image_path, iou))
# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment