javascript 将简单代码从python转换为nodeJs

zzlelutf  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(110)

如何帮助我将此代码转换为nodeJs或javascript/html代码:在此代码中,客户端正在向服务器发送视频,我想将其转换为JavaScript/HTML代码或NodeJS

# In this code client is sending video to server
import socket,cv2, pickle,struct
import pyshine as ps # pip install pyshine
import imutils # pip install imutils
camera = False
if camera == True:
    vid = cv2.VideoCapture(0)
else:
    vid = cv2.VideoCapture('videos/b.mp4')
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_ip = '192.168.114.2' # Here according to your server ip write the address

port = 9999
client_socket.connect((host_ip,port))

if client_socket: 
    while (vid.isOpened()):
        try:
            img, frame = vid.read()
            frame = imutils.resize(frame,width=380)
            a = pickle.dumps(frame)
            message = struct.pack("Q",len(a))+a
            client_socket.sendall(message)
            cv2.imshow(f"TO: {host_ip}",frame)
            key = cv2.waitKey(1) & 0xFF
            if key == ord("q"):
                client_socket.close()
        except:
            print('VIDEO FINISHED!')
            break
jtoj6r0c

jtoj6r0c1#

const net = require('net');
const cv = require('opencv4nodejs');
const struct = require('struct');

const clientSocket = new net.Socket();
const hostIp = '192.168.114.2'; // Here according to your server ip write the address
const port = 9999;

let camera = false;
let vid;
if (camera) {
vid = new cv.VideoCapture(0);
} else {
vid = new cv.VideoCapture('videos/b.mp4');
}

clientSocket.connect(port, hostIp, () => {
console.log('Connected to server');
});

clientSocket.on('error', (error) => {
console.error(`Error: ${error}`);
clientSocket.destroy();
});

clientSocket.on('close', () => {
console.log('Connection closed');
});

async function sendFrame() {
try {
    const frame = await vid.readAsync();
    const resizedFrame = frame.resize(380, 0);

    const buffer = cv.imencode('.jpg', resizedFrame).toString('binary');
    const message = struct.pack('Q', buffer.length) + buffer;
    clientSocket.write(message);

    cv.imshow('TO: ' + hostIp, resizedFrame);
    const key = cv.waitKey(1);
    if (key === 113) { // 113 is the ASCII code for 'q'
    clientSocket.end();
    }
    setImmediate(sendFrame);
} catch (error) {
    console.error(`Error: ${error}`);
    clientSocket.destroy();
}
}

sendFrame();

相关问题