-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.py
27 lines (21 loc) · 897 Bytes
/
counter.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
import cv2
import json
import numpy as np
from shapely.geometry import Polygon
class Counter:
counters = []
def __init__(self, file):
with open(file) as counter_file:
data = json.load(counter_file)
for counter in data["counters"]:
print(f"Parsing Counter ({counter['id']}): {counter['name']}")
self.counters.append(Polygon(np.array(counter["coordinates"], np.int32)))
def to_list(self):
return self.counters
def draw(self, frame, color):
counter_img = np.zeros_like(frame, np.uint8)
for counter in self.counters:
counter_img = cv2.fillPoly(counter_img, [np.array(counter.exterior.coords, dtype=np.int32)], color)
counter_mask = counter_img.astype(bool)
frame[counter_mask] = cv2.addWeighted(frame, 0.2, counter_img, 0.8, 0)[counter_mask]
return frame