-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsender.py
60 lines (51 loc) · 1.51 KB
/
sender.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
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
from __future__ import division
import cv2
import numpy as np
import socket
import struct
import math
class FrameSegment(object):
"""
Object to break down image frame segment
if the size of image exceed maximum datagram size
"""
MAX_DGRAM = 2**16
MAX_IMAGE_DGRAM = MAX_DGRAM - 64 # extract 64 bytes in case UDP frame overflown
def __init__(self, sock, port, addr="127.0.0.1"):
self.s = sock
self.port = port
self.addr = addr
def udp_frame(self, img):
"""
Compress image and Break down
into data segments
"""
compress_img = cv2.imencode('.jpg', img)[1]
dat = compress_img.tostring()
size = len(dat)
count = math.ceil(size/(self.MAX_IMAGE_DGRAM))
array_pos_start = 0
while count:
array_pos_end = min(size, array_pos_start + self.MAX_IMAGE_DGRAM)
self.s.sendto(struct.pack("B", count) +
dat[array_pos_start:array_pos_end],
(self.addr, self.port)
)
array_pos_start = array_pos_end
count -= 1
def main():
""" Top level main function """
# Set up UDP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 12345
fs = FrameSegment(s, port)
cap = cv2.VideoCapture(2)
while (cap.isOpened()):
_, frame = cap.read()
fs.udp_frame(frame)
cap.release()
cv2.destroyAllWindows()
s.close()
if __name__ == "__main__":
main()