def run(input_file_in_memory, method): if input_file_in_memory.shape[2] == 4: input_file_in_memory = input_file_in_memory[:, :, 0:-1] if method == 'instancesegmentation' or method == 'predictions': config_file = '/workspace/detectron2_repo/configs/quick_schedules/mask_rcnn_R_50_FPN_inference_acc_test.yaml' elif method == 'panopticsegmentation': config_file = '/workspace/detectron2_repo/configs/quick_schedules/panoptic_fpn_R_50_inference_acc_test.yaml' elif method == 'keypoint': config_file = '/workspace/detectron2_repo/configs/quick_schedules/keypoint_rcnn_R_50_FPN_inference_acc_test.yaml' elif method == 'densepose': io_buf = None io_buf = io.BytesIO(apply_net_main(input_file_in_memory)) return io_buf else: return {'message': 'invalid parameter'} cfg = setup_cfg(config_file=config_file, is_gpu=False) debug = False if method == 'predictions' else True demo = VisualizationDemo(cfg, debug=debug) predictions, visualized_output, obj = demo.run_on_image( input_file_in_memory, debug) if debug: np_img = visualized_output.get_image() output_file_in_memory = cv2.cvtColor(np_img, cv2.COLOR_BGR2RGB) is_success, buffer = cv2.imencode(".jpg", output_file_in_memory) io_buf = io.BytesIO(buffer) return io_buf else: return obj
def rcnn(img): mp.set_start_method("spawn", force=True) cfg = setup_cfg(args) demo = VisualizationDemo(cfg) img = np.flip(img, 2) start_time = time.time() predictions, visualized_output = demo.run_on_image(img) # print(predictions['instances'].pred_boxes.tensor) return predictions[ 'instances'].pred_boxes.tensor, visualized_output.get_image()
def cmd(i, o): mp.set_start_method("spawn", force=True) cfg = setup_cfg() demo = VisualizationDemo(cfg) img = read_image(i, format="BGR") inputimg_shape = cv2.imread(i).shape[:2] predictions, visualized_output = demo.run_on_image(img) visualized_output.save(o) #output prediction results to json import json prediction_dict = predictions['instances'].get_fields() classes = ['text', 'title', 'list', 'table', 'figure'] prediction_result = {} prediction_result['inputImgShape'] = inputimg_shape #make categories list categories = [] for i in prediction_dict['pred_classes'].tolist(): categories.append(classes[i]) #make overall ratio list overall_ratio = [] for i in prediction_dict['pred_boxes'].tensor.tolist(): small_box = [] for j in range(len(i)): if ((j == 0) or (j == 2)): small_box.append(i[j] / inputimg_shape[1]) elif ((j == 1) or (j == 3)): small_box.append(i[j] / inputimg_shape[0]) overall_ratio.append(small_box) detected_boxes_list = [] for i in range(len(prediction_dict['pred_boxes'])): detected_bos_and_category = {} detected_bos_and_category['category'] = categories[i] detected_bos_and_category['detectedBoxArea'] = prediction_dict[ 'pred_boxes'].tensor.tolist()[i] detected_bos_and_category['overallRatioOfDetectedBox'] = overall_ratio[ i] detected_bos_and_category['confidenceScore'] = prediction_dict[ 'scores'].tolist()[i] detected_boxes_list.append(detected_bos_and_category) prediction_result['detectedBoxes'] = detected_boxes_list f = open('prediction_result.json', 'w') json.dump(prediction_result, f) print('successed')
def get_result(event): """ docstring """ setup_logger(name="fvcore") logger = setup_logger() cfg = setup_cfg() demo = VisualizationDemo(cfg) from_s3_to_tmp(event) # use PIL, to be consistent with evaluation img = read_image('/tmp/input.jpg', format="BGR") start_time = time.time() predictions, visualized_output = demo.run_on_image(img) out_filename = '/tmp/output.jpg' visualized_output.save(out_filename)
def main(): cfg = setup_config() realsense_cfg = setup_realsense() pipeline = rs.pipeline() pipeline.start(realsense_cfg) visualizer = VisualizationDemo(cfg) try: while True: frames = pipeline.wait_for_frames() color_frame = frames.get_color_frame() color_frame = np.asanyarray(color_frame.get_data()) frame = color_frame # Get depth frame align = rs.align(rs.stream.color) aligned_frames = align.process(frames) aligned_depth_frame = aligned_frames.get_depth_frame() #depth_frame = np.asanyarray(aligned_depth_frame.get_data()) # Do instance segmentation output, vis = visualizer.run_on_image(frame) print(output['instances'].pred_classes) # Calculate distance boxes = output['instances'].pred_boxes if len(boxes) != 0: dist_list = get_distance_list(boxes, aligned_depth_frame) for d in dist_list: #cv2.putText(vis, "dist = " + str(d), (50,50), # cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2, cv2.LINE_AA) print(str(dist_list)) cv2.imshow('mask', vis) cv2.waitKey(1) finally: # Stop streaming pipeline.stop()
logger = setup_logger() logger.info("Arguments: " + str(args)) cfg = setup_cfg(args) demo = VisualizationDemo(cfg) if args.input: if len(args.input) == 1: args.input = glob.glob(os.path.expanduser(args.input[0])) assert args.input, "The input path(s) was not found" for path in tqdm.tqdm(args.input, disable=not args.output): # use PIL, to be consistent with evaluation img = read_image(path, format="BGR") start_time = time.time() predictions, visualized_output = demo.run_on_image(img) logger.info("{}: {} in {:.2f}s".format( path, "detected {} instances".format(len(predictions["instances"])) if "instances" in predictions else "finished", time.time() - start_time, )) if args.output: if os.path.isdir(args.output): assert os.path.isdir(args.output), args.output out_filename = os.path.join(args.output, os.path.basename(path)) else: assert len( args.input
args = get_parser().parse_args() cfg = setup_cfg(args) detection_demo = VisualizationDemo(cfg) test_images_path = args.input output_path = args.output start_time_all = time.time() img_count = 0 for i in glob.glob(test_images_path): print(i) img_name = os.path.basename(i) img_save_path = output_path + img_name.split('.')[0] + '.jpg' img = cv2.imread(i) start_time = time.time() prediction, vis_output, polygons = detection_demo.run_on_image(img) #txt_save_path = output_path + 'res_img' + img_name.split('.')[0].split('img')[1] + '.txt' txt_save_path = output_path + 'res_img' + img_name.split( '.')[0] + '.txt' save_result_to_txt(txt_save_path, prediction, polygons) print("Time: {:.2f} s / img".format(time.time() - start_time)) vis_output.save(img_save_path) img_count += 1 print("Average Time: {:.2f} s /img".format( (time.time() - start_time_all) / img_count))
cfg = setup_cfg(args) demo = VisualizationDemo(cfg) if args.input: if len(args.input) == 1: args.input = glob.glob(os.path.expanduser(args.input[0])) assert args.input, "The input path(s) was not found" for path in tqdm.tqdm(args.input, disable=not args.output): # use PIL, to be consistent with evaluation # img = read_image(path, format="BGR") img = read_image(path, format="RGB") # OneNet uses RGB input as default start_time = time.time() predictions, visualized_output = demo.run_on_image( img, args.confidence_threshold) logger.info("{}: {} in {:.2f}s".format( path, "detected {} instances".format(len(predictions["instances"])) if "instances" in predictions else "finished", time.time() - start_time, )) if args.output: if os.path.isdir(args.output): assert os.path.isdir(args.output), args.output out_filename = os.path.join(args.output, os.path.basename(path)) else: assert len( args.input
default=None, help="output images name", ) parser.add_argument( "--confidence-threshold", type=float, default=0.5, help="Minimum score for instance predictions to be shown", ) return parser if __name__ == "__main__": args = get_parser().parse_args() cfg = setup_cfg(args) input = args.input output = args.output debug = output is not None demo = VisualizationDemo(cfg, debug) # use PIL, to be consistent with evaluation img = read_image(input, format="BGR") predictions, visualized_output, obj = demo.run_on_image(img, debug) if output != None: visualized_output.save(output) print(output) else: print(json.dumps(obj))
import tqdm import time from detectron2.config import get_cfg from detectron2.data.detection_utils import read_image from detectron2.utils.logger import setup_logger from predictor import VisualizationDemo # constants WINDOW_NAME = "COCO detections" if __name__ == "__main__": init_start = time.time() demo = VisualizationDemo() init_end = time.time() print("init time %.4f" % (init_end - init_start)) dir_name = "/media/gm/Data/SLAM/dataset/TUM/rgbd_dataset_freiburg3_sitting_halfsphere/rgb" file_name = os.listdir(dir_name) process_start = time.time() file_num = 0 for name in file_name: result = demo.run_on_image(os.path.join(dir_name, name), "result.png") file_num += 1 if file_num >= 10: break process_end = time.time() average_time = (process_end - process_start) / file_num print("average time %.4f" % average_time)
setup_logger(name="fvcore") logger = setup_logger() logger.info("Arguments: " + str(args)) cfg = setup_cfg(args) demo = VisualizationDemo(cfg) input_indices = [int(x) for x in args.input_index.split(',')] output_folder = os.path.dirname(args.output_template) if not os.path.exists(output_folder): os.makedirs(output_folder) for index in tqdm.tqdm(input_indices): # use PIL, to be consistent with evaluation output_name = args.output_template % index if not os.path.exists(output_name): path = args.input_template % index img = read_image(path, format="BGR") start_time = time.time() predictions, visualized_output = demo.run_on_image(img, mask_only=True) logger.info("{}: {} in {:.2f}s".format( path, "detected {} instances".format(len(predictions["instances"])) if "instances" in predictions else "finished", time.time() - start_time, )) visualized_output.save(output_name)
def create_annotation(image_folder, json_path, confidence_thresh=0.8): json_dict = { "images": [], "type": "instances", "annotations": [], "categories": [] } mp.set_start_method("spawn", force=True) args = get_parser().parse_args() logger = setup_logger() logger.info("Arguments: " + str(args)) cfg = setup_cfg(args) demo = VisualizationDemo(cfg) image_path = {} for path, subdirs, files in os.walk(image_folder): for name in files: print(name) if name.endswith('.jpg') or \ name.endswith('.png') or \ name.endswith('.JPG') or \ name.endswith('.PNG') or \ name.endswith('.jpeg') or \ name.endswith('.JPEG'): image_path[name] = os.path.join(path, name) print("length: ", len(image_path.keys())) for path in tqdm.tqdm(image_path.keys(), disable=not args.output): # use PIL, to be consistent with evaluation start_time = time.time() try: img = read_image(image_path[path], format="BGR") # run detector predictions, visualized_output, shape = demo.run_on_image(img) except: print("except") continue height, width, channel = shape global count ## append image info image = { "file_name": str(path), "height": str(height), "width": str(width), "id": str(count), } count += 1 # if count > 10: # break json_dict["images"].append(image) ## append annotation info bnd_id = 0 for i in range(len(predictions["instances"].pred_boxes)): if predictions["instances"].scores[ i] > confidence_thresh and predictions[ "instances"].pred_classes[i] in [0, 2, 5, 7]: # print(predictions["instances"].pred_boxes[i].tensor) x_center, y_center, o_width, o_height = predictions[ "instances"].pred_boxes[i].tensor[0].cpu().detach().numpy( ) score = predictions["instances"].scores[i].cpu().detach( ).numpy() pred_class = predictions["instances"].pred_classes[i].cpu( ).detach().numpy() # print(x_center, y_center, o_width, o_height, score) ann = { "area": str(o_width * o_height), "iscrowd": 0, "image_id": str(count), "bbox": [ str(int(x_center - o_width / 2)), str(int(y_center - o_height / 2)), str(o_width), str(o_height) ], "category_id": str(pred_class + 1), "id": str(bnd_id), "ignore": 0, "segmentation": [], } bnd_id += 1 json_dict["annotations"].append(ann) # cat = {"supercategory": "none", "id": cid, "name": cate} # json_dict["categories"].append(cat) # if args.output: # if os.path.isdir(args.output): # assert os.path.isdir(args.output), args.output # out_filename = os.path.join(args.output, os.path.basename(path)) # else: # assert len(args.input) == 1, "Please specify a directory with args.output" # out_filename = args.output # visualized_output.save(out_filename) # print("pred_boxes: ", predictions["instances"].pred_boxes) # print("scores: ", predictions["instances"].scores) # print("pred_classes: ", predictions["instances"].pred_classes) # print("shape: ", width, height, channel) # logger.info( # "{}: detected {} instances in {:.2f}s".format( # path, len(predictions["instances"]), time.time() - start_time # ) # ) logger.info( ("progress: {:.0f} / {:.0f}".format(count, len(image_path.keys())))) ## append category info cat = {"supercategory": "none", "id": str(1), "name": "person"} json_dict["categories"].append(cat) cat = {"supercategory": "none", "id": str(3), "name": "car"} json_dict["categories"].append(cat) cat = {"supercategory": "none", "id": str(6), "name": "bus"} json_dict["categories"].append(cat) cat = {"supercategory": "none", "id": str(8), "name": "truck"} json_dict["categories"].append(cat) os.makedirs(os.path.dirname(json_path), exist_ok=True) json_fp = open(json_path, "w") json_str = json.dumps(json_dict) json_fp.write(json_str) json_fp.close()
demo = VisualizationDemo(cfg) videos = [ "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33" ] output_dir = "/public/MOTDataset/HIE20/ResNeSt200_hie_all_detections_iou07/test" if not os.path.exists(output_dir): os.makedirs(output_dir) for v in videos: video_sub_dir = "/public/MOTDataset/HIE20/images/test/{}/img".format(v) imgs = get_all_imgs_in_dir(video_sub_dir) output_file = "{}/{}.txt".format(output_dir, v) results = [] for img in tqdm.tqdm(imgs): img_name = img.split("/")[-1] frame = img_name.split(".")[0] frame = int(frame) img = read_image(img, format="BGR") predictions, _ = demo.run_on_image(img) result_single = dump_detection( frame, predictions["instances"].pred_boxes.tensor.tolist(), predictions["instances"].scores.tolist(), predictions["instances"].pred_classes.tolist()) results.extend(result_single) with open(output_file, 'w') as f: for i in results: f.write(i) f.write('\n')
print(dirs) path = path_root + '/' + dirs name = dirs[0:20] input_list.append(path) for path in tqdm.tqdm(input_list, disable=not args.output): # use PIL, to be consistent with evaluation name = path[-24:-4] skeleton_file = "/dev/dataset/nturgbd_skeletons_120/" + name + '.skeleton' res_box10, res_box06 = get_res_box(skeleton_file) img = read_image(path, format="BGR") start_time = time.time() #ipdb.set_trace() predictions, visualized_output = demo.run_on_image( img, res_box06, res_box10) ''' logger.info( "{}: detected {} instances in {:.2f}s".format( path, len(predictions["instances"]), time.time() - start_time ) ) ''' if args.output: if os.path.isdir(args.output): assert os.path.isdir(args.output), args.output out_filename = os.path.join(args.output, os.path.basename(path)) else: assert len( args.input