Skip to main content
 首页 » 编程设计

python之从网络摄像头捕获图像并将其上传到 Flask 服务器

2024年09月07日9wayfarer

我有一个网页可以使用 webcamjs 从网络摄像头捕捉图像.我在后端有一个 flask 服务器,它应该接受图像和另一个表单数据。我无法使用 XMLHttpRequest 将图像发送到 Flask 服务器。

signup.html

    <form method="POST" enctype="multipart/form-data" id="myForm"> 
        <table> 
            <tr> 
                <td>Name/EmailId</td> 
                <td>:  <input type="text" name="userID"></td> 
            </tr> 
            <tr> 
                <td><input type="button" value="Upload" onclick="upload()"></td> 
            </tr> 
        </table> 
    </form> 
    <div id="my_camera"></div> 
    <input type="button" onclick="snap()" value="Snap"> 
    <div id="results"></div> 

JavaScript

function ShowCam() { 
    Webcam.set({ 
        width: 320, 
        height: 240, 
        image_format: 'jpeg', 
        jpeg_quality: 100 
    }); 
    Webcam.attach('#my_camera'); 
} 
window.onload= ShowCam; 
 
function snap() { 
    Webcam.snap( function(data_uri) { 
        // display results in page 
        document.getElementById('results').innerHTML =  
        '<img id="image" src="'+data_uri+'"/>'; 
      } );       
} 
 
function upload() { 
    console.log("Uploading...") 
    var image = document.getElementById('image').src; 
    var form = document.getElementById('myForm'); 
    var formData = new FormData(form); 
    formData.append("file", image); 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.open("POST", "/signup"); 
    xmlhttp.send(formData); 
    console.log(formData.get('file')); 
    console.log(formData.get('userID')); 
} 

正如您在上面的 javascript 代码中看到的,我有用于"file"和“用户 ID”的 console.log。这在 chrome 开发工具中正确显示,但数据不会发送到 Flask 服务器。我没有收到任何错误消息。

python

@app.route('/signup', methods=['GET','POST']) 
def signup(): 
    if request.method == 'POST': 
        return jsonify(request.form['userID'], request.form['file']) 
    return render_template('signup.html') 

我尝试通过简单地返回 return jsonify(request.form['userID']) 也没有用。

Python 调试

127.0.0.1 - - [07/May/2018 17:15:39] "GET /signup HTTP/1.1" 200 - 
127.0.0.1 - - [07/May/2018 17:15:39] "GET /static/webcamjs_min.js HTTP/1.1" 200 - 
127.0.0.1 - - [07/May/2018 17:15:39] "GET /static/webcam.js HTTP/1.1" 200 - 
127.0.0.1 - - [07/May/2018 17:15:57] "POST /signup HTTP/1.1" 200 - 

这是 Python 调试输出,您可以看到 POST 请求返回了一个 HTTP 响应代码 200 但 python 中的 return 语句没有输出。

请您参考如下方法:

您的代码按预期工作。您唯一错过的是在您的 javascript upload 函数中读取成功的 POST 请求。

function ShowCam() { 
    Webcam.set({ 
        width: 320, 
        height: 240, 
        image_format: 'jpeg', 
        jpeg_quality: 100 
    }); 
    Webcam.attach('#my_camera'); 
} 
window.onload= ShowCam; 
 
function snap() { 
    Webcam.snap( function(data_uri) { 
        // display results in page 
        document.getElementById('results').innerHTML =  
        '<img id="image" src="'+data_uri+'"/>'; 
      } );       
} 
 
function upload() { 
    console.log("Uploading...") 
    var image = document.getElementById('image').src; 
    var form = document.getElementById('myForm'); 
    var formData = new FormData(form); 
    formData.append("file", image); 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.open("POST", "/signup"); 
 
    // check when state changes,  
    xmlhttp.onreadystatechange = function() { 
 
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        alert(xmlhttp.responseText); 
        } 
    } 
 
    xmlhttp.send(formData); 
    console.log(formData.get('file')); 
    console.log(formData.get('userID')); 
}