Raspberry Pi writes a few simple programs in Python 5: Send images with a socket

Source: Internet
Author: User

Use the socket to pass the camera image to the PC.

After you've installed OPENCV and Python, identify yourself as the server-side device IP:

The first is the server-side 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 (Ten)  ==  27:        break    s.close () Cv2.destroyallwindows ()

And then the client 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 (+))     sock.send ( stringData );     ret, frame = capture.read ()      #decimg =cv2.imdecode (data,1)      #cv2. Imshow (' CLIENT ', decimg)     if cv2.waitkey ()  ==  27:        breaksock.close () Cv2.destroyalLwindows () 

The following problem occurs in the debugging process, the general IP is wrong, to change the ' 127.0.0.1 ' to the server-side IP,

Traceback (most recent):
File "client.py", line A, 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]

If you can ipconfig view IP in the cmd of window;

If you can ifconfig view IP in Linux.


Raspberry Pi writes a few simple programs in Python 5: Send images with a socket

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.