-
Notifications
You must be signed in to change notification settings - Fork 6
/
FPS.py
63 lines (53 loc) · 1.52 KB
/
FPS.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
import time
import cv2
class FPS:
"""
Helps in finding Frames Per Second and display on an OpenCV Image
"""
def __init__(self):
self.pTime = time.time()
def update(self, img=None, pos=(10, 40), color=(255, 0, 0), scale=2, thickness=2):
"""
Update the frame rate
:param img: Image to display on, can be left blank if only fps value required
:param pos: Position on the FPS on the image
:param color: Color of the FPS Value displayed
:param scale: Scale of the FPS Value displayed
:param thickness: Thickness of the FPS Value displayed
:return:
"""
cTime = time.time()
try:
fps = 1 / (cTime - self.pTime)
self.pTime = cTime
if img is None:
return fps
else:
cv2.putText(img, f'FPS: {int(fps)}', pos, cv2.FONT_HERSHEY_PLAIN,
scale, color, thickness)
return fps, img
except:
return 0
def main():
"""
Without Webcam
"""
fpsReader = FPS()
while True:
time.sleep(0.025) # add delay to get 40 Frames per second
fps = fpsReader.update()
print(fps)
def mainWebcam():
"""
With Webcam
"""
fpsReader = FPS()
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
fps, img = fpsReader.update(img)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
# main()
mainWebcam()