-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_client.py
More file actions
91 lines (68 loc) · 2.72 KB
/
server_client.py
File metadata and controls
91 lines (68 loc) · 2.72 KB
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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 1 10:28:28 2019
@author: ASUS
"""
import socket
import struct
import pickle
import zlib
import io
import sys
import time
import numpy as np
import cv2
def set_as_server():
""" This function going to take back processed camera feed from NVIDIA to ASUS"""
HOST = "" # NVIDIA'un IP adresi
PORT = 8089 # ASUS frameleri bu porttan alacak
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set communication based IP v4 and TCP
server_socket.bind((HOST, PORT))
server_socket.listen(10)
conn, addr = server_socket.accept()
data = b''
payload_size = struct.calcsize('>L')
print("payload_size: {}".format(payload_size))
while True:
while len(data) < payload_size:
print("Recv: {}".format(len(data)))
data += conn.recv(4096)
print("Done Recv: {}".format(len(data)))
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack(">L", packed_msg_size)[0]
print("msg_size: {}".format(msg_size))
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data, fix_imports=True, encoding="bytes")
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
# Display the resulting frame
cv2.imshow('ImageWindow',frame)
overlay = frame.copy()
set_as_client(overlay)
# Wait 3 mili seconds for an interaction. Check the key and do the corresponding job.
if cv2.waitKey(1) & 0xFF == ord('q'):
break
def set_as_client(overlay):
""" This function going to send camera feed from ASUS to NVIDIA"""
HOST = "192.168.1.4" # ASUS'un IP adresi
PORT = 12345 # Nvidia frameleri bu porttan alacak
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
connection = client_socket.makefile('wb')
img_counter = 0
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
while True:
frame = overlay
result, frame = cv2.imencode('.jpg', frame, encode_param)
#data = zlib.compress(pickle.dumps(frame, 0))
data = pickle.dumps(frame, 0)
size = len(data)
cv2.putText(overlay, str(image_counter), (225, 225), cv2.FONT_HERSHEY_SIMPLEX, 1, (50,225,250), 5)
print("{}: {}".format(img_counter, size))
client_socket.sendall(struct.pack(">L", size) + data)
img_counter += 1
if __name__ == '__main__':
set_as_server()