From aa1c8d7edcc38d0c8f1ce26ec78c0eab3cc13b40 Mon Sep 17 00:00:00 2001 From: Arnaud Blanchard <arnaud.blanchard@ensea.fr> Date: Fri, 15 Oct 2021 17:25:49 +0200 Subject: [PATCH] Exemple to stream an image --- python/opencv_client.py | 24 ++++++++++++++++++++++++ python/opencv_server.py | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 python/opencv_client.py create mode 100644 python/opencv_server.py diff --git a/python/opencv_client.py b/python/opencv_client.py new file mode 100644 index 0000000..1802684 --- /dev/null +++ b/python/opencv_client.py @@ -0,0 +1,24 @@ +import cv2 +import numpy + +from blc_network import BlcArrayTCP4Client +client=BlcArrayTCP4Client("localhost", "31440") + +#these informations has been sent by the server +height = client.dims[0] +width = client.dims[1] +deep = client.dims[2] + +print(height,"x",width,"x",deep, cleint.type, client.format) + +#We create an numpy array (equivalent to cv:Mat in C++ ) associated with the data of the client +client_array = numpy.ndarray([height, width, deep], dtype=numpy.uint8, buffer=client.data, offset=0, strides=None, order='C') + +while(True): + client.recv_data() + #We even do not need a copy as the numpy array directly links to the data + cv2.imshow('client', client_array) + cv2.waitKey(1) + + + diff --git a/python/opencv_server.py b/python/opencv_server.py new file mode 100644 index 0000000..3734f52 --- /dev/null +++ b/python/opencv_server.py @@ -0,0 +1,25 @@ +import cv2 +import numpy +from blc_network import BlcArrayTCP4Server + +vid = cv2.VideoCapture(0) +height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) +width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) +deep=3 # 1 if it is in gray + +print(height,"x", width) + +server=BlcArrayTCP4Server("31440", 'UIN8', 'RGB3', [height, width, 3]) +print("Wait client") +server.wait_connection() + +#We create an numpy array (equivalent to cv:Mat in C++ ) associated with the data of server +server_array = numpy.ndarray([height, width, 3], dtype=numpy.uint8, buffer=server.data, offset=0, strides=None, order='C') + +while(True): + ret, img = vid.read() + #we copy the pixels of the image to the array of the server (i.e. the buffer: server.data) + numpy.copyto(server_array, img) + server.send_data() + cv2.imshow('server', server_array) + cv2.waitKey(1) -- GitLab