def __getitem__(self, idx): im_name = 'DensePoseData/demo_data/xyz.jpg' output_dir = 'DensePoseData/' img_name1 = os.path.join(self.root_dir1, self.landmarks_frame.iloc[idx, 0]) image1 = cv2.imread(img_name1) img_name2 = os.path.join(self.root_dir2, self.landmarks_frame.iloc[idx, 1]) image2 = cv2.imread(img_name2) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( self.model, image1, None, timers=self.timers) im1 = vis_utils.vis_one_image( image1[:, :, ::-1], # BGR -> RGB for visualization im_name, output_dir, cls_boxes, cls_segms, cls_keyps, cls_bodys, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( self.model, image2, None, timers=self.timers) im2 = vis_utils.vis_one_image( image2[:, :, ::-1], # BGR -> RGB for visualization im_name, output_dir, cls_boxes, cls_segms, cls_keyps, cls_bodys, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) image3 = cv2.merge((image1, im1, im2)) sample = {'image1': image1, 'image2': image2, 'image3': image3} if self.transform: sample = self.transform(sample) return sample
def _process(self, im): t = time.time() cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( self.model, im, None, timers=self.timers) self.logger.info('Inference time: {:.3f}s'.format(time.time() - t)) for k, v in self.timers.items(): self.logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) self.logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)') mask_list = vis_utils.vis_one_image_opencv_mask_list( im[:, :, ::-1], # BGR -> RGB for visualization #im_name, #args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=None, #box_alpha=0.3, show_class=True, thresh=self.args["thresh"], kp_thresh=self.args["kp_thresh"], #ext=args.output_ext, #out_when_no_box=args.out_when_no_box ) return np.array(mask_list)
def main(args): logger = logging.getLogger(__name__) merge_cfg_from_file(args.cfg) cfg.TEST.WEIGHTS = args.weights cfg.NUM_GPUS = 1 assert_and_infer_cfg() model = infer_engine.initialize_model_from_cfg(args.weights) dummy_dataset = dummy_datasets.get_coco_dataset() if os.path.isdir(args.im_or_folder): im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext) else: im_list = [args.im_or_folder] for i, im_name in enumerate(im_list): logger.info('Processing {}'.format(im_name)) im = cv2.imread(im_name) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_scores, _, _ = infer_engine.im_detect_all(model, im, None, timers=timers) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) cl = np.argmax(cls_scores) for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) logger.info(' | Class is: {}'.format(dummy_dataset.classes[cl])) logger.info(' | Class Confidance is: {:.2f}%'.format( cls_scores[cl] * 100)) if i == 0: logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)')
def hanle_frame(args, frameId, im, logger, model, dataset): #out_name = os.path.join( # args.output_dir, '{}'.format(frameId + '.pdf') #) logger.info('Processing frame: {}'.format(frameId)) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) if frameId == 1: logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)') vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization '{}'.format(frameId), args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2)
def run_model_cfg(args, im, check_blobs): workspace.ResetWorkspace() model, _ = load_model(args) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = test_engine.im_detect_all( model, im, None, None, ) boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format( cls_boxes, cls_segms, cls_keyps) # sort the results based on score for comparision boxes, segms, keypoints, classes = _sort_results( boxes, segms, keypoints, classes) # write final results back to workspace def _ornone(res): return np.array(res) if res is not None else np.array([], dtype=np.float32) with c2_utils.NamedCudaScope(0): workspace.FeedBlob(core.ScopedName('result_boxes'), _ornone(boxes)) workspace.FeedBlob(core.ScopedName('result_segms'), _ornone(segms)) workspace.FeedBlob(core.ScopedName('result_keypoints'), _ornone(keypoints)) workspace.FeedBlob(core.ScopedName('result_classids'), _ornone(classes)) # get result blobs with c2_utils.NamedCudaScope(0): ret = _get_result_blobs(check_blobs) return ret
def generate_predicitions_from_frames(images, config_file, weights): """Generator yields inferred boxes and keypoints for each image in a provided iterable of images Args: images: iterable images config_file: Detectron configuration file weights: pretrained weights Returns: yields i, im, cls_boxes, cls_segms, cls_keyps """ logger = logging.getLogger(__name__) merge_cfg_from_file(config_file) cfg.NUM_GPUS = 1 weights = cache_url(weights, cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) model = infer_engine.initialize_model_from_cfg(weights) for i, im in enumerate(images): logger.info("Processing frame {}".format(i)) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers ) logger.info("Inference time: {:.3f}s".format(time.time() - t)) for k, v in timers.items(): logger.info(" | {}: {:.3f}s".format(k, v.average_time)) if i == 0: logger.info( "Note: inference on the first image will be slower than the " "rest (caches and auto-tuning need to warm up)" ) yield i, im, cls_boxes, cls_segms, cls_keyps
def infer(self, im, thresh=0.5): ts = time.time() timers = defaultdict(Timer) with c2_utils.NamedCudaScope(self.gpu_id): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( self.__model, im, None, timers=timers) te = time.time() print('det time:', te - ts) for k, v in timers.items(): print(' | {}: {:.3f}s'.format(k, v.average_time)) if isinstance(cls_boxes, list): boxes, segms, keypoints, classes = cvt_cls(cls_boxes, cls_segms, cls_keyps) if boxes is None or boxes.shape[0] == 0 or max(boxes[:, 4]) < thresh: return None heads = list() for i in range(len(boxes)): bbox = boxes[i, :4] score = boxes[i, -1] if score < thresh: continue heads.append([int(x) for x in bbox] + [float(score)]) return heads
def process_image(input_image, num_of_people, num_of_permutations): timers = defaultdict(Timer) t = time.time() image = stringToImage(input_image[input_image.find(",")+1:]) img = toRGB(image) size = img.shape[:2] with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( model, img, None, timers=timers ) t2 = time.time() #logger.info('Inference time: {:.3f}s'.format(t2 - t)) jpgs = [] if cls_bodys is not None: (finals,num_people) = process_bodies(img, cls_boxes, cls_bodys, num_of_people, num_of_permutations) logger.info('Num of people {}'.format(num_people)) if len(finals) > 0: for final in finals: retval, buffer = cv2.imencode('.jpg', final) jpg_as_text = base64.b64encode(buffer) jpgs.append(jpg_as_text) else: print('Skipping') else: print('Skipping') return jpgs
def main(input_img): image = stringToImage(input_img[input_img.find(",") + 1:]) img = toRGB(image) logger.info('Processing {} -> {}'.format('New Image', 'Output...')) timers = defaultdict(Timer) t = time.time() size = img.shape[:2] #img = imresize(img, (320, 240), interp='bilinear') with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( model, img, None, timers=timers) for key, timer in timers.items(): print(key, timer.total_time) t2 = time.time() densepose_img = vis_one_image(img, 'testImage', output_dir, cls_boxes, cls_segms, cls_keyps, cls_bodys, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) logger.info('Inference time: {:.3f}s'.format(t2 - t)) return densepose_img
def getKptByModel(self, image, thresh=0.7): top_labels = [] top_xmin = [] top_ymin = [] top_xmax = [] top_ymax = [] top_scores = [] timers = defaultdict(Timer) # with c2_utils.NamedCudaScope(0): # cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( # self.model, image, None, timers=timers # ) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( self.model, image, None, timers=timers) # boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format(cls_boxes, cls_segms, cls_keyps) image = vis_utils.vis_one_image_opencv(image, cls_boxes, cls_segms, cls_keyps, dataset=None, show_class=True, thresh=0.7, kp_thresh=2) # try: # print (keypoints) # except: # pass return image
def detect(self, frame): timers = defaultdict(Timer) mask_frame = frame with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( self.model, mask_frame, None, timers=timers) # verifica se a imagem deve ser descartada boxes, sgms, keypts, classes = vis_utils.convert_from_cls_format( cls_boxes, cls_segms, cls_keyps) bbox_list = [] if len(boxes) > 0: indexes_big = box_utils.filter_big_boxes(boxes, 300) indexes_small = box_utils.filter_small_boxes(boxes, 100) for i in range(len(boxes)): if (i in indexes_big and i in indexes_small): if classes[i] in [1, 2, 3] and boxes[i, 4] > 0.7: box = boxes[i] bbox_list.append([ int(box[0]), int(box[1]), int(box[2]) - int(box[0]), int(box[3]) - int(box[1]) ], classes[i]) # mask_frame = vis_utils.vis_one_image_opencv(mask_frame, cls_boxes, cls_segms, cls_keyps, thresh=0.8, kp_thresh=2, # show_box=True, dataset=CocoNames, show_class=True) #, hiden_indexes=True, indexes_shown=[1]) return bbox_list
def main(args): logger = logging.getLogger(__name__) dummy_coco_dataset = dummy_datasets.get_coco_dataset() cfg_orig = load_cfg(envu.yaml_dump(cfg)) im = cv2.imread(args.im_file) if args.rpn_pkl is not None: proposal_boxes, _proposal_scores = get_rpn_box_proposals(im, args) workspace.ResetWorkspace() else: proposal_boxes = None cls_boxes, cls_segms, cls_keyps, cls_bodys = None, None, None, None for i in range(0, len(args.models_to_run), 2): pkl = args.models_to_run[i] yml = args.models_to_run[i + 1] cfg.immutable(False) merge_cfg_from_cfg(cfg_orig) merge_cfg_from_file(yml) if len(pkl) > 0: weights_file = pkl else: weights_file = cfg.TEST.WEIGHTS cfg.NUM_GPUS = 1 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(weights_file) with c2_utils.NamedCudaScope(0): cls_boxes_, cls_segms_, cls_keyps_ , cls_bodys_= \ model_engine.im_detect_all(model, im, proposal_boxes) cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps cls_bodys = cls_bodys_ if cls_bodys_ is not None else cls_bodys workspace.ResetWorkspace() out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(args.im_file) + '.pdf')) logger.info('Processing {} -> {}'.format(args.im_file, out_name)) with open('test_vis.pkl', 'w') as f: pickle.dump( { 'im': im, 'cls_boxes': np.array(cls_boxes), 'cls_bodys': np.array(cls_bodys) }, f) vis_utils.vis_one_image(im[:, :, ::-1], args.im_file, args.output_dir, cls_boxes, cls_segms, cls_keyps, cls_bodys, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2)
def extract_iuv(image_path, model, infer_engine): im = cv2.imread(image_path) dummy_coco_dataset = dummy_datasets.get_coco_dataset() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( model, im, None ) iuv_out = vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization None, None, cls_boxes, cls_segms, cls_keyps, cls_bodys, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2 ) return iuv_out
def single_process(args, dummy_coco_dataset, im, im_name, model): timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) # if i == 0: # logger.info( # ' \ Note: inference on the first image will be slower than the ' # 'rest (caches and auto-tuning need to warm up)' # ) print("预测结果:", cls_boxes, cls_segms, cls_keyps) new_img = vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=args.thresh, kp_thresh=args.kp_thresh, ext=args.output_ext, out_when_no_box=args.out_when_no_box) return new_img
def process_images( args_weights, args_im_or_folder, args_image_ext ): assert not cfg.MODEL.RPN_ONLY, \ 'RPN models are not supported' assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \ 'Models that require precomputed proposals are not supported' model = infer_engine.initialize_model_from_cfg(args_weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() if os.path.isdir(args_im_or_folder): im_list = glob.iglob(args_im_or_folder + '/*.' + args_image_ext) else: im_list = [args_im_or_folder] for i, im_name in enumerate(im_list): im = cv2.imread(im_name) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers ) boxes, segms, keyps, classes = vis_utils.convert_from_cls_format(cls_boxes, cls_segms, cls_keyps) if classes is not None: class_strs = [dummy_coco_dataset.classes[c] for c in classes] im_name = im_name.split('/')[-1].split('.'+args_image_ext)[0] yield im_name, boxes, segms, class_strs, im.shape[:2]
def run_model_cfg(args, im, check_blobs): workspace.ResetWorkspace() model, _ = load_model(args) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = test_engine.im_detect_all( model, im, None, None, ) boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format( cls_boxes, cls_segms, cls_keyps) # sort the results based on score for comparision boxes, segms, keypoints, classes = _sort_results( boxes, segms, keypoints, classes) # write final results back to workspace def _ornone(res): return np.array(res) if res is not None else np.array([], dtype=np.float32) with c2_utils.NamedCudaScope(0): workspace.FeedBlob(core.ScopedName('result_boxes'), _ornone(boxes)) workspace.FeedBlob(core.ScopedName('result_segms'), _ornone(segms)) workspace.FeedBlob(core.ScopedName('result_keypoints'), _ornone(keypoints)) workspace.FeedBlob(core.ScopedName('result_classids'), _ornone(classes)) # get result blobs with c2_utils.NamedCudaScope(0): ret = _get_result_blobs(check_blobs) return ret
def process_image(input_file, output_file): #image = stringToImage(input_img[input_img.find(",")+1:]) #img = cv2.imread('stylegan/narrow/13.test.jpg') #img = cv2.imread('stylegan/curated/231.jpg') img = cv2.imread(input_file) #img = toRGB(image) #logger.info(input_file) timers = defaultdict(Timer) t = time.time() size = img.shape[:2] with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( model, img, None, timers=timers) t2 = time.time() #logger.info('Inference time: {:.3f}s'.format(t2 - t)) if cls_bodys is not None: (final, num_people) = process_bodies(img, cls_boxes, cls_bodys) if final is not None: if num_people > 4: output_file = output_file + 'X.jpg' else: output_file = output_file + '.jpg' cv2.imwrite(output_file, final) return True else: #print('Skipping') return False else: #print('Skipping') return False
def extract_person(image, background_image, model, dataset): timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, image, None, timers=timers) boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format( cls_boxes, cls_segms, cls_keyps, ) masks = mask_util.decode(segms) masks = np.moveaxis(masks, 2, 0) output_image = np.copy(background_image) for box, mask, c, in zip(boxes, masks, classes): score = box[-1] if score < 0.9: continue if dataset.classes[c] != 'person': continue idx = np.where(mask != 0) output_image[idx[0], idx[1], :] = image[idx[0], idx[1], :] return output_image
def get_detections(self, image): cls_boxes, cls_depths, cls_segms, cls_keyps = infer_engine.im_detect_all( self.model, image, None) boxes, depths, _segms, _keyps, classes = convert_from_cls_format( cls_boxes, cls_depths, None, None) detections = [] for i in range(len(classes)): detection = {} detection["bbox"] = boxes[i, :4] detection["score"] = boxes[i, -1] detection["depth"] = depths[i] detection["category_id"] = classes[i] if detection["score"] > self.cla_thresholds[self.classnames[ detection["category_id"]]]: detections.append(detection) if self.filter_detections: filter_inside_boxes(detections, inside_ratio_thresh=self.inside_box_ratio) return detections
def main(input_img, with_pix2pix=False): image = stringToImage(input_img[input_img.find(",") + 1:]) img = toRGB(image) logger.info('Processing {} -> {}'.format('New Image', 'Output...')) timers = defaultdict(Timer) t = time.time() size = img.shape[:2] with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( model, img, None, timers=timers) for key, timer in timers.items(): print(key, timer.total_time) t2 = time.time() r = vis_one_image(img, 'testImage', output_dir, cls_boxes, cls_segms, cls_keyps, cls_bodys, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) t3 = time.time() if with_pix2pix: r = requests.post(pix2pixURL, data={'data': r}) logger.info('Inference time: {:.3f}s'.format(t2 - t)) logger.info('Visualization time: {:.3f}s'.format(t3 - t2)) logger.info('Pix2pix time: {:.3f}s'.format(time.time() - t3)) return r
def find_optimal_bars(image_directory, layer): model = get_model() im_list = glob.iglob(image_directory + '/*.jpg') im_list = [x for x in im_list] # so we can get length max_sd_per_map = [None] * len(im_list) for i, im_name in enumerate(im_list): print('processing {}'.format(im_name)) im = cv2.imread(im_name) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=None) responses = workspace.blobs['gpu_0/{}'.format(layer)] sd = [] for i in range(responses.shape[0]): sd.append(np.std(responses[i, :, :, :])) max_sd_across_boxes = responses[np.argmax(sd), :, :, :] # stimuli aren't processed in order, so get index from file name bar_name = os.path.basename(im_name)[3:-4] bar_num = int(bar_name) max_sd_per_map[bar_num] = np.max(np.max(max_sd_across_boxes, axis=-1), axis=-1) max_sd_per_map = np.array(max_sd_per_map) result = np.argmax(max_sd_per_map, axis=0) print result # TODO: check this return result
def main(args): logger = logging.getLogger(__name__) merge_cfg_from_file(args.cfg) cfg.NUM_GPUS = 1 args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) assert not cfg.MODEL.RPN_ONLY, \ 'RPN models are not supported' assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \ 'Models that require precomputed proposals are not supported' model = infer_engine.initialize_model_from_cfg(args.weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() if os.path.isdir(args.im_or_folder): im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext) else: im_list = [args.im_or_folder] for i, im_name in enumerate(im_list): output_image_dir = os.path.join(args.output_dir, 'images') out_name = os.path.join( output_image_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext)) logger.info('Processing {} -> {}'.format(im_name, out_name)) im = cv2.imread(im_name) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) if i == 0: logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)') # vis_utils.vis_one_image( # im[:, :, ::-1], # BGR -> RGB for visualization # im_name, # output_image_dir, # cls_boxes, # cls_segms, # cls_keyps, # dataset=dummy_coco_dataset, # box_alpha=0.3, # show_class=True, # thresh=0.7, # kp_thresh=2, # ext=args.output_ext, # out_when_no_box=args.out_when_no_box #) _write_to_txt(cls_boxes, cls_segms, cls_keyps, im_name, dummy_coco_dataset)
def infer_image(self, image): #print(len(image)) if isinstance(image, str): image = cv2.imread(image) with c2_utils.NamedCudaScope(0): boxes, segments, _ = infer_engine.im_detect_all(self.__model, image, None) return boxes, segments
def main(args): logger = logging.getLogger(__name__) merge_cfg_from_file(args.cfg) cfg.NUM_GPUS = 1 args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) model = infer_engine.initialize_model_from_cfg(args.weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() submit_result = [] result_file_name = 'detectron_val_result.txt' if os.path.isdir(args.im_or_folder): im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext) else: im_list = [args.im_or_folder] for i, im_name in enumerate(im_list): out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(im_name) + '.pdf')) logger.info('Processing {} -> {}'.format(im_name, out_name)) im = cv2.imread(im_name) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) if i == 0: logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)') result = vis_utils.vis_one_image_bbox( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) if result: submit_result.extend(result) logger.info('Image {}.'.format(i)) # Write file with open(result_file_name, 'wb') as result_file: for item in submit_result: result_file.write("%s\n" % item) logger.info( 'The result file has been written in {}.'.format(result_file_name))
def main(args): logger = logging.getLogger(__name__) merge_cfg_from_file(args.cfg) cfg.NUM_GPUS = 4 args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) assert not cfg.MODEL.RPN_ONLY, \ 'RPN models are not supported' assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \ 'Models that require precomputed proposals are not supported' model = infer_engine.initialize_model_from_cfg(args.weights) # model = infer_engine.initialize_model_from_cfg(args.weights,gpu_id=3) dummy_coco_dataset = dummy_datasets.get_coco_dataset() if os.path.isdir(args.im_or_folder): im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext) else: im_list = [args.im_or_folder] len_str = str(len(os.listdir(args.im_or_folder))) for i, im_name in enumerate(im_list): print('~~~~~~~~~~~~~~~~~' + str(i) + '/' + len_str + "~~~~~~~~~~~~~~~~~~~~~") out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(im_name) + '.jpg')) logger.info('Processing {} -> {}'.format(im_name, out_name)) im = cv2.imread(im_name) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( #此处输出的cls_boxes包含类别信息 model, im, None, timers=timers) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) if i == 0: logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)') # ipdb.set_trace() # print(cls_boxes) # print(cls_segms) # print(cls_keyps) vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.5, kp_thresh=2, ext='jpg')
def main(args): logger = logging.getLogger(__name__) dummy_nucoco_dataset = dummy_datasets.get_nucoco_dataset() cfg_orig = load_cfg(envu.yaml_dump(cfg)) ## Load image coco = COCO_PLUS(args.ann_file, args.imgs_dir) image_id = coco.dataset['images'][args.im_ind]['id'] img_path = os.path.join(args.imgs_dir, coco.imgs[image_id]["file_name"]) im = cv2.imread(img_path) ## Get the proposals for this image proposals = rrpn_loader(args.rpn_pkl) proposal_boxes = proposals[image_id]['boxes'] _proposal_scores = proposals[image_id]['scores'] workspace.ResetWorkspace() ## run models cls_boxes, cls_segms, cls_keyps = None, None, None for i in range(0, len(args.models_to_run), 2): pkl = args.models_to_run[i] yml = args.models_to_run[i + 1] cfg.immutable(False) merge_cfg_from_cfg(cfg_orig) merge_cfg_from_file(yml) if len(pkl) > 0: weights_file = pkl else: weights_file = cfg.TEST.WEIGHTS cfg.NUM_GPUS = 1 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(weights_file) with c2_utils.NamedCudaScope(0): cls_boxes_, cls_segms_, cls_keyps_ = \ model_engine.im_detect_all(model, im, proposal_boxes) cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps workspace.ResetWorkspace() out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(img_path) + '.pdf') ) logger.info('Processing {} -> {}'.format(img_path, out_name)) vis_utils.vis_one_image( im[:, :, ::-1], img_path, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_nucoco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2 )
def main(args): #logger = logging.getLogger(__name__) merge_cfg_from_file(args.cfg) cfg.NUM_GPUS = 1 args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) model = infer_engine.initialize_model_from_cfg(args.weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() cam = cv2.VideoCapture(0) # Set Texture Tex_Atlas = cv2.imread( 'DensePoseData/demo_data/texture_from_SURREAL.png')[:, :, ::-1] / 255.0 #Tex_Atlas = cv2.imread('DensePoseData/demo_data/texture_atlas_200.png')[:,:,::-1] TextureIm = np.zeros([24, 200, 200, 3]) # for i in range(4): for j in range(6): TextureIm[(6 * i + j), :, :, :] = Tex_Atlas[(200 * j):(200 * j + 200), (200 * i):(200 * i + 200), :] while True: if cv2.waitKey(1) & 0xFF == ord('q'): break retval, im = cam.read() #imsmall = cv2.resize(im, None, fx=0.5, fy=0.5) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( model, im, None, timers=None) iuvout, indsout = vis_utils.vis_webcam(im, cls_boxes, cls_segms, cls_keyps, cls_bodys, thresh=0.9, kp_thresh=2, dataset=dummy_coco_dataset, show_class=True) iuvout, indsout = vis_utils.vis_webcam( im, boxes=cls_boxes, segms=cls_segms, keypoints=cls_keyps, body_uv=cls_bodys, thresh=0.9, kp_thresh=2, dpi=200, box_alpha=0.3, dataset=dummy_coco_dataset, show_class=True, ) #cv2.imshow('input', im) texout = TransferTexturePure(TextureIm, im, iuvout) cv2.imshow('output', texout)
def hanle_frame(args, frameId, origin_im, im, logger, model, dataset, file_name): global predict_time, process_time, show_img logger.info('Processing frame: {}'.format(frameId)) # cv2.imshow("tmplog", im) # cv2.waitKey(0) timers = defaultdict(Timer) t = time.time() im = im[:, :, ::-1] with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers) predict_time.append(time.time() - t) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) logger.info('predict_time: {:.3f}s'.format(np.mean( np.array(predict_time)))) # for k, v in timers.items(): # logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) if frameId == 1: logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)') t = time.time() img_debug = True ret = extractor.get_detection_line(im, cls_boxes, cls_segms, cls_keyps, dataset=dataset, show_class=True, thresh=0.8, kp_thresh=2, frame_id=frameId, img_debug=img_debug) im, mid_im, top_im, result, fork_pos = ret process_time.append(time.time() - t) logger.info('get_detection_line time: {:.3f}s'.format(time.time() - t)) # logger.info('process_time: {:.3f}s'.format(np.mean( np.array(process_time)))) line_list = None cache_list = None particles = None filter_list = None if not result is None: line_list, cache_list, filter_list, particles = add2MsgQueue( result, frameId, fork_pos, img_debug) g_debug_img_queue.put( (origin_im[:, :, ::-1], im, mid_im, top_im, line_list, cache_list, filter_list, frameId, fork_pos, file_name)) if g_debug_img_queue.full(): try: g_debug_img_queue.get_nowait() except Empty: print("Queue.Empty")
def get_iuv(self, img): timers = defaultdict(Timer) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( self.model, img, None, timers=timers) iuv = self.make_iuv(img, cls_boxes, cls_segms, cls_keyps, cls_bodys) if iuv is None: raise Exception("Nothing found") return iuv
def main(args): logger = logging.getLogger(__name__) merge_cfg_from_file(args.cfg) cfg.NUM_GPUS = 1 args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) assert not cfg.MODEL.RPN_ONLY, \ 'RPN models are not supported' assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \ 'Models that require precomputed proposals are not supported' model = infer_engine.initialize_model_from_cfg(args.weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() if os.path.isdir(args.im_or_folder): im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext) else: im_list = [args.im_or_folder] for i, im_name in enumerate(im_list): out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext) ) logger.info('Processing {} -> {}'.format(im_name, out_name)) im = cv2.imread(im_name) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers ) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) if i == 0: logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)' ) vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=args.thresh, kp_thresh=args.kp_thresh, ext=args.output_ext, out_when_no_box=args.out_when_no_box )
def infer(par,thresh): logger = logging.getLogger(__name__) merge_cfg_from_file(par['config_path']) cfg.NUM_GPUS = 1 par['weight_path'] = cache_url(par['weight_path'], cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) assert not cfg.MODEL.RPN_ONLY, \ 'RPN models are not supported' assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \ 'Models that require precomputed proposals are not supported' model = infer_engine.initialize_model_from_cfg(par['weight_path']) if os.path.isdir(par['input_img_path']): im_list = glob.iglob(par['input_img_path'] + '/*.jpg') else: im_list = [par['input_img_path']] count = 0 t_total = 0 np.set_printoptions(suppress=True) #numpy不以科学计数法输出 for i, im_name in enumerate(im_list):# i为计数,im_name为图像路径 out_name = os.path.join( par['output_xml_path'], '{}'.format(os.path.basename(im_name.rstrip(".jpg")) + '.xml') ) #logger.info('Processing {} -> {}'.format(im_name, out_name)) im = cv2.imread(im_name) w = float(im.shape[1]) h = float(im.shape[0]) #开始计时 timers = defaultdict(Timer) t_start = time.time() with c2_utils.NamedCudaScope(par['gpu_id']): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers ) boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format(cls_boxes, cls_segms, cls_keyps) if boxes is not None: boxes = np.array(boxes) # 坐标归一化 ↓ # boxes[:,0:4] = boxes[:,0:4]/np.array([col,row,col,row]) # boxes = np.maximum(boxes,0) # boxes = np.minimum(boxes,1) classes_ = np.array(classes,dtype=int) classes_temp = classes_.reshape(1,-1) classes = np.transpose(classes_temp) res = np.hstack((classes,boxes)) # res中,第一列为类别,2~5列为坐标,第六列为分数 res = res[res[:,-1]>thresh] else: res = [] #结束计时 t_end = time.time() t_total = t_total + (t_end-t_start) count = count + 1 make_xml_file(par,res,w,h,out_name) print("Average detection time:",int(1000*t_total/count),"ms/img")
def main(args): logger = logging.getLogger(__name__) merge_cfg_from_file(args.cfg) cfg.NUM_GPUS = 1 args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) assert not cfg.MODEL.RPN_ONLY, \ 'RPN models are not supported' assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \ 'Models that require precomputed proposals are not supported' model = infer_engine.initialize_model_from_cfg(args.weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() ''' add onboard cam support ''' cam = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=0 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink") if cam.isOpened(): print("camara open succeded") im_name = "Detection" while True: _, im = cam.read(); timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers ) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) ''' for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) ''' vis_utils.vis_cam_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2 ) """ key=cv2.waitKey(10) if key == 27: # Check for ESC key cv2.destroyAllWindows() break ; """ else: print("camera open failed")
def main(args): logger = logging.getLogger(__name__) dummy_coco_dataset = dummy_datasets.get_coco_dataset() cfg_orig = load_cfg(yaml.dump(cfg)) im = cv2.imread(args.im_file) if args.rpn_pkl is not None: proposal_boxes, _proposal_scores = get_rpn_box_proposals(im, args) workspace.ResetWorkspace() else: proposal_boxes = None cls_boxes, cls_segms, cls_keyps = None, None, None for i in range(0, len(args.models_to_run), 2): pkl = args.models_to_run[i] yml = args.models_to_run[i + 1] cfg.immutable(False) merge_cfg_from_cfg(cfg_orig) merge_cfg_from_file(yml) if len(pkl) > 0: weights_file = pkl else: weights_file = cfg.TEST.WEIGHTS cfg.NUM_GPUS = 1 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(weights_file) with c2_utils.NamedCudaScope(0): cls_boxes_, cls_segms_, cls_keyps_ = \ model_engine.im_detect_all(model, im, proposal_boxes) cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps workspace.ResetWorkspace() out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(args.im_file) + '.pdf') ) logger.info('Processing {} -> {}'.format(args.im_file, out_name)) vis_utils.vis_one_image( im[:, :, ::-1], args.im_file, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2 )
def main(args): glob_keypoints = [] logger = logging.getLogger(__name__) merge_cfg_from_file(args.cfg) cfg.NUM_GPUS = 1 args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE) assert_and_infer_cfg(cache_urls=False) assert not cfg.MODEL.RPN_ONLY, \ 'RPN models are not supported' assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \ 'Models that require precomputed proposals are not supported' model = infer_engine.initialize_model_from_cfg(args.weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() if os.path.isdir(args.im_or_folder): im_list = glob.iglob(args.im_or_folder + '/*' + '.png') else: im_list = [args.im_or_folder] im_list = sorted(im_list) for i, im_name in enumerate(im_list): out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext) ) logger.info('Processing {} -> {}'.format(im_name, out_name)) im = cv2.imread(im_name) timers = defaultdict(Timer) t = time.time() with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( model, im, None, timers=timers ) logger.info('Inference time: {:.3f}s'.format(time.time() - t)) for k, v in timers.items(): logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) if i == 0: logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)' ) vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=args.thresh, kp_thresh=args.kp_thresh, ext=args.output_ext, out_when_no_box=args.out_when_no_box ) cls_boxes_np = np.asarray(cls_boxes) cls_boxes_prob = cls_boxes_np[1][:,4] idx_max_prob = np.argmax(cls_boxes_prob) cls_keyps_max_prob = cls_keyps[1][idx_max_prob] pose_x_y_prob_after_softmax = cls_keyps_max_prob[[0,1,3]] glob_keypoints.append(np.transpose(pose_x_y_prob_after_softmax)) dictionarry_keypoints={'S1': {'Directions 1' : np.asarray([glob_keypoints])}} metadata = {'layout_name': 'h36m', 'num_joints': 17, 'keypoints_symmetry': [[4, 5, 6, 11, 12, 13], [1, 2, 3, 14, 15, 16]]} #np.savez(os.path.join('/home/narvis/Dev/VideoPose3D/data', "data_2d_detections.npz"), metadata=metadata, positions_2d=dictionarry_keypoints) np.savez(os.path.join(args.output_dir, "data_2d_detections.npz"), metadata=metadata, positions_2d=dictionarry_keypoints)