import numpy as np import math import os import mimetypes import random import shutil import subprocess from PIL import Image from . import IE_funcs from . import io import copy class IE(object): def __del__(self): if self.thread == '': return try: shutil.rmtree(self.CSIM_path) pass except OSError as e: pass else: pass def __init__(self, CSIM_path=None, thread='', bypass_csim=False, c_compiler=False): ## ini CSIM_path if CSIM_path is None: CSIM_path = os.path.split(os.path.realpath(__file__))[ 0] + '/IE_funcs/bach_ie_cmodel/tlm_model' self.CSIM_path = CSIM_path self.bypass_csim = bypass_csim self.c_compiler = c_compiler # check ie_cmodel if not os.path.isfile(self.CSIM_path+'/ie_cmodel'): try: out = subprocess.check_output(['make', '--version']) except OSError: raise RuntimeError("Make must be installed to build ie_cmdeol") return os.system('cd '+self.CSIM_path+';make -j') # check thread self.thread = str(thread) if self.thread != '': CSIM_path_thread = self.CSIM_path + self.thread while True: try: os.mkdir(CSIM_path_thread) except OSError: print('CSIM_path exist') CSIM_path_thread = CSIM_path_thread + str(random.randint(0,9)) else: break if not os.path.isfile(CSIM_path_thread+'/ie_cmodel'): shutil.copy(self.CSIM_path+'/ie_cmodel',CSIM_path_thread+'/ie_cmodel') self.CSIM_path = CSIM_path_thread ## ini sub class self.m_rotate = IE_funcs.rotate(CSIM_path=self.CSIM_path, bypass_csim = bypass_csim) self.m_matrix = IE_funcs.matrix(CSIM_path=self.CSIM_path, bypass_csim = bypass_csim) self.m_dewarping = IE_funcs.dewarp(CSIM_path=self.CSIM_path, bypass_csim = bypass_csim) # RGBA2NIR self.matrix_RGBA2NIR = {} self.shuffle_RGBA2NIR= {} self.matrix_RGBA2NIR['isigned'] = 0 self.matrix_RGBA2NIR['osigned'] = 0 self.matrix_RGBA2NIR['n'] = 0 self.matrix_RGBA2NIR['csc_sub'] = 0 self.matrix_RGBA2NIR['csc_sub_sel'] = 0 self.matrix_RGBA2NIR['c00'] = 1224 self.matrix_RGBA2NIR['c01'] = 2405 self.matrix_RGBA2NIR['c02'] = 467 self.matrix_RGBA2NIR['c10'] = 0 self.matrix_RGBA2NIR['c11'] = 0 self.matrix_RGBA2NIR['c12'] = 0 self.matrix_RGBA2NIR['c20'] = 0 self.matrix_RGBA2NIR['c21'] = 0 self.matrix_RGBA2NIR['c22'] = 0 self.matrix_RGBA2NIR['c33'] = 0 self.matrix_RGBA2NIR['b0'] = 0 self.matrix_RGBA2NIR['b1'] = 0 self.matrix_RGBA2NIR['b2'] = 0 self.matrix_RGBA2NIR['b3'] = 0 self.shuffle_RGBA2NIR['n'] = 0 self.shuffle_RGBA2NIR['b'] = 4 self.shuffle_RGBA2NIR['g'] = 4 self.shuffle_RGBA2NIR['r'] = 4 # NIR self.matrix_NIR2RGB = {} self.shuffle_NIR2RGB= {} self.matrix_NIR2RGB['isigned'] = 0 self.matrix_NIR2RGB['osigned'] = 0 self.matrix_NIR2RGB['n'] = 0 self.matrix_NIR2RGB['csc_sub'] = 0 self.matrix_NIR2RGB['csc_sub_sel'] = 0 self.matrix_NIR2RGB['c00'] = 4096 self.matrix_NIR2RGB['c01'] = 0 self.matrix_NIR2RGB['c02'] = 0 self.matrix_NIR2RGB['c10'] = 0 self.matrix_NIR2RGB['c11'] = 4096 self.matrix_NIR2RGB['c12'] = 0 self.matrix_NIR2RGB['c20'] = 0 self.matrix_NIR2RGB['c21'] = 0 self.matrix_NIR2RGB['c22'] = 4096 self.matrix_NIR2RGB['c33'] = 0 self.matrix_NIR2RGB['b0'] = 0 self.matrix_NIR2RGB['b1'] = 0 self.matrix_NIR2RGB['b2'] = 0 self.matrix_NIR2RGB['b3'] = 0 self.shuffle_NIR2RGB['n'] = 3 self.shuffle_NIR2RGB['b'] = 0 self.shuffle_NIR2RGB['g'] = 0 self.shuffle_NIR2RGB['r'] = 0 # YUV2RGB self.matrix_YUV2RGB = {} self.shuffle_YUV2RGB = {} self.matrix_YUV2RGB['isigned'] = 0 self.matrix_YUV2RGB['osigned'] = 0 self.matrix_YUV2RGB['n'] = 0 self.matrix_YUV2RGB['csc_sub'] = 1 self.matrix_YUV2RGB['csc_sub_sel'] = 1 self.matrix_YUV2RGB['c00'] = 0x1000 self.matrix_YUV2RGB['c01'] = 0 self.matrix_YUV2RGB['c02'] = 0x1670 self.matrix_YUV2RGB['c10'] = 0x1000 self.matrix_YUV2RGB['c11'] = 0xfa78 self.matrix_YUV2RGB['c12'] = 0xf490 self.matrix_YUV2RGB['c20'] = 0x1000 self.matrix_YUV2RGB['c21'] = 0x1c58 self.matrix_YUV2RGB['c22'] = 0 self.matrix_YUV2RGB['c33'] = 0x2000 self.matrix_YUV2RGB['b0'] = 0 self.matrix_YUV2RGB['b1'] = 0 self.matrix_YUV2RGB['b2'] = 0 self.matrix_YUV2RGB['b3'] = 0x0040 self.shuffle_YUV2RGB['n'] = 3 self.shuffle_YUV2RGB['b'] = 2 self.shuffle_YUV2RGB['g'] = 1 self.shuffle_YUV2RGB['r'] = 0 # YCbCr2RGB self.matrix_YCbCr2RGB = {} self.matrix_YCbCr2RGB['isigned'] = 0 self.matrix_YCbCr2RGB['osigned'] = 0 self.matrix_YCbCr2RGB['n'] = 0 self.matrix_YCbCr2RGB['csc_sub'] = 1 self.matrix_YCbCr2RGB['csc_sub_sel'] = 0 self.matrix_YCbCr2RGB['c00'] = 4768 self.matrix_YCbCr2RGB['c01'] = 0 self.matrix_YCbCr2RGB['c02'] = 6536 self.matrix_YCbCr2RGB['c10'] = 4768 self.matrix_YCbCr2RGB['c11'] = 63932 self.matrix_YCbCr2RGB['c12'] = 62204 self.matrix_YCbCr2RGB['c20'] = 4768 self.matrix_YCbCr2RGB['c21'] = 8260 self.matrix_YCbCr2RGB['c22'] = 0 self.matrix_YCbCr2RGB['c33'] = 0 self.matrix_YCbCr2RGB['b0'] = 0 self.matrix_YCbCr2RGB['b1'] = 0 self.matrix_YCbCr2RGB['b2'] = 0 self.matrix_YCbCr2RGB['b3'] = 0 def crop(self, image, box = None, tile_size=(64,64)): ## assert(isinstance(box,tuple) | isinstance(box,list)) ## input_img_h = image.shape[0] input_img_w = image.shape[1] ## # box = (np.clip(box[0],0,input_img_w),np.clip(box[1],0,input_img_h),np.clip(box[2],0,input_img_w),np.clip(box[3],0,input_img_h)) ## matrix = [[1,0,-box[0]],[0,1,-box[1]]] size_o = np.matmul(matrix, np.array([[input_img_w], [input_img_h], [1]])) output_img_w = int(np.ceil(size_o[0])) - (input_img_w - box[2]) output_img_h = int(np.ceil(size_o[1])) - (input_img_h - box[3]) ## image_data = self.m_dewarping(image=image, matrix=matrix, output_size=(output_img_w,output_img_h), tile_size=tile_size) return image_data # def resize(self, image, size=None, tile_size=None): ## assert(isinstance(size,tuple) | isinstance(size,list)) input_img_w = image.shape[1] input_img_h = image.shape[0] output_img_w = size[0] output_img_h = size[1] if self.c_compiler: IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) input_fmt = 'RGB' IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt, output_size=size) IE_Object.pack_img(IE_Object.image) command = f'RGBA_{input_img_w}x{input_img_h}_resize_{output_img_w}x{output_img_h}' # print(command) # command = 'RGBA_200x200_resize_100x100' # print(self.CSIM_path) os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') IE_Object.runCSIM() image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out else: scale_w = (output_img_w-1) / (input_img_w-1) scale_h = (output_img_h-1) / (input_img_h-1) matrix = [[scale_w,0,0],[0,scale_h,0]] ## tile analysis if tile_size is not None: tile_size_w = tile_size[0] tile_size_h = tile_size[1] else: tile_size_w = 64 tile_size_h = 64 if scale_w <= 1: tile_size_w = (int(64 * scale_w)//2) * 2 if scale_h <= 1: tile_size_h = (int(64 * scale_h)//2) * 2 print('tile_size',tile_size_w,tile_size_h) if (tile_size_w < 2) & (tile_size_h < 2): assert False, 'tile_size too small' # print('tile_size_w,tile_size_h: ',tile_size_w,tile_size_h) image_out = self.m_dewarping(image=image, matrix=matrix, output_size=size, tile_size=(tile_size_w,tile_size_h)) return image_out def dewarping(self, image, matrix, dst_size = None, tile_size=(16,16)): ## assert(isinstance(image,np.ndarray)) assert(isinstance(matrix,list) | isinstance(matrix,np.ndarray)) ## input_img_h = image.shape[0] input_img_w = image.shape[1] size_o1 = np.matmul(matrix, np.array([[input_img_w - 1], [input_img_h - 1], [1]])) size_o2 = np.matmul(matrix, np.array([[input_img_w - 1], [0], [1]])) size_o3 = np.matmul(matrix, np.array([[0], [input_img_h - 1], [1]])) output_img_w = int(np.ceil(min(size_o1[0], size_o2[0]))) output_img_h = int(np.ceil(min(size_o1[1], size_o3[1]))) if dst_size is not None: output_img_w = dst_size[0] output_img_h = dst_size[1] ## if self.c_compiler: pass # IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) # input_fmt = 'RGB' # IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt, output_size=size) # IE_Object.pack_img(IE_Object.image) # command = f'RGBA_{input_img_w}x{input_img_h}_resize_{output_img_w}x{output_img_h}' # # print(command) # # command = 'RGBA_200x200_resize_100x100' # # print(self.CSIM_path) # os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') # IE_Object.runCSIM() # image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) # return image_out else: image_out = self.m_dewarping(image=image, matrix=matrix, output_size=(output_img_w,output_img_h), tile_size=tile_size) return image_out def camera_calibration(self, image, control_points_tar, control_points_ori,tile_size=(64,64)): ## assert(isinstance(image,np.ndarray)) ## if self.c_compiler: pass # IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) # input_fmt = 'RGB' # IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt, output_size=size) # IE_Object.pack_img(IE_Object.image) # command = f'RGBA_{input_img_w}x{input_img_h}_resize_{output_img_w}x{output_img_h}' # # print(command) # # command = 'RGBA_200x200_resize_100x100' # # print(self.CSIM_path) # os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') # IE_Object.runCSIM() # image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) # return image_out else: image_out = self.m_dewarping(image=image, control_points_tar=control_points_tar, control_points_ori=control_points_ori, tile_size=tile_size) return image_out def matrix(self,image,matirx=None,bias=None): assert(isinstance(image,np.ndarray)) matrix_dic = {} matrix_dic['isigned'] = 0 matrix_dic['osigned'] = 0 matrix_dic['n'] = 0 matrix_dic['csc_sub'] = 0 matrix_dic['csc_sub_sel'] = 0 if matirx is not None: assert(isinstance(matrix,list)) matrix_dic['c00'] = int(matirx[0][0]*4096) matrix_dic['c01'] = int(matirx[0][1]*4096) matrix_dic['c02'] = int(matirx[0][2]*4096) matrix_dic['c10'] = int(matirx[1][0]*4096) matrix_dic['c11'] = int(matirx[1][1]*4096) matrix_dic['c12'] = int(matirx[1][2]*4096) matrix_dic['c20'] = int(matirx[2][0]*4096) matrix_dic['c21'] = int(matirx[2][1]*4096) matrix_dic['c22'] = int(matirx[2][2]*4096) matrix_dic['c33'] = 0 else: matrix_dic['c00'] = 4096 matrix_dic['c01'] = 0 matrix_dic['c02'] = 0 matrix_dic['c10'] = 0 matrix_dic['c11'] = 4096 matrix_dic['c12'] = 0 matrix_dic['c20'] = 0 matrix_dic['c21'] = 0 matrix_dic['c22'] = 4096 matrix_dic['c33'] = 0 if bias is not None: assert(isinstance(bias,list)) matrix_dic['b0'] = int(bias[0]*4096) matrix_dic['b1'] = int(bias[1]*4096) matrix_dic['b2'] = int(bias[2]*4096) matrix_dic['b3'] = 0 else: matrix_dic['b0'] = 0 matrix_dic['b1'] = 0 matrix_dic['b2'] = 0 matrix_dic['b3'] = 0 if self.c_compiler: pass # IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) # input_fmt = 'RGB' # IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt, output_size=size) # IE_Object.pack_img(IE_Object.image) # command = f'RGBA_{input_img_w}x{input_img_h}_resize_{output_img_w}x{output_img_h}' # # print(command) # # command = 'RGBA_200x200_resize_100x100' # # print(self.CSIM_path) # os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') # IE_Object.runCSIM() # image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) # return image_out else: image_out = self.m_matrix(image=image, color_matrix=matrix_dic) return image_out def get_matrix_and_shuffle(self, input_fmt='RGB', output_fmt='RGB'): color_matrix = None shuffle = None ## RGBA & RGB if input_fmt.lower() in ['rgba', 'rgba8888', 'rgb', 'rgb888']: if output_fmt.lower() in ['RGBA', 'rgba', 'RGBA8888', 'rgba8888', 'RGB', 'rgb', 'RGB888', 'rgb888']: pass elif output_fmt.lower() in ['nir', 'l']: color_matrix = self.matrix_RGBA2NIR shuffle = self.shuffle_RGBA2NIR elif output_fmt.lower() in ['RGB565', 'rgb565']: pass elif output_fmt.lower() in ['YUYV', 'yuyv', 'YUV422', 'yuv422']: assert False, 'not support RGB/RGBA to YUV yet' elif output_fmt.lower() in ['YCBCR', 'ycbcr', 'YCBCR422', 'YCbCr422']: assert False, 'not support RGB/RGBA to YCbCr yet' ## NIR elif input_fmt.lower() in ['nir', 'l']: if output_fmt.lower() in ['RGBA', 'rgba', 'RGBA8888', 'rgba8888', 'RGB', 'rgb', 'RGB888', 'rgb888']: color_matrix = self.matrix_NIR2RGB shuffle = self.shuffle_NIR2RGB pass elif output_fmt.lower() in ['nir', 'l']: pass elif output_fmt.lower() in ['RGB565', 'rgb565']: assert False, 'not support NIR to rgb565 yet' elif output_fmt.lower() in ['YUYV', 'yuyv', 'YUV422', 'yuv422']: assert False, 'not support NIR to YUV yet' elif output_fmt.lower() in ['YCBCR', 'ycbcr', 'YCBCR422', 'YCbCr422']: assert False, 'not support NIR to YCbCr yet' ## RGB5665 elif input_fmt.lower() in ['rgb565']: if output_fmt.lower() in ['RGBA', 'rgba', 'RGBA8888', 'rgba8888', 'RGB', 'rgb', 'RGB888', 'rgb888']: pass elif output_fmt.lower() in ['NIR', 'nir', 'L', 'l']: color_matrix = self.matrix_RGBA2NIR shuffle = self.shuffle_RGBA2NIR elif output_fmt.lower() in ['RGB565', 'rgb565']: pass elif output_fmt.lower() in ['YUYV', 'yuyv', 'YUV422', 'yuv422']: assert False, 'not support RGB5665 to YUV yet' elif output_fmt.lower() in ['YCBCR', 'ycbcr', 'YCBCR422', 'YCbCr422']: assert False, 'not support RGB5665 to YCbCr yet' ## YUV elif input_fmt.lower() in ['YUYV', 'yuyv', 'yuv', 'yuv444', 'yuv422']: if input_fmt.lower() in ['yuv444']: assert False, 'IE not support input format as YUV444' if output_fmt.lower() in ['RGBA', 'rgba', 'RGBA8888', 'rgba8888', 'RGB', 'rgb', 'RGB888', 'rgb888']: color_matrix = self.matrix_YUV2RGB shuffle = self.shuffle_YUV2RGB elif output_fmt.lower() in ['NIR', 'nir', 'L', 'l']: assert False, 'not support YUV to NIR yet' elif output_fmt.lower() in ['RGB565', 'rgb565']: color_matrix = self.matrix_YUV2RGB shuffle = self.shuffle_YUV2RGB elif output_fmt.lower() in ['YUYV', 'yuyv', 'YUV422', 'yuv422']: pass elif output_fmt.lower() in ['YCBCR', 'ycbcr', 'YCBCR422', 'YCbCr422']: assert False, 'not support YUV to YCbCr yet' ## YCBCR elif input_fmt.lower() in ['ycbcr', 'ycbcr444','ycbcr422','ycbcr420']: if input_fmt.lower() in ['ycbcr444']: assert False, 'IE not support input format as YCBCR444' if output_fmt.lower() in ['RGBA', 'rgba', 'RGBA8888', 'rgba8888', 'RGB', 'rgb', 'RGB888', 'rgb888']: color_matrix = self.matrix_YCbCr2RGB elif output_fmt.lower() in ['NIR', 'nir', 'L', 'l']: assert False, 'not support YCBCR to NIR yet' elif output_fmt.lower() in ['RGB565', 'rgb565']: color_matrix = self.matrix_YCbCr2RGB elif output_fmt.lower() in ['YUYV', 'yuyv', 'YUV422', 'yuv422']: assert False, 'not support YCBCR to YCbCr yet' elif output_fmt.lower() in ['YCBCR', 'ycbcr', 'YCBCR422', 'YCbCr422']: pass return color_matrix, shuffle def color(self, image, input_fmt='RGB', output_fmt='RGB',tile_size=(64,64)): assert(isinstance(image,np.ndarray)) if self.c_compiler: pass # IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) # input_fmt = 'RGB565' # IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt) # IE_Object.pack_img(IE_Object.image) # input_img_h = image.shape[0] # input_img_w = image.shape[1] # command = f'{input_fmt}_{input_img_w}x{input_img_h}_color_RGBA_tilew_{tile_size[0]}_tileh_{tile_size[1]}' # print("cmd:{}".format(command)) # os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') # IE_Object.runCSIM() # image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) # return image_out else: color_matrix, shuffle = self.get_matrix_and_shuffle(input_fmt=input_fmt, output_fmt=output_fmt) image_out = self.m_matrix(image=image, color_matrix=color_matrix, shuffle=shuffle, input_fmt=input_fmt, output_fmt=output_fmt,tile_size=tile_size) return image_out def rotate(self, image, degree,tile_size=(64,64)): assert(isinstance(image,np.ndarray)) assert(degree == 90 or degree == 180 or degree == 270) if self.c_compiler: pass IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) input_fmt = 'RGBA' IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt) IE_Object.pack_img(IE_Object.image) input_img_h = image.shape[0] input_img_w = image.shape[1] command = f'{input_fmt}_{input_img_w}x{input_img_h}_rotate_{degree}' os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') IE_Object.runCSIM() image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out else: image_out = self.m_rotate(image=image,degree=degree,tile_size=tile_size) return image_out def rotate2(self, image, degree,tile_size=(16,16)): assert(isinstance(image,np.ndarray)) matrix = np.array([ [math.cos(math.radians(degree)),-math.sin(math.radians(degree)),0], [math.sin(math.radians(degree)), math.cos(math.radians(degree)),0]]) ## calculate output_size input_img_h = image.shape[0] input_img_w = image.shape[1] output_img_w, output_img_h = (abs(matrix).dot(np.array([input_img_w, input_img_h, 0]))).round().astype('int')[0:2] if self.c_compiler: pass IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) input_fmt = 'RGBA' IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt) IE_Object.pack_img(IE_Object.image) command = f'{input_fmt}_{input_img_w}x{input_img_h}_rotate2_{degree}' os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') IE_Object.runCSIM() image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out else: ## calculate shift length shift_w1, shift_h1 = (matrix.dot(np.array([input_img_w, input_img_h, 0])) ).round().astype('int')[0:2] shift_w2, shift_h2 = (matrix.dot(np.array([input_img_w, 0, 0])) ).round().astype('int')[0:2] shift_w3, shift_h3 = (matrix.dot(np.array([0, input_img_h, 0])) ).round().astype('int')[0:2] shift_w = min(shift_w1,shift_w2,shift_w3) shift_h = min(shift_h1,shift_h2,shift_h3) if shift_w < 0: matrix[0,2] = -shift_w if shift_h < 0: matrix[1,2] = -shift_h ## call dewarp image_out = self.m_dewarping(image=image, matrix=matrix,output_size=(output_img_w,output_img_h), tile_size=tile_size) return image_out def load_raw(self, file, image_fmt, raw_w, raw_h, image_order=0): mime = mimetypes.guess_type(file) ## hex image = io.load_hex(input_file=file, image_fmt=image_fmt, src_w=raw_w, src_h=raw_h, order=image_order) ## bin return image def color_rotate2_resize(self, image, size=None, degree=0, input_fmt='RGB', output_fmt='RGB'): assert(isinstance(image,np.ndarray)) if self.c_compiler: input_img_w = image.shape[1] input_img_h = image.shape[0] output_img_w = size[0] output_img_h = size[1] if degree: matrix = np.array([ [math.cos(math.radians(degree)),-math.sin(math.radians(degree)),0], [math.sin(math.radians(degree)), math.cos(math.radians(degree)),0], [0,0,1]]) ## calculate output_img_h output_img_w, output_img_h = (abs(matrix).dot(np.array([output_img_w, output_img_h, 0]))).round().astype('int')[0:2] IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) IE_Object.ini_image(image=image, input_format=input_fmt, output_format=output_fmt, output_size=size) IE_Object.pack_img(IE_Object.image) command = f'{input_fmt}_{input_img_w}x{input_img_h}_colorXrotate2Xresize_{output_fmt}_rotate2_{degree}_resize_{size[0]}x{size[1]}' print("cmd:{}".format(command)) os.system(f'cd {self.CSIM_path};./kdp530_test 8 {command}') IE_Object.runCSIM() image_out = IE_Object.load_image(img_w=output_img_w, img_h=output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out else: ## color matrix part color_matrix, shuffle = self.get_matrix_and_shuffle(input_fmt=input_fmt, output_fmt=output_fmt) ## ori w, h input_img_h = image.shape[0] input_img_w = image.shape[1] ##================= resize part=================== assert(isinstance(size,tuple) | isinstance(size,list)) scale_w = size[0] / input_img_w scale_h = size[1] / input_img_h matrix_resize = np.array([[scale_w,0,0],[0,scale_h,0],[0,0,1]]) input_img_w = size[0] input_img_h = size[1] ##================= rotate part=================== matrix = np.array([ [math.cos(math.radians(degree)),-math.sin(math.radians(degree)),0], [math.sin(math.radians(degree)), math.cos(math.radians(degree)),0], [0,0,1]]) ## calculate output_size output_img_w, output_img_h = (abs(matrix).dot(np.array([input_img_w, input_img_h, 0]))).round().astype('int')[0:2] ## calculate shift length shift_w1, shift_h1 = (matrix.dot(np.array([input_img_w, input_img_h, 0])) ).round().astype('int')[0:2] shift_w2, shift_h2 = (matrix.dot(np.array([input_img_w, 0, 0])) ).round().astype('int')[0:2] shift_w3, shift_h3 = (matrix.dot(np.array([0, input_img_h, 0])) ).round().astype('int')[0:2] shift_w = min(shift_w1,shift_w2,shift_w3) shift_h = min(shift_h1,shift_h2,shift_h3) if shift_w < 0: matrix[0,2] = -shift_w if shift_h < 0: matrix[1,2] = -shift_h ##=================final matrix=================== matrix = matrix.dot(matrix_resize)[0:2,:] ## tile analysis tile_size_w = 64 tile_size_h = 64 if scale_w <= 1: tile_size_w = (int(64 * scale_w)//2) * 2 if scale_h <= 1: tile_size_h = (int(64 * scale_h)//2) * 2 # print('tile_size:',tile_size_w,tile_size_h) if (tile_size_w < 2) & (tile_size_h < 2): assert False, 'tile_size too small' # ## call dewarp # image_out = self.m_dewarping(image=image, matrix=matrix,output_size=(output_img_w,output_img_h), tile_size=(tile_size_w,tile_size_h)) # return image_out ## main flow IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) IE_Object.ini_image(image=image, input_format=input_fmt, output_format=output_fmt, output_size=(output_img_w,output_img_h)) IE_Object.pack_img(IE_Object.image) IE_Object.tile_analysis(tile_size=(tile_size_w,tile_size_h), matrix=matrix) self.m_matrix.input_format = IE_Object.input_format ## get instructions cnt = 0 mem = 0 instructions = [] for tile in np.array(IE_Object.tiles).flat: if (tile['rdma']['len'] == 0) | (tile['rdma']['line'] == 0): # dewarping instruction, mem = self.m_dewarping.compile( tile=tile, cnt=cnt, mem=mem, IE_Object=IE_Object,) instructions += instruction else: ## matrix instruction, mem = self.m_matrix.compile( tile=tile, cnt=cnt, mem=mem, IE_Object=IE_Object, color_matrix=color_matrix, shuffle=shuffle, multimodule_state='start') instructions += instruction # dewarping instruction, mem = self.m_dewarping.compile( tile=tile, cnt=cnt, mem=mem, IE_Object=IE_Object, multimodule_state='end') instructions += instruction cnt += 1 ## write instructions IE_Object.write_instructions(instructions) ## run Csim IE_Object.runCSIM() ## load_image image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out def color_resize(self, image, size=None, input_fmt='RGB', output_fmt='RGB'): assert(isinstance(image,np.ndarray)) ## color matrix part color_matrix, shuffle = self.get_matrix_and_shuffle(input_fmt=input_fmt, output_fmt=output_fmt) ## resize part assert(isinstance(size,tuple) | isinstance(size,list)) input_img_w = image.shape[1] input_img_h = image.shape[0] output_img_w = size[0] output_img_h = size[1] if self.c_compiler: pass IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) input_fmt = 'RGBA' IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt, output_size=size) IE_Object.pack_img(IE_Object.image) command = f'{input_fmt}_{input_img_w}x{input_img_h}_colorXresize_{output_fmt}_resize_{output_img_w}x{output_img_h}' os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') IE_Object.runCSIM() image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out else: ## scale_w = output_img_w / input_img_w scale_h = output_img_h / input_img_h matrix = [[scale_w,0,0],[0,scale_h,0]] ## tile analysis tile_size_w = 64 tile_size_h = 64 if scale_w <= 1: tile_size_w = (int(64 * scale_w)//2) * 2 if scale_h <= 1: tile_size_h = (int(64 * scale_h)//2) * 2 print('tile_size:',tile_size_w,tile_size_h) if (tile_size_w < 2) & (tile_size_h < 2): assert False, 'tile_size too small' ## main flow IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) IE_Object.ini_image(image=image, input_format=input_fmt, output_format=output_fmt, output_size=size) IE_Object.pack_img(IE_Object.image) IE_Object.tile_analysis(tile_size=(tile_size_w,tile_size_h), matrix=matrix) self.m_matrix.input_format = IE_Object.input_format ## get instructions cnt = 0 mem = 0 instructions = [] for tile in np.array(IE_Object.tiles).flat: ## matrix instruction, mem = self.m_matrix.compile( tile=tile, cnt=cnt, mem=mem, IE_Object=IE_Object, color_matrix=color_matrix, shuffle=shuffle, multimodule_state='start') instructions += instruction # dewarping instruction, mem = self.m_dewarping.compile( tile=tile, cnt=cnt, mem=mem, IE_Object=IE_Object, multimodule_state='end') instructions += instruction cnt += 1 ## write instructions IE_Object.write_instructions(instructions) ## run Csim IE_Object.runCSIM() ## load_image image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out def color_rotate(self, image, degree, input_fmt='RGB', output_fmt='RGB', tile_size=(64,64)): assert(isinstance(image,np.ndarray)) ## color matrix part color_matrix, shuffle = self.get_matrix_and_shuffle(input_fmt=input_fmt, output_fmt=output_fmt) ## rotate part assert(degree == 90 or degree == 180 or degree == 270) if self.c_compiler: pass IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) input_fmt = 'RGBA' IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt) IE_Object.pack_img(IE_Object.image) input_img_h, input_img_w = image.shape[0:2] command = f'{input_fmt}_{input_img_w}x{input_img_h}_colorXrotate_{output_fmt}_rotate_{degree}' os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') IE_Object.runCSIM() image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out else: ## tile analysis tile_size_w = tile_size[0] tile_size_h = tile_size[1] if (tile_size_w < 2) & (tile_size_h < 2): assert False, 'tile_size too small' ## main flow IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) IE_Object.ini_image(image=image, input_format=input_fmt, output_format=output_fmt) IE_Object.pack_img(IE_Object.image) IE_Object.tile_analysis(tile_size=(tile_size_w,tile_size_h)) self.m_rotate.calculate_wdma(degree=degree,IE_Object=IE_Object) self.m_matrix.input_format = IE_Object.input_format ## get instructions cnt = 0 mem = 0 instructions = [] for tile in np.array(IE_Object.tiles).flat: ## matrix instruction, mem = self.m_matrix.compile( tile=tile, cnt=cnt, mem=mem, IE_Object=IE_Object, color_matrix=color_matrix, shuffle=shuffle, multimodule_state='start') instructions += instruction # rotate instruction, mem = self.m_rotate.compile( tile=tile, cnt=cnt, mem=mem, IE_Object=IE_Object, degree = degree, multimodule_state='end') instructions += instruction cnt += 1 ## write instructions IE_Object.write_instructions(instructions) ## run Csim IE_Object.runCSIM() ## load_image image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out def resize_rotate(self, image, degree, size=None, input_fmt='RGB'): assert(isinstance(image,np.ndarray)) ## resize part assert(isinstance(size,tuple) | isinstance(size,list)) input_img_w = image.shape[1] input_img_h = image.shape[0] output_img_w = size[0] output_img_h = size[1] if self.c_compiler: pass IE_Object = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) input_fmt = 'RGB' IE_Object.ini_image(image=image, input_format=input_fmt, output_format=input_fmt, output_size=size) IE_Object.pack_img(IE_Object.image) command = f'RGBA_{input_img_w}x{input_img_h}_resizeXrotate_RGBA_rotate_{degree}_resize_{output_img_w}x{output_img_h}' os.system(f'cd {self.CSIM_path};./kdp530_test 5 {command}') IE_Object.runCSIM() image_out = IE_Object.load_image(img_w=IE_Object.output_img_w, img_h=IE_Object.output_img_h, fmt=IE_Object.output_format, hex_order=IE_Object.hex_order_load_from_ie, output_3ch=IE_Object.output_3ch) return image_out else: scale_w = output_img_w / input_img_w scale_h = output_img_h / input_img_h matrix = [[scale_w,0,0],[0,scale_h,0]] ## rotate part assert(degree == 90 or degree == 180 or degree == 270) ## tile analysis tile_size_w = 64 tile_size_h = 64 if scale_w <= 1: tile_size_w = (int(64 * scale_w)//2) * 2 if scale_h <= 1: tile_size_h = (int(64 * scale_h)//2) * 2 print('tile_size:',tile_size_w,tile_size_h) if (tile_size_w < 2) & (tile_size_h < 2): assert False, 'tile_size too small' ##-------main flow--------## ## Step 1: dewarp IE_Object_step1 = IE_funcs.IEFuncBase(CSIM_path=self.CSIM_path, bypass_csim = self.bypass_csim) IE_Object_step1.ini_image(image=image, input_format=input_fmt, output_format=input_fmt, output_size=size) IE_Object_step1.pack_img(IE_Object_step1.image) ## RDMA IE_Object_step1.tile_analysis(tile_size=(tile_size_w,tile_size_h), matrix=matrix) ## Step 2: Rotate IE_Object_step2 = copy.deepcopy(IE_Object_step1) IE_Object_step2.input_img_w = IE_Object_step1.output_img_w IE_Object_step2.input_img_h = IE_Object_step1.output_img_h for i in range(len(IE_Object_step1.tiles)): for j in range(len(IE_Object_step1.tiles[i])): IE_Object_step2.tiles[i][j]['rdma'] = IE_Object_step1.tiles[i][j]['wdma'] ## WDMA self.m_rotate.calculate_wdma(degree=degree,IE_Object=IE_Object_step2) ##-----instructions flow------## ## get instructions cnt = 0 mem = 0 instructions = [] for i in range(IE_Object_step1.tile_h_cnt): for j in range(IE_Object_step1.tile_w_cnt): # dewarping instruction, mem = self.m_dewarping.compile( tile=IE_Object_step1.tiles[i][j], cnt=cnt, mem=mem, IE_Object=IE_Object_step1, multimodule_state='start') instructions += instruction # rotate instruction, mem = self.m_rotate.compile( tile=IE_Object_step2.tiles[i][j], cnt=cnt, mem=mem, IE_Object=IE_Object_step2, degree = degree, multimodule_state='end') instructions += instruction cnt += 1 ## write instructions IE_Object_step2.write_instructions(instructions) ## run Csim IE_Object_step2.runCSIM() ## load_image image_out = IE_Object_step2.load_image(img_w=IE_Object_step2.output_img_w, img_h=IE_Object_step2.output_img_h, fmt=IE_Object_step2.output_format, hex_order=IE_Object_step2.hex_order_load_from_ie, output_3ch=IE_Object_step2.output_3ch) return image_out