Skip to content
Advertisement

Stream OpenCV Video Capture to flask server

Greeting, I was working on a drone project, I wanted to take stream from my drone process it on my laptop and give a command based on processing, I was using the flask framework from the same.

Currently, as the first step I want to take the stream from drone and PUT it to the flask server and view it on the flask website, not doing the processing part right now.

I PUT the video to server after compressing it into jpg and using base 64 to encode it and then finally use json.dumps() and then requests.put() it.

On the server side in flask server program I get its using request.json, use json.loads(), but I am not clear what to do next.

I am not experienced enough with flask, web development and with limited experience and knowledge made the programs, but it returns error 405 on the flask program.

Here are the programs

flask server

JavaScript

index.html

JavaScript

drone program

JavaScript

Any help is highly appreciated!!!

Advertisement

Answer

You don’t have to convert to base64 and use JSON. it can be simpler and faster to send JPG directly as raw bytes

And to make it simpler I would use /upload to send image from drone to server, and /video to send image to users.

JavaScript

Now flask. This part is not complete.

It gets image from drone and keep in global variable. And when user open page then it loads single image from /video

JavaScript

At this moment it can display only one static image. It needs to send it as motion-jpeg


EDIT:

Version which sends motion-jpeg so you see video.

It works correctly with Chrome, Microsoft Edge and Brave (all use chrome engine).

Problem makes Firefox. It hangs and tries to load image all time. I don’t know what is the real problem but if I add time.sleep() then it can solve problem.

JavaScript

Server may runs users in separated threads on processes and sometimes it may not share frame between users. If I use use_reloader=False then I can stop sending to /upload and this stops video in browser, and later I can start again sending to /upload and browser again displays stream (without reloading page). Without use_reloader=False browser doesn’t restart video and it needs to reload page. Maybe it will need to use flask.g to keep frame. Or /upload will have to save frame in file or database and /video will have to read frame from file or database.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement