我正在尝试通过套接字发送OpenCV的帧,如下面的代码。 问题是视频最终变得怪异,分成多行或多列。
(Python 2.7)
ob:这段代码不是我的。
来源:http://answers.opencv.org/question/69123/sending-opencv-frames-through-udp-socket-python/
客户端:
import socket
import numpy as np
import cv2
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
cap = cv2.VideoCapture(0)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while 1:
ret, frame = cap.read()
cv2.imshow('frame', frame)
d = frame.flatten()
s = d.tostring()
for i in range(20):
sock.sendto(s[i*46080:(i+1)*46080], (UDP_IP, UDP_PORT))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
服务器:
import socket
import numpy as np
import cv2
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
s = ""
while True:
data, addr = sock.recvfrom(46080)
s += data
if len(s) == (46080*20):
frame = np.fromstring(s, dtype='uint8')
frame = frame.reshape(480, 640, 3)
cv2.imshow('frame', frame)
s = ""
if cv2.waitKey(1) & 0xFF == ord('q'):
break