-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensorlfow_test_client1.py
304 lines (262 loc) · 10.8 KB
/
tensorlfow_test_client1.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import numpy as np
import cv2
import time
import threading
from multiprocessing import Process, Queue
import os
import queue
import imagiz
# import tensorflow as tf
# gpus = tf.config.experimental.list_physical_devices('GPU')
# if gpus:
# try:
# # Currently, memory growth needs to be the same across GPUs
# for gpu in gpus:
# tf.config.experimental.set_memory_growth(gpu, True)
# logical_gpus = tf.config.experimental.list_logical_devices('GPU')
# print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
# except RuntimeError as e:
# # Memory growth must be set before GPUs have been initialized
# print(e)
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
# System call
os.system("")
# Class of different styles
class style():
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
UNDERLINE = '\033[4m'
RESET = '\033[0m'
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
# Pix2Pix variables declaration
# <==================================================================>
# generator = tf.saved_model.load("./model/pix2pixTF-TRT512")
# <==================================================================>
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
exitFlag = 0
queueLock = threading.Lock()
inputPix2PixQueue1 = Queue(3)
outputPix2PixQueue1 = Queue(3)
inputPix2PixQueue2 = Queue(3)
outputPix2PixQueue2 = Queue(3)
client1 = imagiz.TCP_Client(
server_ip='10.42.0.1', server_port=5555, client_name='cc1', request_retries=100)
client2 = imagiz.TCP_Client(
server_ip='10.42.0.1', server_port=5555, client_name='cc2', request_retries=100)
SIZE = 512
NORM = 255.5
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
class myProcess(Process):
def __init__(self, name, function, iq, oq=None):
Process.__init__(self)
self.name = name
self.function = function
self.iq = iq
self.oq = oq
def run(self):
print(style.YELLOW + "Starting " + self.name)
if self.oq:
self.function(self.name, self.iq, self.oq)
else:
self.function(self.name, self.iq)
print(style.GREEN + "Exiting " + self.name)
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
class WebcamVideoStream:
def __init__(self, src=0, device=None):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src, device)
(self.grabbed, self.frame) = self.stream.read()
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
def start(self):
# start the thread to read frames from the video stream
threading.Thread(target=self.update, args=()).start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
# otherwise, read the next frame from the stream
(self.grabbed, self.frame) = self.stream.read()
def read(self):
# return the frame most recently read
return self.frame
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
def gstreamer_pipeline(
sensor_id=0,
capture_width=3840,
capture_height=2160,
display_width=1920, # 2560
display_height=1080, # 1440
framerate=60,
flip_method=0,
):
return (
"nvarguscamerasrc sensor-id=%d ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
sensor_id,
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
# def crop(img, offset_height, offset_width, target_height, target_width):
# """
# Args
# image 4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels].
# offset_height Vertical coordinate of the top-left corner of the result in the input.
# offset_width Horizontal coordinate of the top-left corner of the result in the input.
# target_height Height of the result.
# target_width Width of the result.
# Returns
# If image was 4-D, a 4-D float Tensor of shape [batch, target_height, target_width, channels]
# If image was 3-D, a 3-D float Tensor of shape [target_height, target_width, channels]
# """
# return tf.image.crop_to_bounding_box(
# img, offset_height, offset_width, target_height, target_width)
# # -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
# def resize(img, size, method=ResizeMethod.BILINEAR, preserve_aspect_ratio=False,
# antialias=False, name=None):
# """
# Args
# images 4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels].
# size A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images.
# method An image.ResizeMethod, or string equivalent. Defaults to bilinear.
# Returns
# If images was 4-D, a 4-D float Tensor of shape [batch, new_height, new_width, channels].
# If images was 3-D, a 3-D float Tensor of shape [new_height, new_width, channels].
# """
# return tf.image.resize(img, size, method=ResizeMethod.BILINEAR, preserve_aspect_ratio=False,
# antialias=False, name=None)
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
def get_from_model(name, iq, oq):
print(style.BLUE + name)
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices(
'GPU')
print(len(gpus), "Physical GPUs,", len(
logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(style.RED + str(e))
generator = tf.saved_model.load("./model/pix2pixTF-TRT512")
print(style.GREEN + name)
while not exitFlag:
if not iq.empty():
image = iq.get()
# print(image.shape)
# print(type(image))
input_image = tf.cast(image, tf.float32)
input_image = tf.image.resize(input_image, [SIZE, SIZE],
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
input_image = (input_image / NORM) - 1
# input_image = load_from_video(image)
ext_image = tf.expand_dims(input_image, axis=0)
prediction = generator(ext_image, training=True)
# generated_image = generate_images(generator, ext_image)
# pil_image = tf.keras.preprocessing.image.array_to_img(
# generated_image)
pil_image = tf.keras.preprocessing.image.array_to_img(
prediction[0])
if not oq.full():
oq.put(np.array(pil_image))
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
def send_to_imagiz_server(name, iq, cl):
print(style.BLUE + name)
# encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
while not exitFlag:
if not iq.empty():
# from image to binary buffer
image = iq.get()
# _, image = cv2.imencode('.jpg', iq.get(), encode_param)
res = cl.send(image)
# print(res)
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
def main():
global exitFlag
process = []
cameras = []
cap1 = WebcamVideoStream(src=gstreamer_pipeline(
sensor_id=0), device=cv2.CAP_GSTREAMER).start()
cap2 = WebcamVideoStream(src=gstreamer_pipeline(
sensor_id=1), device=cv2.CAP_GSTREAMER).start()
cameras.append(cap1)
cameras.append(cap2)
clientTH1 = myProcess("client 1", send_to_imagiz_server,
outputPix2PixQueue1, client1)
# clientTH2 = myProcess("client 2", send_to_imagiz_server,
# outputPix2PixQueue2, client2)
pix2pixTH1 = myProcess(
"pix2pix1 Thread", get_from_model, inputPix2PixQueue1, outputPix2PixQueue1)
# pix2pixTH2 = myProcess(
# "pix2pix2 Thread", get_from_model, inputPix2PixQueue2, outputPix2PixQueue2)
process.append(clientTH1)
process.append(pix2pixTH1)
# process.append(clientTH2)
# process.append(pix2pixTH2)
for t in process:
t.start()
try:
while True:
frame1 = cap1.read()
# frame2 = cap2.read()
if not inputPix2PixQueue1.full():
inputPix2PixQueue1.put(frame1)
# if not inputPix2PixQueue2.full():
# inputPix2PixQueue2.put(frame2)
except Exception as e:
print(style.RED + str(e))
for c in cameras:
c.stop()
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in process:
t.join()
except KeyboardInterrupt:
for c in cameras:
c.stop()
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in process:
t.join()
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in process:
t.join()
for c in cameras:
c.stop()
print(style.GREEN + "Exiting Main Thread")
if __name__ == "__main__":
main()