我正在努力解决将cv2视频流(网络摄像头)发送到服务器(稍后将用于面部识别)的问题。
我一直得到以下错误的服务器:
Traceback (most recent call last):
File "server.py", line 67, in <module>
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'resize'
> Overload resolution failed:
> - src data type = 18 is not supported
> - Expected Ptr<cv::UMat> for argument 'src'
服务器是这样的:
if client_socket:
while True:
packet = client_socket.recv(4 * 1024)
frame = np.array(packet)
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
客户端:
while True:
vid = cv2.VideoCapture(0)
while vid.isOpened():
time.sleep(0.5)
img, frame = vid.read()
frame = imutils.resize(frame, 4 * 1024)
a = pickle.dumps(frame)
message = struct.pack("Q", len(a)) + a
try:
client_socket.sendall(message)
except Exception as e:
print(e)
raise Exception(e)
有人知道吗?
因为它是一个字节数据流,我试图包括例如睡眠功能,以允许更多的处理。也试图隔离客户端图片,但也得到错误。
您的代码有几个问题。关于您的问题:您正在将字节字符串传递给resize
:
import cv2
import numpy as np
cv2.resize(np.array(b"Hello"), (0, 0), fx=0.25, fy=0.25)
输出:
cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'resize'
> Overload resolution failed:
> - src data type = 18 is not supported
> - Expected Ptr<cv::UMat> for argument 'src'
请考虑:
- 不使用pickle处理这类任务,而是在发送之前将数据压缩为png或类似的格式
- 在发送数据之前传递数据长度,并在服务器端使用它在解压缩之前收集完整的数据