Created
June 3, 2019 05:16
-
-
Save robobe/0385b6ce1eef5e6641a9355d1a3fcee5 to your computer and use it in GitHub Desktop.
OpenCV simple tcp server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import cv2 | |
import numpy as np | |
import struct | |
HOST = '127.0.0.1' | |
PORT = 5005 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((HOST, PORT)) | |
s.listen(10) | |
print('Socket now listening') | |
conn, addr = s.accept() | |
while True: | |
header = conn.recv( struct.calcsize("!I") ) | |
img_len = struct.unpack("!I", header)[0] | |
print (img_len) | |
data = '' | |
e=0 | |
while e < img_len: | |
d = conn.recv(1024) | |
e += len(d) | |
data += d | |
img = np.fromstring(data, dtype='uint8') | |
decimg=cv2.imdecode(img,1) | |
cv2.imshow('SERVER',decimg) | |
cv2.waitKey(1) | |
s.close() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment