From a79517c136427f1982fbbc47dbd343806c176654 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sun, 30 Jun 2024 13:24:17 +0200 Subject: [PATCH] - refactored - draw path --- ocv_track.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/ocv_track.py b/ocv_track.py index 94ebd3e..3f2056e 100644 --- a/ocv_track.py +++ b/ocv_track.py @@ -4,23 +4,12 @@ import numpy as np import imutils as im IMG_SCALE_UP = 4 -TEMPLATE_SEARCH_AREA = 0.8 +TEMPLATE_SEARCH_AREA = 0.5 (major_ver, minor_ver, subminor_ver) = cv2.__version__.split('.') print(cv2.__version__) -def bbox_extend(bbox: cv2.typing.Rect, search_area: float): - we2 = int(round(bbox[2]*search_area/2)) - he2 = int(round(bbox[3]*search_area/2)) - res = (int(round(bbox[0]-we2)),) - res += (int(round(bbox[1]-he2)),) - res += (int(bbox[2]+2*we2),) - res += (int(bbox[3]+2*he2),) - - return res - - def bbox_round(src): x = int(round(src[0])) y = int(round(src[1])) @@ -30,10 +19,14 @@ def bbox_round(src): return x, y, w, h -def image_crop(src, bbox: cv2.typing.Rect, search_area: float = 0): - if search_area > 0: - bbox = bbox_extend(bbox, search_area) +def bbox_extend(bbox: cv2.typing.Rect, factor: float = 0): + we2 = bbox[2]*factor/2 + he2 = bbox[3]*factor/2 + return bbox_round((bbox[0]-we2, bbox[1]-he2, bbox[2]+2*we2, bbox[3]+2*he2)) + + +def image_crop(src, bbox: cv2.typing.Rect): x = bbox[0] y = bbox[1] w = bbox[2] @@ -137,22 +130,26 @@ if not video.isOpened(): sys.exit() # Read first frame. -ok, frame = video.read() +ok, frame_init = video.read() if not ok: print('Cannot read video file') sys.exit() -bbox = cv2.selectROI("Frame", frame, False) -image_template = process_image_bbox(image_crop(frame, bbox, TEMPLATE_SEARCH_AREA)) +bbox_init = cv2.selectROI("Frame", frame_init, False) +bbox_template = bbox_extend(bbox_init, TEMPLATE_SEARCH_AREA) +image_template = process_image_bbox(image_crop(frame_init, bbox_template)) ref = Reference(image_template) ref_center = ref.set() print(f"ref_center = {ref_center}") # Initialize tracker with first frame and bounding box tracker.create() -tracker.init(frame, bbox) +tracker.init(frame_init, bbox_init) current_angle = 0 +line_from = (ref_center[0] + bbox_template[0], ref_center[1] + bbox_template[1]) + + while True: # Read a new frame ok, frame = video.read() @@ -185,6 +182,12 @@ while True: # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) + + # Draw path + line_to = (round(startX + bbox[0]), round(startY + bbox[1])) + cv2.line(frame_init, line_from, line_to, (0, 0, 255), 2) + line_from = line_to + else: # Tracking failure cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) @@ -196,6 +199,7 @@ while True: cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display result + cv2.imshow("Path", frame_init) cv2.imshow("Tracking", frame) cv2.imshow("image_bbox", image_template_rotated)