def infer_one_picture(self, picture): if cfg.mode != 0: print( "视屏流推理未关闭,seg单图片处理同时将持续处理视屏流影响速度,建议将selfDriverInEuroTruck/Road/config.py中mode改成0" ) return palette = get_palette(cfg.class_num) # 数据获取 ori_img = picture # 用来处理的图 src = picture # 存原图 image = self.preprocess(ori_img) im_shape = ori_img.shape[:2] # 模型预测 result = self.exe.run(program=self.test_prog, feed={self.feed_name[0]: image}, fetch_list=self.fetch_list, return_numpy=True) parsing = np.argmax(result[0][0], axis=0) parsing = cv2.resize(parsing.astype(np.uint8), im_shape[::-1]) # 预测结果 output_im = PILImage.fromarray(np.asarray(parsing, dtype=np.uint8)) output_im.putpalette(palette) arr_infer = np.asarray(output_im) arr_src = np.asarray(src) # 覆盖原图 arr_src[np.where(arr_infer == 13)] = 125 arr_src[np.where(((arr_infer != 0) & (arr_infer != 13)))] = 255 # 写入本地 return arr_src
def infer(self): if not os.path.exists(cfg.vis_dir): os.makedirs(cfg.vis_dir) palette = get_palette(cfg.class_num) while True: # 数据获取 ori_img = self.imm src = self.imm image = self.preprocess(ori_img) im_shape = ori_img.shape[:2] #模型预测 last_time = time.time() result = self.exe.run(program=self.test_prog, feed={self.feed_name[0]: image}, fetch_list=self.fetch_list, return_numpy=True) print("推理延迟:" + str(round(time.time() - last_time, 2))) parsing = np.argmax(result[0][0], axis=0) parsing = cv2.resize(parsing.astype(np.uint8), im_shape[::-1]) # 预测结果 output_im = PILImage.fromarray(np.asarray(parsing, dtype=np.uint8)) output_im.putpalette(palette) arr_infer = np.asarray(output_im) arr_src = np.asarray(src) arr_src[np.where(arr_infer == 13)] = 125 arr_src[np.where(((arr_infer != 0) & (arr_infer != 13)))] = 255 #img = cv2.cvtColor(arr2, cv2.COLOR_RGB2BGR) # 用来显示的图片 cv2.imshow("cvPicture", arr_src) cv2.waitKey(3)
def infer(): if not os.path.exists(cfg.vis_dir): os.makedirs(cfg.vis_dir) palette = get_palette(cfg.class_num) # 人像分割结果显示阈值 thresh = 120 place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) # 加载预测模型 test_prog, feed_name, fetch_list = fluid.io.load_inference_model( dirname=cfg.model_path, executor=exe, params_filename='__params__') #加载预测数据集 test_dataset = TestDataSet() data_num = test_dataset.data_num for idx in range(data_num): # 数据获取 ori_img, image, im_name, im_shape = test_dataset.get_data(idx) if image is None: print(im_name, 'is None') continue # 预测 if cfg.example == 'ACE2P': # ACE2P模型使用多尺度预测 reader = importlib.import_module(args.example+'.reader') multi_scale_test = getattr(reader, 'multi_scale_test') parsing, logits = multi_scale_test(exe, test_prog, feed_name, fetch_list, image, im_shape) else: # HumanSeg,RoadLine模型单尺度预测 result = exe.run(program=test_prog, feed={feed_name[0]: image}, fetch_list=fetch_list) parsing = np.argmax(result[0][0], axis=0) parsing = cv2.resize(parsing.astype(np.uint8), im_shape[::-1]) # 预测结果保存 result_path = os.path.join(cfg.vis_dir, im_name + '.png') if cfg.example == 'HumanSeg': logits = result[0][0][1]*255 logits = cv2.resize(logits, im_shape[::-1]) ret, logits = cv2.threshold(logits, thresh, 0, cv2.THRESH_TOZERO) logits = 255 *(logits - thresh)/(255 - thresh) # 将分割结果添加到alpha通道 rgba = np.concatenate((ori_img, np.expand_dims(logits, axis=2)), axis=2) cv2.imwrite(result_path, rgba) else: output_im = PILImage.fromarray(np.asarray(parsing, dtype=np.uint8)) output_im.putpalette(palette) output_im.save(result_path) if idx % 100 == 0: print('%d processd' % (idx)) print('%d processd done' % (idx)) return 0
def infer(): if not os.path.exists(cfg.vis_dir): os.makedirs(cfg.vis_dir) startup_prog = fluid.Program() test_prog = fluid.Program() input, output = get_model(test_prog, startup_prog) test_prog = test_prog.clone(for_test=True) place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) exe.run(startup_prog) # 加载预测模型 def if_exist(var): return os.path.exists(os.path.join(cfg.model_path, var.name)) fluid.io.load_vars(exe, cfg.model_path, main_program=test_prog, predicate=if_exist) #加载预测数据集 test_dataset = TestDataSet() data_num = test_dataset.data_num for idx in range(data_num): # 数据获取 image, im_name, im_shape = test_dataset.get_data(idx) if image is None: print(im_name, 'is None') continue # 预测 outputs = exe.run(program=test_prog, feed={'image': image}, fetch_list=output) instance_map, predictions = cluster.cluster(outputs[0][0], n_sigma=cfg.n_sigma, \ min_pixel=cfg.min_pixel, threshold=cfg.threshold) # 预测结果保存 instance_map = pad_img(instance_map, image.shape[2:]) instance_map = instance_map[:im_shape[0], :im_shape[1]] output_im = PILImage.fromarray(np.asarray(instance_map, dtype=np.uint8)) palette = get_palette(len(predictions) + 1) output_im.putpalette(palette) result_path = os.path.join(cfg.vis_dir, im_name + '.png') output_im.save(result_path) if (idx + 1) % 100 == 0: print('%d processd' % (idx + 1)) print('%d processd done' % (idx + 1)) return 0
def segOnePicture(self, img): palette = get_palette(self.cfg.class_num) # 数据获取 ori_img = img # 用来处理的图 image = self.preprocess(ori_img) im_shape = ori_img.shape[:2] # 模型预测 result = self.exe.run(program=self.test_prog, feed={self.feed_name[0]: image}, fetch_list=self.fetch_list, return_numpy=True) parsing = np.argmax(result[0][0], axis=0) parsing = cv2.resize(parsing.astype(np.uint8), im_shape[::-1]) # 预测结果 output_im = PILImage.fromarray(np.asarray(parsing, dtype=np.uint8)) output_im.putpalette(palette) res = cv2.cvtColor(np.asarray(output_im) * 255, cv2.COLOR_RGB2BGR) # PIL转cv return res
def infer_pictures(self, readPath, savePath): if cfg.mode != 0: print( "视屏流推理未关闭,seg处理同时将持续处理视屏流不会退出脚本,建议将selfDriverInEuroTruck/Road/config.py中mode改成0" ) return palette = get_palette(cfg.class_num) img_list = os.listdir(readPath) for item in img_list: # 数据获取 print("处理" + item + "中") ori_img = cv2.imread(readPath + item) # 用来处理的图 src = cv2.imread(readPath + item) # 存原图 image = self.preprocess(ori_img) im_shape = ori_img.shape[:2] # 模型预测 result = self.exe.run(program=self.test_prog, feed={self.feed_name[0]: image}, fetch_list=self.fetch_list, return_numpy=True) parsing = np.argmax(result[0][0], axis=0) parsing = cv2.resize(parsing.astype(np.uint8), im_shape[::-1]) # 预测结果 output_im = PILImage.fromarray(np.asarray(parsing, dtype=np.uint8)) output_im.putpalette(palette) arr_infer = np.asarray(output_im) arr_src = np.asarray(src) # 覆盖原图 arr_src[np.where(arr_infer == 13)] = 125 arr_src[np.where(((arr_infer != 0) & (arr_infer != 13)))] = 255 # 写入本地 cv2.imwrite(savePath + item, arr_src)
def infer_vedio(self): palette = get_palette(cfg.class_num) # 加载预测数据集 cap = cv2.VideoCapture("./vedio/record.avi") # 打开视频 size = (self.right - self.left, self.bottom - self.top) # 需要转为视频的图片的尺寸 fourcc = cv2.VideoWriter_fourcc(*'XVID') # 编码格式 video = cv2.VideoWriter("./vedio/" + cfg.model + '_result.avi', fourcc, 16, size) while cap.isOpened(): # 数据获取 ret, fram = cap.read() ori_img = fram if ori_img is None: return image = self.preprocess(ori_img) im_shape = ori_img.shape[:2] # HumanSeg,RoadLine模型单尺度预测 start_time = time.time() result = self.exe.run(program=self.test_prog, feed={self.feed_name[0]: image}, fetch_list=self.fetch_list, return_numpy=True) print("推理延迟:" + str(round(time.time() - start_time, 2))) parsing = np.argmax(result[0][0], axis=0) parsing = cv2.resize(parsing.astype(np.uint8), im_shape[::-1]) # 预测结果保存 output_im = PILImage.fromarray(np.asarray(parsing, dtype=np.uint8)) output_im.putpalette(palette) img = cv2.cvtColor(np.asarray(output_im) * 255, cv2.COLOR_RGB2BGR) # PIL转cv video.write(img) cv2.imshow("2", img) cv2.waitKey(1)
print("Image used: #%d (label=%d)" % (img_to_visualize, np.argmax(Y_train[img_to_visualize]))) print(np.max(fmap_Y_pre), np.min(fmap_Y_post)) #save original image and yuv channels Ychannel = X_train[img_to_visualize, 0, :, :] Ychannel = Ychannel + np.abs(np.min(Ychannel)) scipy.misc.imsave('imageY.jpg', X_train[img_to_visualize, 0, :, :]) scipy.misc.imsave('imageU.jpg', X_train[img_to_visualize, 1, :, :]) scipy.misc.imsave('imageV.jpg', X_train[img_to_visualize, 2, :, :]) scipy.misc.imsave( 'image.jpg', np.flipud(np.rot90(np.transpose(X_train_rgb[img_to_visualize])))) #get palette for colored combination of feature map layers palette_Y = get_palette(depthY) palette_UV = get_palette(depthUV) #initialize all combined feature maps fmap_Y_pre_combined = np.zeros((16, 16, 3)) fmap_Y_post_combined = np.zeros((16, 16, 3)) fmap_U_pre_combined = np.zeros((16, 16, 3)) fmap_U_post_combined = np.zeros((16, 16, 3)) fmap_V_pre_combined = np.zeros((16, 16, 3)) fmap_V_post_combined = np.zeros((16, 16, 3)) #combine for Y channel for i in range(depthY): fmap_pre_slice = fmap_Y_pre[0][i] fmap_pre_slice_color = np.repeat( np.expand_dims(fmap_pre_slice, axis=2), 3, axis=2) * palette_Y[i]