樹莓派用Python寫幾個簡單程式5:用socket傳映像

來源:互聯網
上載者:User

標籤:

使用socket 傳遞網路攝影機映像到pc。

確定安裝好opencv和python後,確定自己作為伺服器端裝置ip:

首先是伺服器端 server.py:

import socketimport cv2import numpyaddress = (‘127.0.0.1‘, 8002)s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.bind(address)s.listen(True)def recvall(sock, count):    buf = b‘‘    while count:        newbuf = sock.recv(count)        if not newbuf: return None        buf += newbuf        count -= len(newbuf)    return bufconn, addr = s.accept()while 1:    length = recvall(conn,16)    stringData = recvall(conn, int(length))    data = numpy.fromstring(stringData, dtype=‘uint8‘)    decimg=cv2.imdecode(data,1)    cv2.imshow(‘SERVER‘,decimg)    if cv2.waitKey(10) == 27:        break    s.close()cv2.destroyAllWindows()

然後是用戶端 client.py:

import socketimport cv2import numpyaddress = (‘127.0.0.1‘, 8002)sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)sock.connect(address)capture = cv2.VideoCapture(0)ret, frame = capture.read()encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),90]while ret:    result, imgencode = cv2.imencode(‘.jpg‘, frame, encode_param)    data = numpy.array(imgencode)    stringData = data.tostring()    sock.send( str(len(stringData)).ljust(16));    sock.send( stringData );    ret, frame = capture.read()    #decimg=cv2.imdecode(data,1)    #cv2.imshow(‘CLIENT‘,decimg)    if cv2.waitKey(10) == 27:        breaksock.close()cv2.destroyAllWindows()

調試過程出現下面這個問題,一般是ip不對,要把‘127.0.0.1‘改為伺服器端的ip,

Traceback (most recent call last):
  File "client.py", line 12, in <module>
    sock.connect((TCP_IP, TCP_PORT))
  File "C:\Python27\lib\socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 10061]

若在window的cmd中可以ipconfig查看ip;

若在linux中可以ifconfig查看ip。


樹莓派用Python寫幾個簡單程式5:用socket傳映像

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.