forked from ancabilloni/udp_camera_streaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.py
47 lines (39 loc) · 1.07 KB
/
receiver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
from __future__ import division
import cv2
import numpy as np
import socket
import struct
MAX_DGRAM = 2**16
def dump_buffer(s):
""" Emptying buffer frame """
while True:
seg, addr = s.recvfrom(MAX_DGRAM)
print(seg[0])
if struct.unpack("B", seg[0:1])[0] == 1:
print("finish emptying buffer")
break
def main():
""" Getting image udp frame &
concate before decode and output image """
# Set up socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('127.0.0.1', 12345))
dat = b''
dump_buffer(s)
while True:
seg, addr = s.recvfrom(MAX_DGRAM)
if struct.unpack("B", seg[0:1])[0] > 1:
dat += seg[1:]
else:
dat += seg[1:]
img = cv2.imdecode(np.fromstring(dat, dtype=np.uint8), 1)
cv2.imshow('frame', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
dat = b''
# cap.release()
cv2.destroyAllWindows()
s.close()
if __name__ == "__main__":
main()