-
Notifications
You must be signed in to change notification settings - Fork 6
/
streamlit_app.py
44 lines (31 loc) · 1.13 KB
/
streamlit_app.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
import streamlit as st
from PIL import Image
import torch
import numpy as np
from streamlit_webrtc import webrtc_streamer, WebRtcMode, RTCConfiguration
import av
device = 'cpu'
if not hasattr(st, 'classifier'):
st.model = torch.hub.load('ultralytics/yolov5', 'yolov5s', _verbose=False)
# st.model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.pt', _verbose=False)
RTC_CONFIGURATION = RTCConfiguration(
{"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
)
class VideoProcessor:
def recv(self, frame):
img = frame.to_ndarray(format="bgr24")
# vision processing
flipped = img[:, ::-1, :]
# model processing
im_pil = Image.fromarray(flipped)
results = st.model(im_pil, size=112)
bbox_img = np.array(results.render()[0])
return av.VideoFrame.from_ndarray(bbox_img, format="bgr24")
webrtc_ctx = webrtc_streamer(
key="WYH",
mode=WebRtcMode.SENDRECV,
rtc_configuration=RTC_CONFIGURATION,
video_processor_factory=VideoProcessor,
media_stream_constraints={"video": True, "audio": False},
async_processing=False,
)