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 predict_camera(self, configs): """ using camera to predict image :param configs: :return: None """ # for serial communication # camera_serial = SerialThread('serial_thread') # camera_serial.start() self.video_thread.start() init_flag = True while True: frame_read = self.video_thread.get_image() if frame_read is None: print("[ERROR] fail to read frame...") break else: print("[INFO] camera prediction running...") if init_flag: init_flag = False continue image = self.preprocess_image(frame_read, configs) self.tensor.data = pm.PaddleBuf(image) paddle_data_feeds = [self.tensor] outputs = self.predictor.Run(paddle_data_feeds) output = np.array(outputs[0], copy=False) # for test self.show_result_in_console(frame_read, output, configs['threshold'])
def predict_video(self, configs): """ read a video to precdict :param configs: detection configuration :return: None """ self.video_thread.start() init_flag = True while True: frame = self.video_thread.get_image() if frame is None: print('[INFO] fail to read frame ...') break if init_flag: print('[INFO] prediction is running ...') init_flag = False image = self.preprocess_image(frame, configs) self.tensor.data = pm.PaddleBuf(image) paddle_data_feeds = [self.tensor] outputs = self.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) # final # todo: write the result per frame to new video # for test self.show_result_in_console(frame, output, configs['threshold'])
def predict_image(self, configs): """ read a image to predict :param configs: detection configuration :return: None """ image = self.read_image(configs) input = self.preprocess_image(image, configs) self.tensor.data = pm.PaddleBuf(input) paddle_data_feeds = [self.tensor] print('prediction is running ...') outputs = self.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('\nprediction result :') print('\t nDim: ' + str(output.ndim)) print('\tShape: ' + str(output.shape)) print('\tDType: ' + str(output.dtype)) image = self.read_image(configs) self.draw_results(image, output, configs['threshold'])
def predict(self, input_data): ''' PaddleMobile模型预测 参数:输入数据张量、图像数据、预测器 返回:模型预测结果 ''' self.tensor.data = pm.PaddleBuf(input_data) paddle_data_feeds = [self.tensor] outputs = self.predictor.Run(paddle_data_feeds) result = np.array(outputs[0]) return result
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
def predict(self, image): """ PaddleMobile模型预测 :param image: 图像数据 :return: 模型预测结果 """ image = self.preprocess_image(image) self.tensor.data = pm.PaddleBuf(image) paddle_data_feeds = [self.tensor] outputs = self.predictor.Run(paddle_data_feeds) result = np.array(outputs[0]) # if result[0] == -1.0: # return [] height, width, _ = image.shape boxes = self.convert_predict_result(result, height, width) return boxes
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])