diff --git a/receive.py b/receive.py new file mode 100644 index 0000000..5f066f0 --- /dev/null +++ b/receive.py @@ -0,0 +1,37 @@ +import socket +import numpy as np +import cv2 as cv + + +addr = ("192.168.0.195", 1111) +buf = 512 +width = 640 +height = 480 +code = b'start' +num_of_chunks = width * height * 3 / buf + +if __name__ == '__main__': + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.bind(addr) + print("Start") + while True: + chunks = [] + start = False + while len(chunks) < num_of_chunks: + chunk, _ = s.recvfrom(buf) + if start: + chunks.append(chunk) + elif chunk.startswith(code): + start = True + + byte_frame = b''.join(chunks) + + frame = np.frombuffer( + byte_frame, dtype=np.uint8).reshape(height, width, 3) + + cv.imshow('recv', frame) + if cv.waitKey(1) & 0xFF == ord('q'): + break + + s.close() + cv.destroyAllWindows() diff --git a/send.py b/send.py new file mode 100644 index 0000000..13bed5e --- /dev/null +++ b/send.py @@ -0,0 +1,35 @@ +import socket +import numpy as np +import cv2 as cv + + +addr = ('192.168.0.195', 1111) +print(socket.gethostname()) +buf = 512 +width = 640 +height = 480 +cap = cv.VideoCapture(0) +cap.set(3, width) +cap.set(4, height) +code = 'start' +code = ('start' + (buf - len(code)) * 'a').encode('utf-8') + + +if __name__ == '__main__': + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + print("cap.isOpened") + while(cap.isOpened()): + ret, frame = cap.read() + if ret: + s.sendto(code, addr) + data = frame.tostring() + for i in range(0, len(data), buf): + s.sendto(data[i:i+buf], addr) + # cv.imshow('send', frame) + # if cv.waitKey(1) & 0xFF == ord('q'): + # break + else: + break + # s.close() + # cap.release() + # cv.destroyAllWindows()