Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-074
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-074
2022-074
Commits
868f042d
Commit
868f042d
authored
Jun 19, 2022
by
IT19110530-Pramodini A.A.D.A
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace Object_Tracker2.py
parent
e7a3e9ba
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
128 additions
and
127 deletions
+128
-127
Object_Tracker/Object_Tracker2.py
Object_Tracker/Object_Tracker2.py
+128
-127
No files found.
Object_Tracker/Object_Tracker2.py
View file @
868f042d
#REFERENCE MATERIAL
#https://livecodestream.dev/post/object-tracking-with-opencv/
import
cv2
import
sys
import
csv
import
pandas
as
pd
(
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'
]
# Set up tracker
tracker_types
=
[
'BOOSTING'
,
'MIL'
,
'KCF'
,
'TLD'
,
'MEDIANFLOW'
,
'GOTURN'
,
'MOSSE'
,
'CSRT'
]
tracker_type
=
tracker_types
[
7
]
if
int
(
minor_ver
)
<
3
:
...
...
@@ -20,65 +21,54 @@ if __name__ == '__main__' :
else
:
if
tracker_type
==
'BOOSTING'
:
tracker
=
cv2
.
TrackerBoosting_create
()
if
tracker_type
==
'MIL'
:
el
if
tracker_type
==
'MIL'
:
tracker
=
cv2
.
TrackerMIL_create
()
if
tracker_type
==
'KCF'
:
el
if
tracker_type
==
'KCF'
:
tracker
=
cv2
.
TrackerKCF_create
()
if
tracker_type
==
'TLD'
:
el
if
tracker_type
==
'TLD'
:
tracker
=
cv2
.
TrackerTLD_create
()
if
tracker_type
==
'MEDIANFLOW'
:
el
if
tracker_type
==
'MEDIANFLOW'
:
tracker
=
cv2
.
TrackerMedianFlow_create
()
if
tracker_type
==
'GOTURN'
:
el
if
tracker_type
==
'GOTURN'
:
tracker
=
cv2
.
TrackerGOTURN_create
()
if
tracker_type
==
'MOSSE'
:
el
if
tracker_type
==
'MOSSE'
:
tracker
=
cv2
.
TrackerMOSSE_create
()
if
tracker_type
==
"CSRT"
:
el
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'
)
# Read video
video
=
cv2
.
VideoCapture
(
'EYEDIAP
\
EYEDIAP
\
_6_A_FT_M
\
_6_A_FT_M.mov'
)
# 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'
# 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'
# 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)
# 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
)
# 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
)
# Initialize tracker with first frame and bounding box
ok
=
tracker
.
init
(
frame
,
bbox
)
#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'
])
# Defining arrays to store values
df_frame
,
df_centerX
,
df_centerY
,
df_x1
,
df_y1
,
df_x2
,
df_y2
=
[],[],[],[],[],[],[]
frameNo
=
0
while
True
:
# Frame count
frameNo
+=
1
while
True
:
#incrementing the frame count
frameNo
=
frameNo
+
1
# Read a new frame
ok
,
frame
=
video
.
read
()
if
not
ok
:
...
...
@@ -91,8 +81,7 @@ with open('1_A_FT_M.csv', mode='w') as object_track_file:
ok
,
bbox
=
tracker
.
update
(
frame
)
# Calculate Frames per second (FPS)
fps
=
cv2
.
getTickFrequency
()
/
(
cv2
.
getTickCount
()
-
timer
)
fps
=
cv2
.
getTickFrequency
()
/
(
cv2
.
getTickCount
()
-
timer
);
# Draw bounding box
if
ok
:
...
...
@@ -106,14 +95,32 @@ with open('1_A_FT_M.csv', mode='w') as object_track_file:
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
cv2
.
rectangle
(
frame
,
p1
,
p2
,
(
255
,
0
,
0
),
2
,
1
)
else
:
# Tracking failure
cv2
.
putText
(
frame
,
"Tracking failure detected"
,
(
100
,
80
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.75
,(
0
,
0
,
255
),
2
)
#Initializing values
avgX
=
0
avgY
=
0
x1
=
0
y1
=
0
x2
=
0
y2
=
0
df_frame
.
append
(
frameNo
)
df_centerX
.
append
(
avgX
)
df_centerY
.
append
(
avgY
)
df_x1
.
append
(
x1
)
df_y1
.
append
(
y1
)
df_x2
.
append
(
x2
)
df_y2
.
append
(
y2
)
print
(
"Frame No : "
+
str
(
int
(
frameNo
)))
print
(
"FPS : "
+
str
(
int
(
fps
)),
"----"
,
p1
,
" , "
,
p2
)
print
(
"X displacement = "
,
avgX
)
...
...
@@ -121,30 +128,24 @@ with open('1_A_FT_M.csv', mode='w') as object_track_file:
# 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
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'q'
):
# if press SPACE bar
break
# CSV File generation
df_dict
=
{
'Frame_number'
:
df_frame
,
'Tracker_center(x)'
:
df_centerX
,
'Tracker_center(y)'
:
df_centerY
,
'Tracker_x1'
:
df_x1
,
'Tracker_y1'
:
df_y1
,
'Tracker_x2'
:
df_x2
,
'Tracker_y2'
:
df_y2
}
df
=
pd
.
DataFrame
(
df_dict
)
print
(
df
.
head
)
df
.
to_csv
(
"csv_files/Eyediap_csv/Tracker_output_6_A_FT_M.csv"
,
index
=
False
)
video
.
release
()
cv2
.
destroyAllWindows
()
\ No newline at end of file
writer
.
release
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment