def predict(configs, detection): global predictor width = configs['input_width'] height = configs['input_height'] image = read_image(configs) input = preprocess_image(image, configs) tensor = pm.PaddleTensor() tensor.dtype = pm.PaddleDType.FLOAT32 tensor.shape = (1, 3, width, height) tensor.data = pm.PaddleBuf(input) paddle_data_feeds = [tensor] print('prediction is running ...') outputs = predictor.Run(paddle_data_feeds) assert len( outputs ) == 1, 'error numbers of tensor returned from Predictor.Run function !!!' output = np.array(outputs[0], copy=False) print('output:', output) print('\nprediction result :') print('\t nDim: ' + str(output.ndim)) print('\tShape: ' + str(output.shape)) print('\tDType: ' + str(output.dtype)) # print(output) # print('') if detection: detect(output, configs) else: classify(output, configs)
def init_tensor(self, data_shape): """ 初始化PaddleMobile模型输入数据张量 :param data_shape: 数据形状 :return: 数据张量 """ tensor = pm.PaddleTensor() tensor.dtype = pm.PaddleDType.FLOAT32 tensor.shape = data_shape return tensor
def data_feed(self, data_shape): ''' 初始化PaddleMobile模型输入数据张量 参数:数据形状 返回:数据张量 ''' tensor = pm.PaddleTensor() tensor.dtype = pm.PaddleDType.FLOAT32 tensor.shape = (data_shape) return tensor
def tensor_deal(self, origin): tensor_img = origin.resize((256, 256), Image.BILINEAR) if tensor_img.mode != 'RGB': tensor_img = tensor_img.convert('RGB') tensor_img = np.array(tensor_img).astype('float32').transpose( (2, 0, 1)) tensor_img -= 127.5 tensor_img *= 0.007843 tensor_img = tensor_img[np.newaxis, :] tensor = pm.PaddleTensor() tensor.dtype = pm.PaddleDType.FLOAT32 tensor.shape = (1, 3, 256, 256) tensor.data = pm.PaddleBuf(tensor_img) paddle_data_feeds = [tensor] return paddle_data_feeds
predictor = load_model() while True: # 保存图像 调试用 select.select((video,), (), ()) image_data = video.read_and_queue() frame = cv2.imdecode(np.frombuffer(image_data, dtype=np.uint8), cv2.IMREAD_COLOR) cv2.imwrite("test.jpg", frame) # 图像预处理 origin, img = dataset(video) tensor_img = origin.resize((244, 244), Image.BILINEAR) if tensor_img.mode != 'RGB': tensor_img = tensor_img.convert('RGB') tensor_img = np.array(tensor_img).astype('float32').transpose((2, 0, 1)) # HWC to CHW tensor = pm.PaddleTensor() tensor.dtype =pm.PaddleDType.FLOAT32 tensor.shape = (1,1,244,244) tensor.data = pm.PaddleBuf(tensor_img) paddle_data_feeds = [tensor] # 将图像输入神经网络并获得输出 outputs = predictor.Run(paddle_data_feeds) # 处理输出结果并返回最终预测值 output = np.array(outputs[0], copy = False) # print(output) number = np.argsort(output) print("The number is ", number[0][-1])
def init_tensor(self, data_shape): tensor = pm.PaddleTensor() tensor.dtype = pm.PaddleDType.FLOAT32 tensor.shape = data_shape return tensor