最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to prevent valuerror: too many packets in payload in flask socketio - Stack Overflow

programmeradmin1浏览0评论

so i'm doing my final project. i want to get head pose estimation from client webcam. i succesfully stream the webcam to server with websocket. but when i try to put my head pose function inside the socket route, the terminal show error valueError: too many packets in payload frequently. does anyone know how to prevent this error ? any answer would be appreciated. thank you !

here's my code for more information.

JavaScript

var constraints = { audio: false, video: { width: 500, height: 500 } }; 
var video = document.querySelector('video');
var canvas = document.querySelector('canvas');

var socket = io(':8000');

navigator.mediaDevices.getUserMedia(constraints)
    .then(function(mediaStream) {
    video.srcObject = mediaStream;
    video.onloadedmetadata = function(e) {
    video.play();

  };
})
.catch(function(err) { console.log(err.name + ": " + err.message); })

socket.on('connect', function () {
    console.log('connected !', socket.connected);

    function capture() {
        canvas.width = 200;
        canvas.height = 200;
        canvas.getContext('2d').drawImage(video, 0, 0, 200, 200);
        var data = canvas.toDataURL('image/jpeg');

        return data;
    };

    var FPS = 50

    setInterval(() => {
        var imgData = capture();

        socket.emit('image', imgData);

    }, 1000 / FPS);
});

flask app

@socketio.on('image')
def image(data_image):
    time.sleep(1)
    encoded_image = data_image.split(",")[1]
    decoded = base64.b64decode(encoded_image)
    frame = cv2.imdecode(np.frombuffer(decoded, np.uint8), -1)

    #this is my head pose module
    pose = FacePosition(frame)

    head_pose = pose.run()
    print(head_pose)

if __name__ == '__main__':
    socketio.run(app, threaded = True)

so i'm doing my final project. i want to get head pose estimation from client webcam. i succesfully stream the webcam to server with websocket. but when i try to put my head pose function inside the socket route, the terminal show error valueError: too many packets in payload frequently. does anyone know how to prevent this error ? any answer would be appreciated. thank you !

here's my code for more information.

JavaScript

var constraints = { audio: false, video: { width: 500, height: 500 } }; 
var video = document.querySelector('video');
var canvas = document.querySelector('canvas');

var socket = io('https://0.0.0.0:8000');

navigator.mediaDevices.getUserMedia(constraints)
    .then(function(mediaStream) {
    video.srcObject = mediaStream;
    video.onloadedmetadata = function(e) {
    video.play();

  };
})
.catch(function(err) { console.log(err.name + ": " + err.message); })

socket.on('connect', function () {
    console.log('connected !', socket.connected);

    function capture() {
        canvas.width = 200;
        canvas.height = 200;
        canvas.getContext('2d').drawImage(video, 0, 0, 200, 200);
        var data = canvas.toDataURL('image/jpeg');

        return data;
    };

    var FPS = 50

    setInterval(() => {
        var imgData = capture();

        socket.emit('image', imgData);

    }, 1000 / FPS);
});

flask app

@socketio.on('image')
def image(data_image):
    time.sleep(1)
    encoded_image = data_image.split(",")[1]
    decoded = base64.b64decode(encoded_image)
    frame = cv2.imdecode(np.frombuffer(decoded, np.uint8), -1)

    #this is my head pose module
    pose = FacePosition(frame)

    head_pose = pose.run()
    print(head_pose)

if __name__ == '__main__':
    socketio.run(app, threaded = True)

Share Improve this question asked May 3, 2020 at 17:52 Farhan RabbaaniiFarhan Rabbaanii 4631 gold badge9 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You may want to take a look at this github issue. Raising enigneio's max_decode_packets did solve that problem for me.

I followed ross933's answer and it worked for me. The actual bit of code that helped (in case the github issue disappears in the future) is below

from flask_socketio import SocketIO

from engineio.payload import Payload
Payload.max_decode_packets = 500
socketio = SocketIO(async_mode='gevent', ping_timeout=1000, ping_interval=0)
发布评论

评论列表(0)

  1. 暂无评论