-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv_receiver.py
159 lines (140 loc) · 5.42 KB
/
opencv_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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"
import threading
import struct
import time
import cv2
import numpy as np
import matplotlib.pyplot as plt
import socket
import traceback
INTERRUPT = False
THREAD_LOCK = threading.Lock()
OUTGOING_BUFFER = None
CHUNK_SIZE = 1400
# image defaults
IMAGE_WIDTH = 1280
IMAGE_HEIGHT = 720
EOM_MARKER = b'<<EOM>>'
# image for later use
RGB_FRAME = None
DEPTH_FRAME = None
# thread safety lock
THREAD_LOCK = threading.Lock()
def rgb_func(ipaddr='127.0.0.1', port=65400):
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udp_socket.bind((ipaddr, port))
udp_socket.settimeout(0.1)
while not INTERRUPT:
try:
frame_data = bytearray()
# read until saw at least two EOM markers
while frame_data.count(EOM_MARKER) < 2:
try:
data, _ = udp_socket.recvfrom(CHUNK_SIZE)
frame_data.extend(data)
except socket.timeout:
pass
# get the content between the two EOM markers
start_idx = frame_data.find(EOM_MARKER)
end_idx = frame_data.find(EOM_MARKER, start_idx + 1)
frame_data = frame_data[start_idx + len(EOM_MARKER):end_idx]
# decode JPEG image
nparr = np.frombuffer(frame_data, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# display the image
# cv2.imshow('RGB Image', frame)
# cv2.waitKey(1)
# save the image
with THREAD_LOCK:
global RGB_FRAME
RGB_FRAME = frame
except:
# print(traceback.format_exc())
# probably garbled frame, ignore
pass
def depth_func(ipaddr='127.0.0.1', port=65401):
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udp_socket.bind((ipaddr, port))
udp_socket.settimeout(0.1)
while not INTERRUPT:
try:
frame_data = bytearray()
# read until saw at least two EOM markers
while frame_data.count(EOM_MARKER) < 2:
try:
data, _ = udp_socket.recvfrom(CHUNK_SIZE)
frame_data.extend(data)
except socket.timeout:
pass
# get the content between the two EOM markers
start_idx = frame_data.find(EOM_MARKER)
end_idx = frame_data.find(EOM_MARKER, start_idx + 1)
frame_data = frame_data[start_idx + len(EOM_MARKER):end_idx]
# decode EXR ZIP image
frame = np.frombuffer(frame_data, np.uint8)
frame = cv2.imdecode(frame, cv2.IMREAD_UNCHANGED)
frame = frame[:, :, 2]
# print(f'Minimum depth: {np.min(frame)}, Maximum depth: {np.max(frame)}')
frame = cv2.normalize(frame, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
# display the image
# cv2.imshow('Depth Image', frame)
# cv2.waitKey(1)
# save the image
with THREAD_LOCK:
global DEPTH_FRAME
DEPTH_FRAME = frame
except:
# print(traceback.format_exc())
# probably garbled frame, ignore
pass
def send_thread(ipaddr='127.0.0.1', bind_port=65403, destination_port=65402):
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udp_socket.bind((ipaddr, bind_port))
udp_socket.settimeout(0.1)
while not INTERRUPT:
try:
rgb_image = None
depth_image = None
try:
with THREAD_LOCK:
if RGB_FRAME is not None: rgb_image = RGB_FRAME.copy()
if DEPTH_FRAME is not None: depth_image = DEPTH_FRAME.copy()
except:
pass
# ---------------------------------------------------------------------------- #
# ADD YOUR CODE BELOW THIS LINE #
# ---------------------------------------------------------------------------- #
# do image processing here
# update the information needed for robot motion
outgoing_message = 'test message'.encode()
# ---------------------------------------------------------------------------- #
# ADD YOUR CODE ABOVE THIS LINE #
# ---------------------------------------------------------------------------- #
udp_socket.sendto(outgoing_message, (ipaddr, destination_port))
except Exception:
# print(traceback.format_exc())
# probably garbled frame, ignore
pass
if __name__ == '__main__':
# start rgb thread
rbg_thread = threading.Thread(target=rgb_func)
rbg_thread.start()
# start depth thread
depth_thread = threading.Thread(target=depth_func)
depth_thread.start()
# start sending thread
send_thread = threading.Thread(target=send_thread)
send_thread.start()
# wait for threads to finish
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
INTERRUPT = True
rbg_thread.join()
send_thread.join()
exit(0)