I am trying to serve frames generated through my webcam to a web app using NATS pub-sub. I am able to retrieve frames which I can confirm while putting a print "data" in the callback function(yield statement is commented).
But when I'm running it again with yield statement it doesn't show any frame yield and the web app page is blank.
Can you please help me? I'm very new to all these things.
import nest_asyncio
nest_asyncio.apply()
import cv2
import sys
import base64
import numpy as np
from flask import Flask, Response, render_template
from nats.aio.client import Client as NATS
from json import loads
from nats.aio.errors import ErrConnectionClosed, ErrTimeout, ErrNoServers
loop = asyncio.get_event_loop()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_feed', methods=['GET'])
def video_feed():
async def get_video_stream(loop):
nc = NATS()
await nc.connect(servers = ["nats://localhost:4222"],
connect_timeout=1, loop=loop)
async def message_handler(msg):
data = msg.data
print(type(data))
try:
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + data + b'\r\n')
except:
print("Yield not working")
sid = await nc.subscribe("video_stream", "workers", cb=message_handler)
await asyncio.sleep(5, loop=loop)
loop.run_until_complete(get_video_stream(loop))
return Response(mimetype = 'multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug = True)
print("1111")