Spaces:
Runtime error
Runtime error
decouple inference from streaming
Browse files
app.py
CHANGED
|
@@ -122,18 +122,49 @@ RTC_CONFIG = RTCConfiguration({
|
|
| 122 |
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
|
| 123 |
})
|
| 124 |
|
|
|
|
|
|
|
| 125 |
class CaptionProcessor(VideoProcessorBase):
|
| 126 |
def __init__(self):
|
| 127 |
self.interval = 1.0
|
| 128 |
self.last_time = time.time()
|
| 129 |
self.caption = ""
|
|
|
|
|
|
|
| 130 |
|
| 131 |
def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
|
| 132 |
img = frame.to_ndarray(format="bgr24")
|
| 133 |
now = time.time()
|
|
|
|
|
|
|
| 134 |
if now - self.last_time >= self.interval:
|
| 135 |
self.last_time = now
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
| 138 |
|
| 139 |
ctx = webrtc_streamer(
|
|
|
|
| 122 |
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
|
| 123 |
})
|
| 124 |
|
| 125 |
+
import concurrent.futures
|
| 126 |
+
|
| 127 |
class CaptionProcessor(VideoProcessorBase):
|
| 128 |
def __init__(self):
|
| 129 |
self.interval = 1.0
|
| 130 |
self.last_time = time.time()
|
| 131 |
self.caption = ""
|
| 132 |
+
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
| 133 |
+
self.future = None
|
| 134 |
|
| 135 |
def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
|
| 136 |
img = frame.to_ndarray(format="bgr24")
|
| 137 |
now = time.time()
|
| 138 |
+
|
| 139 |
+
# 1) Schedule a new inference if interval has passed and previous is done
|
| 140 |
if now - self.last_time >= self.interval:
|
| 141 |
self.last_time = now
|
| 142 |
+
# only submit if there isn't already a running task
|
| 143 |
+
if self.future is None or self.future.done():
|
| 144 |
+
# copy the frame so that downstream modifying code can't clash
|
| 145 |
+
img_copy = img.copy()
|
| 146 |
+
self.future = self.executor.submit(caption_frame, img_copy)
|
| 147 |
+
|
| 148 |
+
# 2) If the background task finished, grab its result
|
| 149 |
+
if self.future and self.future.done():
|
| 150 |
+
try:
|
| 151 |
+
self.caption = self.future.result()
|
| 152 |
+
except Exception as e:
|
| 153 |
+
self.caption = f"[error: {e}]"
|
| 154 |
+
self.future = None
|
| 155 |
+
|
| 156 |
+
# 3) Draw the **last** caption onto every frame immediately
|
| 157 |
+
cv2.putText(
|
| 158 |
+
img,
|
| 159 |
+
self.caption or "_…thinking…_",
|
| 160 |
+
org=(10, img.shape[0] - 20),
|
| 161 |
+
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
|
| 162 |
+
fontScale=0.6,
|
| 163 |
+
color=(255, 255, 255),
|
| 164 |
+
thickness=2,
|
| 165 |
+
lineType=cv2.LINE_AA,
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
| 169 |
|
| 170 |
ctx = webrtc_streamer(
|