Commit b6865dc9 authored by Крайнов Роман Анатольевич's avatar Крайнов Роман Анатольевич
Browse files

add: main file

parent 7363091a
No related merge requests found
Showing with 59 additions and 0 deletions
+59 -0
main.py 0 → 100644
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()
Supports Markdown
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