0

Im testing with this piece of code to send opencv img from python to nodejs with socket.io but its not working can anyone help me with?

Python

from socketio import Client
from cv2 import imread, imencode
import base64

socket = Client( )

@socket.on( 'connect' )
def connect( ):
    print( 'connected to server' )
    img = imread( 'screenshot.png' )
    frame = imencode( '.png', img )[ 1 ]
    encoded = base64.b64encode( frame ) 
    socket.emit( 'img', encoded )

@socket.on( 'disconnect' )
def disconnect( ):
    print( 'disconnected from server' )

socket.connect( 'http://127.0.0.1:3000' )
socket.wait( )

Nodejs

const express = require( 'express' )
const app = express( )
const http = require( 'http' ).Server( app )
const socket = require( 'socket.io' )( http )
const path = require( 'path' )
const fs = require( 'fs' )
const cv = require( 'opencv4nodejs' );

app.get( '/', ( req, res ) => {
    res.sendFile( path.join( __dirname + '/index.html' ) )
} )

socket.on( 'connect', ( player ) => {
    console.log( 'Connected: '+ player.id )

    player.on( 'img', ( encoded ) => {
        var buffer = Buffer.from( encoded, 'base64' )
        var image = cv.imdecode( buffer, cv.IMREAD_COLOR )
        cv.imwrite( 'img.png', image )
    } )

    player.on( 'disconnect', ( player ) => {
        console.log( 'Disconnected: '+ player.id )
    } )
} )

http.listen( 3000, ( ) => {
    console.log( 'listening on *:3000' )
} )

Im getting this warnings/error in nodejs:

libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data

Sorry for me bad english, and thanks by the way.

1

1 Answer 1

1

I deduced it in another way.. it should decode with utf-8 the base64 string.

socket.emit( 'img', encoded.decode( 'utf-8' ) )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.