forked from astra-robotics/IRC2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver2.py
58 lines (45 loc) · 1.64 KB
/
server2.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
#!/usr/bin/env python
# coding: utf-8
import socket
import cv2
import sys
import argparse
import video_grabber2
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, help='The port on which the server is listening', required=True)
parser.add_argument('--jpeg_quality', type=int, help='The JPEG quality for compressing the reply', default=50)
parser.add_argument('--encoder', type=str, choices=['cv2','turbo'], help='Which library to use to encode/decode in JPEG the images', default='cv2')
args = parser.parse_args()
jpeg_quality = args.jpeg_quality
host = ''
port = args.port
encoder = args.encoder
# The grabber of the webcam
grabber = video_grabber2.VideoGrabber(jpeg_quality, encoder)
grabber.start()
get_message = lambda: grabber.get_buffer()
keep_running = True
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = (host, port)
print('starting up on %s port %s\n' % server_address)
sock.bind(server_address)
while(keep_running):
data, address = sock.recvfrom(4)
data = data.decode('utf-8')
if(data == "get"):
buffer = get_message()
if buffer is None:
continue
if len(buffer) > 65507:
print("The message is too large to be sent within a single UDP datagram. We do not handle splitting the message in multiple datagrams")
sock.sendto("FAIL".encode('utf-8'),address)
continue
# We send back the buffer to the client
sock.sendto(buffer, address)
elif(data == "quit"):
grabber.stop()
keep_running = False
print("Quitting..")
grabber.join()
sock.close()