【问题标题】:Python send opencv img with socket.io to NodejsPython将带有socket.io的opencv图像发送到Node Js
【发布时间】:2021-02-19 01:10:04
【问题描述】:

我正在使用这段代码进行测试,以使用 socket.io 将 opencv img 从 python 发送到 nodejs,但它不起作用,谁能帮助我?

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( )

节点

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' )
} )

我在 nodejs 中收到此警告/错误:

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

对不起,我的英语不好,顺便谢谢。

【问题讨论】:

标签: node.js python-3.x opencv socket.io-1.0


【解决方案1】:

我用另一种方式推断它。它应该用 utf-8 解码 base64 字符串。

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

【讨论】:

    猜你喜欢
    • 2020-01-13
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2014-04-17
    • 2017-04-08
    相关资源
    最近更新 更多