Spaces:
Runtime error
Runtime error
thinh-huynh-re
commited on
Commit
•
b47bdbb
1
Parent(s):
2be4540
Add camera
Browse files- camera.py +46 -12
- requirements-lock.txt +92 -0
- requirements.txt +1 -0
- utils/__init__.py +0 -0
- utils/frame_rate.py +37 -0
camera.py
CHANGED
@@ -1,18 +1,52 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
img_file_buffer = st.camera_input("Take a picture")
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
11 |
|
12 |
-
# Check the type of cv2_img:
|
13 |
-
# Should output: <class 'numpy.ndarray'>
|
14 |
-
st.write(type(cv2_img))
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from streamlit_webrtc import webrtc_streamer
|
2 |
+
import numpy as np
|
3 |
import streamlit as st
|
4 |
+
|
5 |
import numpy as np
|
6 |
+
import av
|
7 |
+
import threading
|
8 |
+
|
9 |
+
from utils.frame_rate import FrameRate
|
10 |
+
|
11 |
+
lock = threading.Lock()
|
12 |
+
|
13 |
+
rtc_configuration = {
|
14 |
+
"iceServers": [
|
15 |
+
{
|
16 |
+
"urls": "turn:relay1.expressturn.com:3478",
|
17 |
+
"username": "efBRTY571ATWBRMP36",
|
18 |
+
"credential": "pGcX1BPH5fMmZJc5",
|
19 |
+
},
|
20 |
+
# {
|
21 |
+
# "urls": [
|
22 |
+
# "stun:stun1.l.google.com:19302",
|
23 |
+
# "stun:stun2.l.google.com:19302",
|
24 |
+
# "stun:stun3.l.google.com:19302",
|
25 |
+
# "stun:stun4.l.google.com:19302",
|
26 |
+
# ]
|
27 |
+
# },
|
28 |
+
],
|
29 |
+
}
|
30 |
+
|
31 |
+
|
32 |
+
class ImgContainer:
|
33 |
+
img: np.ndarray = None # raw image
|
34 |
+
frame_rate: FrameRate = FrameRate()
|
35 |
|
|
|
36 |
|
37 |
+
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
38 |
+
img = frame.to_ndarray(format="bgr24")
|
39 |
+
with lock:
|
40 |
+
img_container.img = img
|
41 |
+
img_container.frame_rate.count()
|
42 |
+
img = img_container.frame_rate.show_fps(img)
|
43 |
+
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
44 |
|
|
|
|
|
|
|
45 |
|
46 |
+
img_container = ImgContainer()
|
47 |
+
img_container.frame_rate.reset()
|
48 |
+
ctx = st.session_state.ctx = webrtc_streamer(
|
49 |
+
key="snapshot",
|
50 |
+
video_frame_callback=video_frame_callback,
|
51 |
+
rtc_configuration=rtc_configuration,
|
52 |
+
)
|
requirements-lock.txt
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aioice==0.7.6
|
2 |
+
aiortc==1.4.0
|
3 |
+
altair==4.2.2
|
4 |
+
attrs==22.2.0
|
5 |
+
av==10.0.0
|
6 |
+
black==23.1.0
|
7 |
+
blinker==1.5
|
8 |
+
cachetools==5.3.0
|
9 |
+
certifi==2022.12.7
|
10 |
+
cffi==1.15.1
|
11 |
+
charset-normalizer==3.1.0
|
12 |
+
click==8.1.3
|
13 |
+
cmake==3.26.0
|
14 |
+
cryptography==39.0.2
|
15 |
+
decorator==5.1.1
|
16 |
+
dnspython==2.3.0
|
17 |
+
entrypoints==0.4
|
18 |
+
filelock==3.10.0
|
19 |
+
gitdb==4.0.10
|
20 |
+
GitPython==3.1.31
|
21 |
+
google-crc32c==1.5.0
|
22 |
+
huggingface-hub==0.13.2
|
23 |
+
idna==3.4
|
24 |
+
importlib-metadata==6.0.0
|
25 |
+
Jinja2==3.1.2
|
26 |
+
jsonschema==4.17.3
|
27 |
+
lit==15.0.7
|
28 |
+
markdown-it-py==2.2.0
|
29 |
+
MarkupSafe==2.1.2
|
30 |
+
mdurl==0.1.2
|
31 |
+
mpmath==1.3.0
|
32 |
+
mypy-extensions==1.0.0
|
33 |
+
netifaces==0.11.0
|
34 |
+
networkx==3.0
|
35 |
+
numpy==1.24.2
|
36 |
+
nvidia-cublas-cu11==11.10.3.66
|
37 |
+
nvidia-cuda-cupti-cu11==11.7.101
|
38 |
+
nvidia-cuda-nvrtc-cu11==11.7.99
|
39 |
+
nvidia-cuda-runtime-cu11==11.7.99
|
40 |
+
nvidia-cudnn-cu11==8.5.0.96
|
41 |
+
nvidia-cufft-cu11==10.9.0.58
|
42 |
+
nvidia-curand-cu11==10.2.10.91
|
43 |
+
nvidia-cusolver-cu11==11.4.0.1
|
44 |
+
nvidia-cusparse-cu11==11.7.4.91
|
45 |
+
nvidia-nccl-cu11==2.14.3
|
46 |
+
nvidia-nvtx-cu11==11.7.91
|
47 |
+
opencv-python==4.7.0.72
|
48 |
+
opencv-python-headless==4.7.0.72
|
49 |
+
packaging==23.0
|
50 |
+
pandas==1.5.3
|
51 |
+
pathspec==0.11.1
|
52 |
+
Pillow==9.4.0
|
53 |
+
platformdirs==3.1.1
|
54 |
+
protobuf==3.20.3
|
55 |
+
pyarrow==11.0.0
|
56 |
+
pycparser==2.21
|
57 |
+
pydeck==0.8.0
|
58 |
+
pyee==9.0.4
|
59 |
+
Pygments==2.14.0
|
60 |
+
pylibsrtp==0.8.0
|
61 |
+
Pympler==1.0.1
|
62 |
+
pyOpenSSL==23.0.0
|
63 |
+
pyrsistent==0.19.3
|
64 |
+
python-dateutil==2.8.2
|
65 |
+
pytz==2022.7.1
|
66 |
+
pytz-deprecation-shim==0.1.0.post0
|
67 |
+
PyYAML==6.0
|
68 |
+
regex==2022.10.31
|
69 |
+
requests==2.28.2
|
70 |
+
rich==13.3.2
|
71 |
+
semver==2.13.0
|
72 |
+
six==1.16.0
|
73 |
+
smmap==5.0.0
|
74 |
+
streamlit==1.20.0
|
75 |
+
streamlit-webrtc==0.45.0
|
76 |
+
sympy==1.11.1
|
77 |
+
tokenizers==0.13.2
|
78 |
+
toml==0.10.2
|
79 |
+
tomli==2.0.1
|
80 |
+
toolz==0.12.0
|
81 |
+
torch==2.0.0
|
82 |
+
tornado==6.2
|
83 |
+
tqdm==4.65.0
|
84 |
+
transformers==4.27.1
|
85 |
+
triton==2.0.0
|
86 |
+
typing_extensions==4.5.0
|
87 |
+
tzdata==2022.7
|
88 |
+
tzlocal==4.2
|
89 |
+
urllib3==1.26.15
|
90 |
+
validators==0.20.0
|
91 |
+
watchdog==2.3.1
|
92 |
+
zipp==3.15.0
|
requirements.txt
CHANGED
@@ -5,3 +5,4 @@ torch
|
|
5 |
black
|
6 |
opencv-python
|
7 |
opencv-python-headless
|
|
|
|
5 |
black
|
6 |
opencv-python
|
7 |
opencv-python-headless
|
8 |
+
streamlit-webrtc
|
utils/__init__.py
ADDED
File without changes
|
utils/frame_rate.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import time, cv2
|
3 |
+
|
4 |
+
|
5 |
+
class FrameRate:
|
6 |
+
def __init__(self) -> None:
|
7 |
+
self.c: int = 0
|
8 |
+
self.start_time: float = None
|
9 |
+
self.NO_FRAMES = 100
|
10 |
+
self.fps: float = -1
|
11 |
+
|
12 |
+
def reset(self) -> None:
|
13 |
+
self.start_time = time.time()
|
14 |
+
self.c = 0
|
15 |
+
self.fps = -1
|
16 |
+
|
17 |
+
def count(self) -> None:
|
18 |
+
self.c += 1
|
19 |
+
if self.c % self.NO_FRAMES == 0:
|
20 |
+
self.c = 0
|
21 |
+
end_time = time.time()
|
22 |
+
self.fps = self.NO_FRAMES / (end_time - self.start_time)
|
23 |
+
self.start_time = end_time
|
24 |
+
|
25 |
+
def show_fps(self, image: np.ndarray) -> np.ndarray:
|
26 |
+
if self.fps != -1:
|
27 |
+
return cv2.putText(
|
28 |
+
image,
|
29 |
+
f"FPS {self.fps:.0f}",
|
30 |
+
(50, 50),
|
31 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
32 |
+
fontScale=1,
|
33 |
+
color=(255, 0, 0),
|
34 |
+
thickness=2,
|
35 |
+
)
|
36 |
+
else:
|
37 |
+
return image
|