OPENCV通过TCP发送帧Python,Pickle无法找到标记

时间:2018-06-02 10:54:24

标签: python sockets opencv pickle

我正在尝试使用python over tcp socket从opencv发送和接收帧。 我想出了connection.recv(bytes)的大小,使用sys.getsizeof()函数来查看发送结束字节的大小,然后在服务器代码中使用它。 当运行此代码时,我收到一些帧5到10,然后我在服务器端得到错误:

_pickle.UnpicklingError: could not find MARK

并且客户端代码冻结并且什么都不做。

服务器代码: -

#server
import threading
import cv2
import socket
import time
import pickle
import sys
import numpy as np
class Receive(threading.Thread):
    def __init__(self,RX_PORT):
        threading.Thread.__init__(self)
        self.daemon = True
        self.rx_port = RX_PORT
        self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.s.bind(('',self.rx_port))
    def run(self):
        self.s.listen(1)
        self.c, self.addr = self.s.accept()
        print('[+] Incoming Connection From '+str(self.addr))
        while True:
            self.data = self.c.recv(921781)
            self.frame = pickle.loads(self.data)
            cv2.imshow('RECV',self.frame)
    def __del__(self):
        self.s.close()
        cv2.destroyAllWindows()
        self.is_alive = False

RX = Receive(5550)
RX.start()
time.sleep(60)

客户代码: -

#client code
import numpy as np
import cv2
import socket
import pickle
import sys

cap = cv2.VideoCapture(0)

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',5550))

while(True):
    ret, frame = cap.read()
    data = pickle.dumps(frame)
    s.sendall(data)
    print(sys.getsizeof(data))
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

2 个答案:

答案 0 :(得分:0)

不知道,我还试图找出通过Kafka发送的泡菜数据。

也许这是您的解决方案:

ret, frame = cap.read()
ret, jpeg = cv2.imencode('.jpeg', frame)   #Encode the frame into JPEG
data = pickle.dumps(jpeg)
s.sendall(data)

答案 1 :(得分:0)

好。我解决了。 也许是socket.recv(size)有问题

答案位于此处的代码:https://github.com/HarshVaragiya/Chat-Application/tree/master/VideoChat