I have a simple code that reads video stream from youtube for processing with OpenCV. I want to display these video frames with st.empty(). On localhost it works fine, but when I run same code in Google Colab, only first frame is shown and then the video freezes. Is there some workaround? Thanks.
Here is my code
class ThreadedCamera(object):
def __init__(self, src=0):
self.capture = cv2.VideoCapture(src)
self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 2)
# FPS = 1/X
# X = desired FPS
self.FPS = 1/30
self.FPS_MS = int(self.FPS * 1000)
# Start frame retrieval thread
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
time.sleep(self.FPS)
def show_frame(self):
cv2.imshow('frame', self.frame)
cv2.waitKey(self.FPS_MS)
if __name__ == '__main__':
tmpFile = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
src = 'https://manifest.googlevideo.com/api/manifest/hls_playlist/expire/1643753319/ei/B1v5YdTDH4mK1gKp36I4/ip/5.104.23.198/id/0Ua8_c0Nphg.4/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D136/hls_chunk_host/rr2---sn-2gb7sn7y.googlevideo.com/playlist_duration/30/manifest_duration/30/vprv/1/playlist_type/DVR/initcwndbps/6260/mh/-O/mm/44/mn/sn-2gb7sn7y/ms/lva/mv/m/mvi/2/pl/22/dover/11/keepalive/yes/fexp/24001373,24007246/mt/1643731472/sparams/expire,ei,ip,id,itag,source,requiressl,ratebypass,live,sgoap,sgovp,playlist_duration,manifest_duration,vprv,playlist_type/sig/AOq0QJ8wRgIhAKpvu9nZsd2wzn_Qb-HVrkAWpZz1J5rLCsF4tXuFPTi5AiEAyLhxBxab8L81rbUcv6dXRQg86_ioTauBV_NyaUtqMb0%3D/lsparams/hls_chunk_host,initcwndbps,mh,mm,mn,ms,mv,mvi,pl/lsig/AG3C_xAwRQIgO9vurnIVlHrNAjwmcN9lQlj2gGCu3RD9Y2MYIk7tpwsCIQD6h34nm7YZXL-Nm45qVFYoK7h02p9RR20pQ5G34SML1g%3D%3D/playlist/index.m3u8'
threaded_camera = ThreadedCamera(src)
st.title("Real Time Face Emotion Detection Application")
st.header("Webcam Live Feed")
image_place = st.empty()
while True:
try:
image_place.image(threaded_camera.frame)
#window.image = threaded_camera.frame
except AttributeError:
pass