Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer image continuously as video frame through Websocket with Base64Encode problem? #2071

Closed
HoangNK93 opened this issue Sep 18, 2020 · 6 comments

Comments

@HoangNK93
Copy link

Hi,

I create an application that get video from client, and process some analytics by open-cv in python server then response realtime as soon as possible to user.

So I take frame from client video (webcam), encode to base64 and send it to server via websocket (json format). It worked properly with normal video size, but when video size is bigger the client is disconnected.

How can I fix that? Should I try other solution?

Thank you

@HoangNK93 HoangNK93 added the question Question or problem label Sep 18, 2020
@mvoitko
Copy link

mvoitko commented Jan 6, 2021

Could you share how you are sending and reading the file?

@hoangnk-sunasterisk
Copy link

@mvoitko I encode image to base64 at front-end, then send it to server via web-socket.

        const data = canvas.toDataURL(type);
        const ws_data = {
            'frame': {
                'image': data.replace('data:' + type + ';base64,', ''), //split off junk,
                'id': frame
            },
        };
        ws.send(JSON.stringify(ws_data));

At server I decode image from this data:

    base64_data += "=" * ((4 - len(base64_data) % 4) % 4)
    im_bytes = base64.b64decode(base64_data)
    im_arr = np.frombuffer(im_bytes, dtype=np.uint8)  # im_arr is one-dim Numpy array
    img = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR)

@jiahuei
Copy link

jiahuei commented Mar 17, 2021

This is probably related to this issue:
Websocket disconnect happening when uploading a base64 file with size > 1MB

encode/uvicorn#432
encode/uvicorn#538

@satyajitghana
Copy link

satyajitghana commented Apr 28, 2021

i moved to hypercorn, instant fix :)

@tiangolo
Copy link
Member

tiangolo commented Dec 9, 2022

Thanks for the help here everyone! 👏 🙇

If that solves the original problem, then you can close this issue @HoangNK93 ✔️

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7304 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

6 participants