File size: 1,640 Bytes
1e87f84
e94e369
1e87f84
 
b47bdbb
d7b7f52
b47bdbb
 
 
 
 
e94e369
 
b47bdbb
e94e369
db86fb5
6f53cc7
b47bdbb
 
 
 
 
 
 
 
 
 
 
 
 
 
6b89aad
b47bdbb
d7b7f52
 
 
 
 
 
 
 
 
 
 
 
ce95383
d7b7f52
 
6b89aad
 
 
 
 
b47bdbb
 
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
import time
from typing import Optional

import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont


class FrameRate:
    def __init__(self) -> None:
        self.c: int = 0
        self.start_time: Optional[float] = None
        self.NO_FRAMES = 10
        self.fps: float = -1
        self.label: str = ""
        self.font = ImageFont.truetype("fonts/arial.ttf", 50)
        self.reset()

    def reset(self) -> None:
        self.start_time = time.time()
        self.c = 0
        self.fps = -1

    def count(self) -> None:
        self.c += 1
        if self.c % self.NO_FRAMES == 0:
            self.c = 0
            end_time = time.time()
            self.fps = self.NO_FRAMES / (end_time - self.start_time)
            self.start_time = end_time

    def show_fps(self, image: np.ndarray, is_recording=False) -> np.ndarray:
        if self.fps != -1:
            text = f"FPS {self.fps:.0f} _ {self.label}"
            # image = cv2.putText(
            #     image,
            #     text,
            #     (50, 50),
            #     cv2.FONT_HERSHEY_SIMPLEX,
            #     fontScale=1,
            #     color=(255, 0, 0),
            #     thickness=2,
            # )
            pil_image = Image.fromarray(image)
            draw = ImageDraw.Draw(pil_image)
            draw.text((50, 50), text, font=self.font, fill=(0, 0, 204))
            image = np.asarray(pil_image)

            if is_recording:
                image = cv2.circle(
                    image, (50, 100), radius=10, color=(0, 0, 255), thickness=-1
                )
            return image
        else:
            return image