Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Крайнов Роман Анатольевич
mmedia
Commits
b6865dc9
Commit
b6865dc9
authored
6 months ago
by
Крайнов Роман Анатольевич
Browse files
Options
Download
Patches
Plain Diff
add: main file
parent
7363091a
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.py
+59
-0
main.py
with
59 additions
and
0 deletions
+59
-0
main.py
0 → 100644
+
59
−
0
View file @
b6865dc9
import
cv2
import
numpy
as
np
def
track_object_in_video
(
input_video_path
,
output_video_path
,
lower_color_range
,
upper_color_range
):
video_capture
=
cv2
.
VideoCapture
(
input_video_path
)
frame_width
=
int
(
video_capture
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
))
frame_height
=
int
(
video_capture
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
))
frame_rate
=
video_capture
.
get
(
cv2
.
CAP_PROP_FPS
)
video_writer
=
cv2
.
VideoWriter
(
output_video_path
,
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
),
frame_rate
,
(
frame_width
,
frame_height
))
tracked_points
=
[]
while
True
:
ret
,
current_frame
=
video_capture
.
read
()
if
not
ret
:
break
hsv_frame
=
cv2
.
cvtColor
(
current_frame
,
cv2
.
COLOR_BGR2HSV
)
mask
=
cv2
.
inRange
(
hsv_frame
,
lower_color_range
,
upper_color_range
)
_
,
binary_image
=
cv2
.
threshold
(
mask
,
127
,
255
,
cv2
.
THRESH_BINARY
)
contours
,
_
=
cv2
.
findContours
(
binary_image
,
cv2
.
RETR_TREE
,
cv2
.
CHAIN_APPROX_SIMPLE
)
if
contours
:
largest_contour
=
max
(
contours
,
key
=
cv2
.
contourArea
)
moments
=
cv2
.
moments
(
largest_contour
)
if
moments
[
'm00'
]
!=
0
:
center_x
=
int
(
moments
[
'm10'
]
/
moments
[
'm00'
])
center_y
=
int
(
moments
[
'm01'
]
/
moments
[
'm00'
])
tracked_points
.
append
((
center_x
,
center_y
))
cv2
.
drawMarker
(
current_frame
,
(
center_x
,
center_y
),
(
0
,
255
,
0
),
markerType
=
cv2
.
MARKER_CROSS
)
for
point
in
tracked_points
:
cv2
.
drawMarker
(
current_frame
,
point
,
(
255
,
0
,
0
),
markerType
=
cv2
.
MARKER_CROSS
)
video_writer
.
write
(
current_frame
)
cv2
.
imshow
(
'Object Tracking'
,
current_frame
)
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'q'
):
break
video_capture
.
release
()
video_writer
.
release
()
cv2
.
destroyAllWindows
()
def
main
():
input_video
=
r
'./IMG_2304.MOV'
output_video
=
'tracked_output_video.mp4'
black_lower_bound
=
np
.
array
([
0
,
0
,
0
])
black_upper_bound
=
np
.
array
([
180
,
255
,
50
])
track_object_in_video
(
input_video
,
output_video
,
black_lower_bound
,
black_upper_bound
)
if
__name__
==
"__main__"
:
main
()
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets