【问题标题】:How to integrate deep learning algorithms and tcp transmission video services如何整合深度学习算法和tcp传输视频服务
【发布时间】:2020-03-07 20:38:23
【问题描述】:

如何将人工智能处理后的实时视频通过tcp协议传输到其他平台。 我将使用计算机作为服务器接收视频,树莓派作为客户端传输视频。

我用OpenCV在tcp协议中实现实时视频传输,有完整可行的对象识别码(来自https://github.com/PINTO0309/MobileNet-SSD-RealSense,感谢工程师PINTO0309)。我在电脑端接收视频(Windows系统)、对象识别和树莓派中的视频传输。

服务器


import socket,time,cv2,numpy

def ReceiveVideo():
    address = ('193.169.4.155', 50000)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(address)

    s.listen(1)

    def recvall(sock, count):
        buf = b''
        while count:
            newbuf = sock.recv(count)
            if not newbuf: return None
            buf += newbuf
            count -= len(newbuf)
        return buf

    conn, addr = s.accept()
    print('connect from:'+str(addr))
    while 1:
        start = time.time()
        length = recvall(conn,16)
        stringData = recvall(conn, int(length))
        data = numpy.frombuffer(stringData, numpy.uint8)
        decimg=cv2.imdecode(data,cv2.IMREAD_COLOR)
        cv2.imshow('SERVER',decimg)

        end = time.time()
        seconds = end - start
        fps  = 1/seconds;
        conn.send(bytes(str(int(fps)),encoding='utf-8'))
        k = cv2.waitKey(10)&0xff
        if k == 27:
            break
    s.close()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    ReceiveVideo()

客户



import socket,cv2,numpy,time,sys

def SendVideo():
    address = ('193.169.4.155', 50000)
    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.connect(address)

    global capture

    capture = cv2.VideoCapture(0)
    ret, frame = capture.read()
    encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),15]

    while ret:

        time.sleep(0.01)

        result, imgencode = cv2.imencode('.jpg', frame , encode_param)
        data = numpy.array(imgencode)
        stringData = data.tostring()

        sock.send(str.encode(str(len(stringData)).ljust(16)));

        sock.send(stringData);

        receive = sock.recv(1024)
        if len(receive):print(str(receive,encoding='utf-8'))

        ret, frame = capture.read()
        if cv2.waitKey(10) == 27:
            break
    sock.close()
if __name__ == '__main__':
    SendVideo()

对象识别码来自 https://github.com/PINTO0309/MobileNet-SSD-RealSense/blob/master/MultiStickSSDwithPiCamera_OpenVINO_NCS2.py

我多次修改代码后遇到如下问题。

  1. 传输后的显卡在第一帧。
  2. 无法传输,树莓派显示错误[Errno:32]Broken pipe,pc端显示错误。

OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

我认为第二个错误意味着没有得到图片。

我终于想实现将识别出来的视频实时传输到电脑了。

【问题讨论】:

    标签: python opencv tcp raspberry-pi openvino


    【解决方案1】:

    您从视频中捕获帧的逻辑是错误的:我在这里添加一些评论

    # you take one frame here
    ret, frame = capture.read()
    encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),15]
    
    # this will loop without taking any new frame
    while ret:
    
        time.sleep(0.01)
        # as result: at the first frame will be encoded successfully
        # but from second times, frame will be double/tripple encoded and became un-usable
        result, imgencode = cv2.imencode('.jpg', frame , encode_param)
        data = numpy.array(imgencode)
        stringData = data.tostring()
    

    如何解决:将你的代码改成这样的

    encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),15]
    
    while(True):
        ret,frame = cap.read()
        result, imgencode = cv2.imencode('.jpg', frame , encode_param)
        data = numpy.array(imgencode)
        stringData = data.tostring()
    

    【讨论】:

      猜你喜欢
      • 2020-02-07
      • 2020-06-16
      • 2021-01-18
      • 2021-09-22
      • 2017-03-05
      • 2018-11-05
      • 1970-01-01
      • 2017-04-24
      • 2015-06-24
      相关资源
      最近更新 更多