def PCS(pos, NG=32, L=300): ''' shape of pos is [-1, 3], conrresponding to [x, y, z]''' if not pos.flags['C_CONTIGUOUS']: pos = np.ascontiguous(pos, dtype=pos.dtype) pos_ctypes_ptr = cast(pos.ctypes.data, POINTER(Pos)) Gp = libcd.Griding_PCS(NG, L, len(pos), pos_ctypes_ptr) Grid = np.fromiter(Gp, dtype=np.float64, count=NG**3).astype(np.float32) return Grid
def input(self,in_data): i=0 for _data in in_data: _data=np.array(_data).reshape(-1).astype('int32') assert (len(_data) == self.input_data[0].length), 'input error : required {} not {}'.format(self.input_data[i].length,len(_data)) if not _data.flags['C_CONTIGUOUS']: _data = np.ascontiguous(_data, dtype=_data.dtype) # 如果不是C连续的内存,必须强制转换 data_ctypes_ptr = cast(_data.ctypes.data, POINTER(c_int)) #转换为ctypes,这里转换后的可以直接利用ctypes转换为c语言中的int*,然后在c中使用 #for m in range(len(_data)): #print(_data[m],data_ctypes_ptr[m]) self.NLB_input(data_ctypes_ptr,self.input_data,i,self.p.contents.model) i=i+1
def read(self): img = np.zeros((1728,2304,3),dtype=np.uint8) if not img.flags['C_CONTIGUOUS']: img = np.ascontiguous(img, dtype=img.dtype) img_ctypes_ptr = cast(img.ctypes.data,c_void_p) ret=TUCAM_Buf_WaitForFrame(self.hIdxTUCam,byref(self.mframe)) assert(ret == 1) ret=TUCAM_Buf_CopyFrame(self.hIdxTUCam,byref(self.mframe)) assert(ret == 1) memcpy(img_ctypes_ptr,self.mframe.pBuffer+self.mframe.usOffset,self.mframe.uiImgSize) return img
def load_image_python(imgPIL): # imgPIL = Image.open('tmp.jpg') imgCv = cv2.cvtColor(np.asarray(imgPIL), cv2.COLOR_RGB2BGR) im = IMAGE() im.w = c_int(imgCv.shape[0]) im.h = c_int(imgCv.shape[1]) im.c = c_int(imgCv.shape[2]) imgCv = 1.0 * imgCv / 255.0 # cv2.imshow('dd', imgCv) # cv2.waitKey(0) if not imgCv.flags['C_CONTIGUOUS']: imgCv = np.ascontiguous(imgCv, dtype=imgCv.dtype) # 如果不是C连续的内存,必须强制转换 im.data = cast(imgCv.ctypes.data, POINTER(c_float)) # 转换为ctypes,这里转换后的可以直接利用ctypes转换为c语言中的int*,然后在c中使用 return im
def Cal(Data): #{'Nodes': ,'Links': } Py_N = Data['Nodes'] Py_L = Data['Links'] GSL = ctypes.cdll.LoadLibrary('./后台_01_DLL.dll') GSL.My_C.argtypes = (POINTER(ctypes.c_float), POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_int) Py_Nodes = np.asarray(Py_N, dtype=np.float32) Py_Links = np.asarray(Py_L, dtype=np.float32) Py_Nodes_P = ctypes.cast(Py_Nodes.ctypes.data, POINTER(ctypes.c_float)) Py_Links_P = ctypes.cast(Py_Links.ctypes.data, POINTER(ctypes.c_float)) if not Py_Nodes.flags['C_CONTIGUOUS']: Py_Nodes = np.ascontiguous(Py_Nodes, dtype=Py_Nodes.dtype) if not Py_Links.flags['C_CONTIGUOUS']: Py_Links = np.ascontiguous(Py_Links, dtype=Py_Links.dtype) GSL.My_C(Py_Nodes_P, Py_Links_P, Py_N.shape[0], Py_L.shape[0]) Data2 = {'Nodes': Py_Nodes, 'Links': Py_Links} return Data2
def input(self, in_data): i = 0 for _data in in_data: _data = np.array(_data).reshape(-1).astype('int32') assert (len(_data) == self.input_data[0].length ), 'input error : required {} not {}'.format( self.input_data[i].length, len(_data)) if not _data.flags['C_CONTIGUOUS']: _data = np.ascontiguous(_data, dtype=_data.dtype) # 如果不是C连续的内存,必须强制转换 data_ctypes_ptr = cast(_data.ctypes.data, POINTER( c_int)) #转换为ctypes,这里转换后的可以直接利用ctypes转换为c语言中的int*,然后在c中使用 #for m in range(len(_data)): #print(_data[m],data_ctypes_ptr[m]) self.NLB_input(data_ctypes_ptr, self.input_data, i, self.p.contents.model) i = i + 1
def c_array(ctype, values): t1 = time.time() if not values.flags['C_CONTIGUOUS']: values = np.ascontiguous(values, dtype=values.dtype) arr = (ctype * len(values))() arr[:] = values """ c_p=POINTER(ctype) data=np.array(values) data=data.astype(np.float32) arr=data.ctypes.data_as(c_p) """ t2 = time.time() print "c_array ", (t2 - t1) * 1000, " ms" return arr
def _from_numpy(np_ary, device=None, usm_type="shared"): if type(np_ary) is np.ndarray: if np_ary.flags["FORC"]: x = np_ary else: x = np.ascontiguous(np_ary) R = dpt.usm_ndarray( np_ary.shape, dtype=np_ary.dtype, buffer=usm_type, buffer_ctor_kwargs={ "queue": Device.create_device(device).sycl_queue }, ) R.usm_data.copy_from_host(x.reshape((-1)).view("|u1")) return R else: raise ValueError("Expected numpy.ndarray, got {}".format(type(np_ary)))
def preprocess(self, velo): geom = self.config['geometry'] velo_processed = np.zeros(geom['input_shape'], dtype=np.float32) if self.cdll: # print('11111') num = velo.shape[0] # print('velo.shape[0]', num) if not velo.flags['C_CONTIGUOUS']: velo = np.ascontiguous(velo, dtype=velo.dtype) # 如果不是C连续的内存,必须强制转换 velo_ctypes_ptr = velo.ctypes.data_as( ctypes.POINTER(ctypes.c_float)) c_data = ctypes.c_void_p(velo_processed.ctypes.data) self.LidarLib.createTopViewMaps_ros(c_data, velo_ctypes_ptr, ctypes.c_int(num)) else: print('22222') def passthrough(velo): q = (geom['W1'] < velo[:, 0]) * (velo[:, 0] < geom['W2']) * \ (geom['L1'] < velo[:, 1]) * (velo[:, 1] < geom['L2']) * \ (geom['H1'] < velo[:, 2]) * (velo[:, 2] < geom['H2']) indices = np.where(q)[0] return velo[indices, :] velo = passthrough(velo) velo_processed = np.zeros(geom['input_shape'], dtype=np.float32) intensity_map_count = np.zeros( (velo_processed.shape[0], velo_processed.shape[1])) for i in range(velo.shape[0]): x = int((velo[i, 1] - geom['L1']) / 0.1) y = int((velo[i, 0] - geom['W1']) / 0.1) z = int((velo[i, 2] - geom['H1']) / 0.1) velo_processed[x, y, z] = 1 velo_processed[x, y, -1] += velo[i, 3] intensity_map_count[x, y] += 1 velo_processed[:, :, -1] = np.divide(velo_processed[:, :, -1], intensity_map_count, \ where=intensity_map_count != 0) # print('velo_processed.shape',velo_processed.shape) # print(velo_processed) velo_processed = torch.from_numpy(velo_processed).permute(2, 0, 1).to( self.device) velo_processed.require_grad = False return velo_processed
def run(m): m = __src_resize(m) if not m.flags['C_CONTIGUOUS']: print("df") m = np.ascontiguous(m, dtype=m.dtype) if len(m.shape) != 2: m = cv2.cvtColor(m, cv2.COLOR_BGR2GRAY) info = np.zeros((1, 2), np.uint8) so = ct.cdll.LoadLibrary(SO_PATH) so.get_info(m.ctypes.data_as(ct.POINTER(ct.c_ubyte)), info.ctypes.data_as(ct.POINTER(ct.c_ubyte)), ct.c_int(m.shape[0]), ct.c_int(m.shape[1])) # print(info) num, hig = info[0] wit = m.shape[1] res = [] for i in range(num): res.append([False, np.zeros((hig, wit), np.uint8)]) res[i][0] = so.get_data( res[i][1].ctypes.data_as(ct.POINTER(ct.c_ubyte)), i) return res
ra = float(r0) / float(c0) dst = cv2.resize(src, (int(900 / ra), 900)) elif (c0 >= r0 and c0 > 900): ra = float(c0) / float(r0) dst = cv2.resize(src, (900, int(900 / ra))) else: return src return dst for i in range(1, 55): fn = './binimg/D' + str(i) + '.jpg' img = cv2.imread(fn) # img=cv2.transpose(img) # cv2.imshow("src",img) #print img if not img.flags['C_CONTIGUOUS']: print("not C_CONTIGUOUS") a = np.ascontiguous(img, dtype=img.dtype) # _, img=cv2.threshold(img,175,255,cv2.THRESH_BINARY) a = time.time() res = cla.run(img) print 'time:', time.time() - a dst = cla.show_mats(res) cv2.imshow("dst", dst) cv2.waitKey()
x = c_char_p() # 一级指针 print x, type(x) x.value = 'x' y = pointer(x) # 二级指针 print y, type(y) print x.value print y.contents.value a = np.asarray(range(16), dtype=np.int32).reshape([4,4]) print a.flags if not a.flags['C_CONTIGUOUS']: a = np.ascontiguous(a, dtype=a.dtype) a_ctypes_ptr = cast(a.ctypes.data, POINTER(c_int)) for i in range(16): print a_ctypes_ptr[i] b = np.array(range(10)) print b, b.dtype c = b.reshape(2,-1) print b.reshape(2, -1), c[1][1] print b.shape, c.shape x = (c_int * 4)(1,2,3,4)