简体   繁体   English

Python 将 opencv img 与 socket.io 发送到 Nodejs

[英]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?我正在使用这段代码进行测试,以将 opencv img 从 python 发送到带有 socket.io 的 nodejs,但它不工作有人可以帮我吗?

Python 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:我在 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.对不起,我的英语不好,顺便谢谢你。

I deduced it in another way.. it should decode with utf-8 the base64 string.我以另一种方式推断它..它应该用 utf-8 解码 base64 字符串。

socket.emit( 'img', encoded.decode( 'utf-8' ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM