From 4bc1e56dadf38e70892fbf64a81e9644e219710e Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Tue, 31 Dec 2024 14:46:27 +0100 Subject: [PATCH] Faster cv rendering with polylines --- trap/tools.py | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/trap/tools.py b/trap/tools.py index 2aecf8b..7fb6731 100644 --- a/trap/tools.py +++ b/trap/tools.py @@ -200,7 +200,7 @@ def transition_path_points(path: np.array, t: float): # distance = cum_lenghts[-1] * t # ts = np.concatenate((np.array([0.]), cum_lenghts / cum_lenghts[-1])) # print(cum_lenghts[-1]) - DRAW_SPEED = 30 # fixed speed (independent of lenght) TODO)) make variable + DRAW_SPEED = 35 # fixed speed (independent of lenght) TODO)) make variable ts = np.concatenate((np.array([0.]), cum_lenghts / DRAW_SPEED)) new_path = [path[0]] @@ -235,6 +235,7 @@ def draw_track_predictions(img: cv2.Mat, track: Track, color_index: int, camera: # if convert_points: # current_point = convert_points([current_point])[0] + lines = [] for pred_i, pred in enumerate(track.predictions): pred_coords = pred #cv2.perspectiveTransform(np.array([pred]), inv_H)[0].tolist() # line_points = pred_coords @@ -249,23 +250,15 @@ def draw_track_predictions(img: cv2.Mat, track: Track, color_index: int, camera: color = bgr_colors[color_index % len(bgr_colors)] color = tuple([int(c*opacity) for c in color]) - - for start, end in zip(line_points[:-1], line_points[1:]): - # for ci in range(0, len(pred_coords)): - # if ci == 0: - # # TODO)) prev point - # # continue - # start = [int(p) for p in current_point] - # # start = [int(p) for p in coords[-1]] - # # start = [0,0]? - # # print(start) - # else: - # start = [int(p) for p in pred_coords[ci-1]] - # end = [int(p) for p in pred_coords[ci]] - # print(np.rint(start),np.rint(end).tolist()) - cv2.line(img, start, end, color, 2, lineType=cv2.LINE_AA) - pass - # cv2.circle(img, end, 2, color, 1, lineType=cv2.LINE_AA) + line_points = line_points.reshape((-1,1,2)) + lines.append(line_points) + + # draw in a single pass + cv2.polylines(img, lines, False, color, 2, cv2.LINE_AA) + # for start, end in zip(line_points[:-1], line_points[1:]): + # cv2.line(img, start, end, color, 2, lineType=cv2.LINE_AA) + # pass + # # cv2.circle(img, end, 2, color, 1, lineType=cv2.LINE_AA) def draw_trackjectron_history(img: cv2.Mat, track: Track, color_index: int, convert_points: Optional[Callable]): if not track.predictor_history: @@ -301,12 +294,15 @@ def draw_track_projected(img: cv2.Mat, track: Track, color_index: int, camera: C point_color = bgr_colors[color_index % len(bgr_colors)] cv2.circle(img, to_point(history[0]), 3, point_color, 2) + + points = np.rint(history.reshape((-1,1,2))).astype(np.int32) + cv2.polylines(img, [points], False, point_color, 1) for j in range(len(history)-1): - a = history[j] + # a = history[j] b = history[j+1] - cv2.line(img, to_point(a), to_point(b), point_color, 1) + # cv2.line(img, to_point(a), to_point(b), point_color, 1) cv2.circle(img, to_point(b), 3, point_color, 2)