问题
I am getting a stream from a source which I made so that it can be accessed at a particular websocket URL (Not sure if this ideal and would appreciate any other architectures as well). I need to now do object detection on this video stream, and thought of the architecture that I will connect to the websocket URL through a client websocket library like websocket in a server which is made through flask or fastapi, and then again stream the object detected video to multiple clients through another websocket URL.
The problem is I am unable to receive any images even after connecting to the websocket URL and am not sure how to handle asyncio in a server scenario as in where to put the line run_until_complete.
Any suggestions or help would be greatly appreciated
Server code
import uvicorn
from fastapi import FastAPI, WebSocket
# import websockets
# import asyncio
# init app
app = FastAPI()
async def clientwebsocketconnection():
uri = "wss://<websocket URL here>"
async with websockets.connect(uri) as websocket:
print("reached here")
data = await websocket.recv()
print(f"< {data}")
# Routes
@app.get('/')
async def index():
return {"text": "Its working"}
@app.websocket('/websocketconnection') # FIXME: change this to a websocket endpoint
async def websocketconnection():
return {"text": "Its working"}
if __name__ == '__main__':
uvicorn.run(app, host="127.0.0.1", port=8000)
# asyncio.get_event_loop().run_until_complete(clientwebsocketconnection())
回答1:
I assume you want to send a text via websocket. TO do this you need the following code:
@app.websocket('/websocketconnection')
async def websocketconnection(websocket: WebSocket) -> None:
await websocket.accept()
await websocket.send_text("It's working!")
await websocket.close()
You may find more examples in the official FastAPI docs.
来源:https://stackoverflow.com/questions/64328188/how-to-do-object-detection-on-a-video-stream-coming-from-a-websocket-url