-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
50 lines (39 loc) · 1.16 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
import sys
import cv2 as cv
from constants import CAM_WIDTH, CAM_HEIGHT
from constants import font, fontScale, fontColor, lineType
def setup_capture():
"""Initializes video capture (camera) device"""
cap = cv.VideoCapture(0)
cap.set(cv.CAP_PROP_FRAME_WIDTH, CAM_WIDTH)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, CAM_HEIGHT)
if cap.isOpened():
return cap
print("Cannot open camera")
sys.exit()
class Timer:
"""Used to track fps and frame number"""
def __init__(self, decay=0.9):
self.decay = decay
self.last_time = cv.getTickCount()
self.fps = 30
self.frame = 0
def tick(self):
"""Returns new fps"""
self.frame += 1
current_time = cv.getTickCount()
current_fps = cv.getTickFrequency() / (current_time - self.last_time)
self.last_time = current_time
self.fps = self.decay * self.fps + (1 - self.decay) * current_fps
return self.fps
def add_text(img, text, position):
"""position - bottomLeftCornerOfText"""
cv.putText(
img,
text,
position,
font,
fontScale,
fontColor,
lineType
)