在Python 3中通过套接字的OpenCV直播视频

时间:2023-01-27 23:40:02

I am trying to create a simple application to send live stream video over the socket in Python 3 with OpenCV. I am new to OpenCV and socket programming so if you can provide answer in detail I will be very grateful. Thank you.

我正在尝试创建一个简单的应用程序,通过OpenCV在Python 3中通过套接字发送实时流视频。我是OpenCV和socket编程的新手,所以如果你能详细提供答案,我将非常感激。谢谢。

Here is sender.py

这是sender.py

import socket
import time

import cv2

capture = cv2.VideoCapture(0)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.1.10', 50505))

while True:
    ret, frame = capture.read()
    data = cv2.imencode('.jpg', frame)[1].tostring()
    sock.sendall(data)
    time.sleep(2)

Here is receiver.py

这是receiver.py

import socket
import cv2
import numpy as np
import time

HOST = '192.168.1.10'
PORT = 50505

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')

s.bind((HOST, PORT))
print('Socket bind complete')

s.listen(10)
print('Socket now listening')

conn, addr = s.accept()

while True:
    data = conn.recv(8192)
    nparr = np.fromstring(data, np.uint8)
    frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    cv2.imshow('frame', frame)
    time.sleep(2)

and this is the error

这是错误

 receiver.py", line 29, in <module>
        cv2.imshow('frame', frame)
    cv2.error: D:\Build\OpenCV\opencv-3.4.0\modules\highgui\src\window.cpp:339:
error: (-215) size.width>0 && size.height>0 in function cv::imshow

1 个解决方案

#1


0  

It is because you are receiving small amount of data, and image is not complete. 8192 bytes is not enough in 99.99% of the time, because every image is larger than 8Kb. You'll need to grab ALL data sent by sender in order to convert it to image.

这是因为您收到少量数据,而且图像不完整。在99.99%的时间内,8192个字节是不够的,因为每个图像都大于8Kb。您需要获取发件人发送的所有数据,以便将其转换为图像。

You can take a look at my code on github and change it acording to your need.

您可以在github上查看我的代码,并根据您的需要进行更改。

Long story short, easy option is to first send number of bytes to the client, and then send an image itself. In client code, after receiving length of image, loop until all bytes are received. for example:

长话短说,简单的选择是首先向客户端发送字节数,然后自己发送图像。在客户端代码中,在接收到图像长度后,循环直到接收到所有字节。例如:

...
img_len = 175428 # received by sender.py
e=0
data = ''
while e < img_len:
  d = sock.recv(1024)
  e += len(d)
  data += d

nparr = np.fromstring(data, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow('frame', frame)

#1


0  

It is because you are receiving small amount of data, and image is not complete. 8192 bytes is not enough in 99.99% of the time, because every image is larger than 8Kb. You'll need to grab ALL data sent by sender in order to convert it to image.

这是因为您收到少量数据,而且图像不完整。在99.99%的时间内,8192个字节是不够的,因为每个图像都大于8Kb。您需要获取发件人发送的所有数据,以便将其转换为图像。

You can take a look at my code on github and change it acording to your need.

您可以在github上查看我的代码,并根据您的需要进行更改。

Long story short, easy option is to first send number of bytes to the client, and then send an image itself. In client code, after receiving length of image, loop until all bytes are received. for example:

长话短说,简单的选择是首先向客户端发送字节数,然后自己发送图像。在客户端代码中,在接收到图像长度后,循环直到接收到所有字节。例如:

...
img_len = 175428 # received by sender.py
e=0
data = ''
while e < img_len:
  d = sock.recv(1024)
  e += len(d)
  data += d

nparr = np.fromstring(data, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow('frame', frame)