-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
81 lines (66 loc) · 1.94 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import random
import time
import cv2
import numpy as np
import torch
def seed_everything(**args):
"""Seed everything for better reproducibility.
NOTE that some pytorch operation is non-deterministic like the backprop of grid_samples
"""
seed = args.get("seed", 42)
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
def draw_red_point_bbox_based_on_center(image_np):
"""Draw a red point in the center of the image"""
height, width = image_np.shape[:2]
center = (
width // 2,
height // 2,
)
bbox = (
center[0] - width // 2,
center[1] - height // 2,
center[0] + width // 2,
center[1] + height // 2,
)
# cv2 to draw bbox
image_np = cv2.rectangle(
image_np, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 2
)
image_np[center[1], center[0]] = [255, 0, 0]
return image_np
def draw_center_bbox(image_np, center, bbox):
"""Draw a red point in the center of the image"""
image_np[center[0], center[1]] = [255, 0, 0]
# cv2 to draw bbox
image_np = cv2.rectangle(
image_np, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 2
)
return image_np
class VideoWriter:
def __init__(
self,
out_dir,
name,
):
if not os.path.exists(out_dir):
os.makedirs(out_dir)
self.name = name
timestamp = int(time.time())
self.fn = "{}_{}.mp4".format(
self.name,
timestamp,
)
self.out = os.path.join(out_dir, self.fn)
self.output_file = None
def __call__(self, frame):
if self.output_file is None:
height, width, _ = frame.shape
self.output_file = cv2.VideoWriter(
self.out, cv2.VideoWriter_fourcc(*"mp4v"), 20.0, (width, height)
)
self.output_file.write(frame)
def release(self):
self.output_file.release()