简体   繁体   中英

Video streaming with Flask server running on VM

I am trying to access client's camera when running server on virtual machine (GCP). When I run Flask server on my local machine it works okay - asks for permission to access the camera but when I run Flask server on virtual machine it doesn't detect camera (I run <vm_ip_adress>:<port_number> in browser). Instead I have this information in logs:

[ WARN:3] global /tmp/pip-req-build-7m_g9lbm/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index .

code:
app.py

from flask import Flask, render_template, Response
import cv2

app = Flask(__name__)
camera = cv2.VideoCapture(0) 

def gen_frames():
    while True:
        success, frame = camera.read()
        if not success:
            break
        else:
            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


@app.route('/video_feed')
def video_feed():
    return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5002, debug=True)

My question: Is it possible to run Flask server in such way, detect user's camera and ask for permission to access it? Thanks for any advice.

The way you've opened the camera will only ever open the camera on the machine where the Flask app is running - so it worked on your local machine, but deployed to the server it is trying to find a camera and failing.

You will need to get your Flask app to serve up some browser code to open up the camera on the user side, and then send it to Flask as an HTTP request, at which point you can process the video. Take a look here for a starting-point , but bear in mind that streaming it to your Flask server in real-time might be quite challenging.

You may wish to look into WebRTC and Jitsi as further starting-points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM