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