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.

Siewdass Sf
  • 169
  • 1
  • 10
  • this could help : https://stackoverflow.com/questions/40928205/python-opencv-image-to-byte-string-for-json-transfer – Loïc Feb 19 '21 at 01:46

1 Answers1

0

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

socket.emit( 'img', encoded.decode( 'utf-8' ) )
Siewdass Sf
  • 169
  • 1
  • 10