樹莓派用Python寫幾個簡單程式4:socket的使用__Python

來源:互聯網
上載者:User

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

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

首先是伺服器端:

import socketimport cv2import numpy<pre name="code" class="python">address = ('local ip',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) cv2.waitKey(10) s.close()cv2.destroyAllWindows()
 

然後是用戶端:

import socketimport cv2import numpy <pre name="code" class="python">capture = cv2.VideoCapture(0)ret, frame = capture.read()
address = ('local ip',8002)sock = socket.socket()sock.connect(address)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) cv2.waitKey(10)sock.close()cv2.destroyAllWindows()
 出現下面這個問題, 

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]
要確認 local ip是伺服器端的ip,

ip在window的cmd中可以ipconfig查看;在linux中可以ifconfig查看。




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.