Upload code
This commit is contained in:
parent
3108d15852
commit
062dcb4744
37
receive.py
Normal file
37
receive.py
Normal file
@ -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()
|
35
send.py
Normal file
35
send.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user