Thinh Huynh Nguyen Truong commited on
Commit
bd42292
·
1 Parent(s): 6691d61
Files changed (3) hide show
  1. README.md +2 -0
  2. app.py +75 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -10,3 +10,5 @@ pinned: false
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+ streamlit run app.py
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from streamlit_webrtc import webrtc_streamer
2
+ import numpy as np
3
+ import streamlit as st
4
+
5
+ import time
6
+ import cv2
7
+ import numpy as np
8
+ import av
9
+ import threading
10
+
11
+ lock = threading.Lock()
12
+
13
+ class FrameRate:
14
+ def __init__(self) -> None:
15
+ self.c: int = 0
16
+ self.start_time: float = None
17
+ self.NO_FRAMES = 100
18
+ self.fps: float = -1
19
+
20
+ def reset(self) -> None:
21
+ self.start_time = time.time()
22
+ self.c = 0
23
+ self.fps = -1
24
+
25
+ def count(self) -> None:
26
+ self.c += 1
27
+ if self.c % self.NO_FRAMES == 0:
28
+ self.c = 0
29
+ end_time = time.time()
30
+ self.fps = self.NO_FRAMES / (end_time - self.start_time)
31
+ self.start_time = end_time
32
+
33
+ def show_fps(self, image: np.ndarray) -> np.ndarray:
34
+ if self.fps != -1:
35
+ return cv2.putText(
36
+ image,
37
+ f'FPS {self.fps:.0f}',
38
+ (50, 50),
39
+ cv2.FONT_HERSHEY_SIMPLEX,
40
+ fontScale=1,
41
+ color=(255, 0, 0),
42
+ thickness=2
43
+ )
44
+ else:
45
+ return image
46
+
47
+
48
+ rtc_configuration = {
49
+ "iceServers": [{"urls": [
50
+ "stun:stun1.l.google.com:19302",
51
+ "stun:stun2.l.google.com:19302",
52
+ "stun:stun3.l.google.com:19302",
53
+ "stun:stun4.l.google.com:19302",
54
+ ]}],
55
+ }
56
+
57
+ class ImgContainer:
58
+ img: np.ndarray = None # raw image
59
+ frame_rate: FrameRate = FrameRate()
60
+
61
+ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
62
+ img = frame.to_ndarray(format="bgr24")
63
+ with lock:
64
+ img_container.img = img
65
+ img_container.frame_rate.count()
66
+ img = img_container.frame_rate.show_fps(img)
67
+ return av.VideoFrame.from_ndarray(img, format="bgr24")
68
+
69
+ img_container = ImgContainer()
70
+ img_container.frame_rate.reset()
71
+ ctx = st.session_state.ctx = webrtc_streamer(
72
+ key="snapshot",
73
+ video_frame_callback=video_frame_callback,
74
+ rtc_configuration=rtc_configuration
75
+ )
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ opencv-python
2
+ streamlit-webrtc