- refactored
- draw path
This commit is contained in:
+23
-19
@@ -4,23 +4,12 @@ import numpy as np
|
|||||||
import imutils as im
|
import imutils as im
|
||||||
|
|
||||||
IMG_SCALE_UP = 4
|
IMG_SCALE_UP = 4
|
||||||
TEMPLATE_SEARCH_AREA = 0.8
|
TEMPLATE_SEARCH_AREA = 0.5
|
||||||
|
|
||||||
(major_ver, minor_ver, subminor_ver) = cv2.__version__.split('.')
|
(major_ver, minor_ver, subminor_ver) = cv2.__version__.split('.')
|
||||||
print(cv2.__version__)
|
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):
|
def bbox_round(src):
|
||||||
x = int(round(src[0]))
|
x = int(round(src[0]))
|
||||||
y = int(round(src[1]))
|
y = int(round(src[1]))
|
||||||
@@ -30,10 +19,14 @@ def bbox_round(src):
|
|||||||
return x, y, w, h
|
return x, y, w, h
|
||||||
|
|
||||||
|
|
||||||
def image_crop(src, bbox: cv2.typing.Rect, search_area: float = 0):
|
def bbox_extend(bbox: cv2.typing.Rect, factor: float = 0):
|
||||||
if search_area > 0:
|
we2 = bbox[2]*factor/2
|
||||||
bbox = bbox_extend(bbox, search_area)
|
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]
|
x = bbox[0]
|
||||||
y = bbox[1]
|
y = bbox[1]
|
||||||
w = bbox[2]
|
w = bbox[2]
|
||||||
@@ -137,22 +130,26 @@ if not video.isOpened():
|
|||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
# Read first frame.
|
# Read first frame.
|
||||||
ok, frame = video.read()
|
ok, frame_init = video.read()
|
||||||
if not ok:
|
if not ok:
|
||||||
print('Cannot read video file')
|
print('Cannot read video file')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
bbox = cv2.selectROI("Frame", frame, False)
|
bbox_init = cv2.selectROI("Frame", frame_init, False)
|
||||||
image_template = process_image_bbox(image_crop(frame, bbox, TEMPLATE_SEARCH_AREA))
|
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 = Reference(image_template)
|
||||||
ref_center = ref.set()
|
ref_center = ref.set()
|
||||||
|
|
||||||
print(f"ref_center = {ref_center}")
|
print(f"ref_center = {ref_center}")
|
||||||
# Initialize tracker with first frame and bounding box
|
# Initialize tracker with first frame and bounding box
|
||||||
tracker.create()
|
tracker.create()
|
||||||
tracker.init(frame, bbox)
|
tracker.init(frame_init, bbox_init)
|
||||||
|
|
||||||
current_angle = 0
|
current_angle = 0
|
||||||
|
line_from = (ref_center[0] + bbox_template[0], ref_center[1] + bbox_template[1])
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Read a new frame
|
# Read a new frame
|
||||||
ok, frame = video.read()
|
ok, frame = video.read()
|
||||||
@@ -185,6 +182,12 @@ while True:
|
|||||||
|
|
||||||
# Calculate Frames per second (FPS)
|
# Calculate Frames per second (FPS)
|
||||||
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
|
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:
|
else:
|
||||||
# Tracking failure
|
# Tracking failure
|
||||||
cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
|
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)
|
cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
|
||||||
|
|
||||||
# Display result
|
# Display result
|
||||||
|
cv2.imshow("Path", frame_init)
|
||||||
cv2.imshow("Tracking", frame)
|
cv2.imshow("Tracking", frame)
|
||||||
cv2.imshow("image_bbox", image_template_rotated)
|
cv2.imshow("image_bbox", image_template_rotated)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user