Nerv python cv2라이브러리를 이용한 영상 송수신¶
import time
import threading
import signal
import sys
import numpy
import cv2
#from PIL import Image
import py_nerv as nerv
class class_camera_1(nerv.class_remote_camera):
def on_object_create(self):
err = self.SensorData_Image_connect(True, 1.0 , None )
print("py::SensorData_Image_connect() %d" % err)
print("py::on_object_create()")
print(flush=True)
self
def on_object_destroy(self):
# print("py::on_object_destroy()")
# print(flush=True)
self
def on_SensorData_Image_disconnected(self):
print("py::on_SensorData_Image_disconnected()")
print(flush=True)
self
def on_SensorData_Image_sync(self, in_sync_model, in_elapsed_time, in_limit, pin_buffer):
print("py::on_SensorData_Image_sync(%d , %lf, %f)" % ( in_sync_model , in_elapsed_time, in_limit ) )
print("image size = %d" % pin_buffer.ImageData_size() )
print(flush=True)
# np_img = numpy.frombuffer( pin_buffer.ImageData_memory(0) , dtype=numpy.uint8).reshape( pin_buffer.Height, pin_buffer.Width , 3)
# cv2.imshow('image1', np_img)
time.sleep(1)
self
def main():
print("CV 윈도우")
cv2.namedWindow('image1', cv2.WINDOW_NORMAL)
cv2.namedWindow('image2', cv2.WINDOW_NORMAL)
cv2.namedWindow('image3', cv2.WINDOW_NORMAL)
cv2.resizeWindow('image1', 640,480)
cv2.resizeWindow('image2', 640,480)
cv2.resizeWindow('image3', 640,480)
nerv.initialize()
camera_1 = class_camera_1()
camera_2 = class_camera_1()
camera_3 = class_camera_1()
camera_1.create(nerv.get_Advanced_Method_Sensor_port()+0, nerv.inet_addr("192.168.0.143") , 201, 0 )
camera_2.create(nerv.get_Advanced_Method_Sensor_port()+1, nerv.inet_addr("192.168.0.143") , 202, 0 )
camera_3.create(nerv.get_Advanced_Method_Sensor_port()+2, nerv.inet_addr("192.168.0.143") , 203, 0 )
print("카메라1 객체 생성")
print("카메라2 객체 생성")
print("카메라3 객체 생성")
print("루프")
camera_image_1 = nerv.struct_SensorData_Image()
camera_image_2 = nerv.struct_SensorData_Image()
camera_image_3 = nerv.struct_SensorData_Image()
cnt=0
run=True
while run:
err1 = camera_1.SensorData_Image_read(camera_image_1)
if not err1:
np_img1 = numpy.frombuffer( camera_image_1.ImageData_memory(0) , dtype=numpy.uint8).reshape( camera_image_1.Height , camera_image_1.Width, 3 )
cv2.imshow('image1', np_img1)
print( cnt , " camera1 ", camera_image_1.get_sizeof() )
err2 = camera_2.SensorData_Image_read(camera_image_2)
if not err2:
np_img2 = numpy.frombuffer( camera_image_2.ImageData_memory(0) , dtype=numpy.uint8).reshape( camera_image_2.Height , camera_image_2.Width, 3 )
cv2.imshow('image2', np_img2)
print( cnt , " camera2 ", camera_image_2.get_sizeof() )
err3 = camera_3.SensorData_Image_read(camera_image_3)
if not err3:
np_img3 = numpy.frombuffer( camera_image_3.ImageData_memory(0) , dtype=numpy.uint8).reshape( camera_image_3.Height , camera_image_3.Width, 3 )
cv2.imshow('image3', np_img3)
print( cnt , " camera3 ", camera_image_3.get_sizeof() )
k = cv2.waitKey(33)
if k == 27: # esc key
run = False
cnt=cnt+1
if __name__ == '__main__':
main()
import time
import threading
import signal
import sys
import numpy
import cv2
import py_nerv as nerv
import py_print_nerv_data as nprint
import py_set_nerv_data as nset
class class_camera_py(nerv.class_camera):
def on_SensorData_Image_connect(self, client_addr, in_tcp_request, in_elapsed_time, in_limit_time):
print("py::on_SensorData_Image_connect()")
print(flush=True)
return 0
def on_SensorData_Image_shutdown(self, client_addr):
print("py::on_SensorData_Image_shutdown()")
print(flush=True)
if __name__ == '__main__':
print("hello")
print(flush=True)
nerv.initialize()
camera1 = class_camera_py()
camera1.create(nerv.get_Advanced_Method_Sensor_port()+0, 201 )
# camera2 = class_camera_py()
# camera2.create(nerv.get_Advanced_Method_Sensor_port()+1, 202 )
# camera3 = class_camera_py()
# camera3.create(nerv.get_Advanced_Method_Sensor_port()+2, 203 )
cv2.namedWindow('stub', cv2.WINDOW_NORMAL)
cv2.resizeWindow('stub',640,480)
img = cv2.imread('img.jpg', cv2.IMREAD_COLOR) # read image file 'img.jpg' to numpy 'img'
byte_size =img.shape[0] * img.shape[1] * img.shape[2] # buffer size of image
Height = img.shape[0]
struct_SensorData_Image = nerv.struct_SensorData_Image()
struct_SensorData_Image.Height = img.shape[0]
struct_SensorData_Image.Width = img.shape[1]
struct_SensorData_Image.init_dynamic_buffer() # init dynamic buffer of nerv data
struct_SensorData_Image.ImageData_alloc( byte_size ) # Alloc buffer for ImageData
mem = struct_SensorData_Image.ImageData_memory(1) # 0: readonly 1:read/write # memory view of ImageData
np_img = numpy.frombuffer( mem , dtype=numpy.uint8).reshape( img.shape ) # numpy image from memory view
cnt = 0
run = True
while run:
# nprint.struct_SensorData_Image(struct_SensorData_Image)
# print(flush=True)
first_line0 = img[0]
first_line1 = img[1]
first_line2 = img[2]
first_line3 = img[3]
first_line4 = img[4]
first_line5 = img[5]
first_line6 = img[6]
first_line7 = img[7]
first_line8 = img[8]
first_line9 = img[9]
for i in range(img.shape[0]-10):
img[i] = img[i+1]
img[Height-10] = first_line0
img[Height-9] = first_line1
img[Height-8] = first_line2
img[Height-7] = first_line3
img[Height-6] = first_line4
img[Height-5] = first_line5
img[Height-4] = first_line6
img[Height-3] = first_line7
img[Height-2] = first_line8
img[Height-1] = first_line9
mem[0:byte_size] = img.tobytes('C') # copy numpy image to memory view
# cv2.imshow('stub', np_img ) # show numpy image
cv2.imshow('stub', img ) # show numpy image
# camera1.SensorData_Image_write(struct_SensorData_Image)
# camera2.SensorData_Image_write(struct_SensorData_Image)
# camera3.SensorData_Image_write(struct_SensorData_Image)
#if cnt%30==0:
print(cnt , img.shape , struct_SensorData_Image.get_sizeof() )
cnt=cnt+1
k = cv2.waitKey(3333)
if k == 27: # esc key
run = False
time.sleep(1)