Python send opencv img with socket.io to Nodejs

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? maybe incompatibility with does libraries or im doing something wrong?

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 error:

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.



Read more here: https://stackoverflow.com/questions/66270432/python-send-opencv-img-with-socket-io-to-nodejs

Content Attribution

This content was originally published by Siewdass Sf at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: