import cv2 from vidocp.utils import copy_and_normalize_channels def draw_contours(image, contours): image = copy_and_normalize_channels(image) for cont in contours: cv2.drawContours(image, cont, -1, (0, 255, 0), 4) return image def draw_rectangles(image, rectangles, color=None, annotate=False): def annotate_rect(x, y, w, h): cv2.putText(image, "+", (x + (w // 2) - 12, y + (h // 2) + 9), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) image = copy_and_normalize_channels(image) if not color: color = (0, 255, 0) for rect in rectangles: x, y, w, h = rect cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) if annotate: annotate_rect(x, y, w, h) return image def draw_stats(image, stats, annotate=False): keys = ["x", "y", "w", "h"] def annotate_stat(x, y, w, h): for i, (s, v) in enumerate(zip(keys, [x, y, w, h])): anno = f"{s} = {v}" xann = int(x + 5) yann = int(y + h - (20 * (i + 1))) cv2.putText(image, anno, (xann, yann), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) def draw_stat(stat): x, y, w, h, area = stat cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) if annotate: annotate_stat(x, y, w, h) image = copy_and_normalize_channels(image) for stat in stats[2:]: draw_stat(stat) return image