-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
104 lines (87 loc) · 2.9 KB
/
client.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# @Author:langyi
# @Time :2019/4/12
import socket
import os
import time
import json
import struct
import threading
import random
import argparse
# Super parameters
parser = argparse.ArgumentParser(description=" ")
parser.add_argument('--frame_num', default=16, type=int)
args = parser.parse_args()
# Connect server by TCP
def sock_connect(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
return s
# Send image thread
class SendThread(threading.Thread):
def __init__(self, sock):
threading.Thread.__init__(self)
self.sock = sock
def run(self):
while True:
# Image path
dir_path = 'driver_p002_c7'
n_frames = len(os.listdir(dir_path))
# Choose sequence frame number randomly
begin_idx = random.randint(0, n_frames - args.frame_num)
frame_indices = list(range(begin_idx, begin_idx + args.frame_num))
for i in frame_indices:
image_path = os.path.join(dir_path, 'image_{:05d}.jpg'.format(i))
if os.path.exists(image_path):
self.send_image(image_path)
else:
raise ValueError('index error')
# Send a piece of image by its path
def send_image(self, image_path):
timestamp = time.time()
'''
File head:
Head size: '24si' (s means bytes in python or char[] in C,24s is 24 bytes;
i means int, 4 bytes;
total 28 bytes)
Head content: timestamp, file size
'''
file_head = struct.pack('24si', bytes(str(timestamp), encoding='utf-8'), os.stat(image_path).st_size)
self.sock.send(file_head)
# Read image and send it
with open(image_path, 'rb') as f:
while True:
data = f.read(1024)
if not data:
# print('{} send over !'.format(image_path))
break
self.sock.send(data)
# Receive result thread
class RecvThread(threading.Thread):
def __init__(self, sock):
threading.Thread.__init__(self)
self.sock = sock
def run(self):
while True:
result = self.sock.recv(1024).strip().decode('utf-8')
if len(result) > 0:
if is_json(result):
# If result is json type, loads it
result = json.loads(result)
print(result)
else:
print(result)
# Judge received data is json format or not
def is_json(data):
try:
json.loads(data)
except:
return False
return True
if __name__ == '__main__':
HOST, PORT = 'localhost', 6666
sk = sock_connect(HOST, PORT)
send_thread = SendThread(sk)
send_thread.start()
recv_thread = RecvThread(sk)
recv_thread.start()