def main(): args = parse_args() if mmdet.CPU_ONLY: device = torch.device('cpu') else: device = torch.device('cuda', args.device) model = init_detector(args.config, args.checkpoint, device=device) camera = cv2.VideoCapture(args.camera_id) print('Press "Esc", "q" or "Q" to exit.') while True: ret_val, img = camera.read() result = inference_detector(model, img) ch = cv2.waitKey(1) if ch == 27 or ch == ord('q') or ch == ord('Q'): break show_result(img, result, model.CLASSES, score_thr=args.score_thr, wait_time=1)
def main(): #args = parse_args() '''model = init_detector( args.config, args.checkpoint, device=torch.device('cuda', args.device)) ''' model = init_detector( '../configs/cityscapes/mask_rcnn_r50_fpn_1x_cityscapes.py', '../checkpoints/mask_rcnn_r50_fpn_1x_city_20190727-9b3c56a5.pth', device=torch.device('cuda', 0)) modelseg = ESPNet_Encoder(3, p=2, q=3).to(device) modelseg.load_state_dict( torch.load('bdd_weights_20_ESPNET_road.pth', map_location='cpu')) modelseg.eval() #camera = cv2.VideoCapture(args.camera_id) camera = cv2.VideoCapture('umgt.avi') print('Press "Esc", "q" or "Q" to exit.') if camera.isOpened(): while True: ret_val, img = camera.read() imgroad = RoadSeg(img, modelseg) #imgroad = evaluateModel(img,modelseg) result = inference_detector(model, img) ch = cv2.waitKey(1) if ch == 27 or ch == ord('q') or ch == ord('Q'): break #show_result(img, result, model.CLASSES, score_thr=args.score_thr, wait_time=1) show_result(imgroad, result, model.CLASSES, score_thr=0.5, wait_time=1) cv2.destroyAllWindows()
def main(show_one_pic=False): model = init_detector(config_file, checkpoint_file) if show_one_pic: img = 'data/coco/train2017/IMG_9372.MOV0035.jpg' result = inference_detector(model, img) print(result) show_result(img, result, model.CLASSES) return # fourcc = cv2.VideoWriter_fourcc(*'XVID') # out = cv2.VideoWriter('out.avi', fourcc, 25.0, (1280, 720)) camera = cv2.VideoCapture('/home/rex/mmdetection/IMG_9372~1.avi') print('Press "Esc", "q" or "Q" to exit.') while True: ret_val, img = camera.read() result = inference_detector(model, img) ch = cv2.waitKey(1) if ch == 27 or ch == ord('q') or ch == ord('Q'): break # frame = show_result(img, result, ('wing', 'drumstick', 'pai', 'tart', 'none'), score_thr=0.35, wait_time=1, # show=False) show_result(img, result, model.CLASSES, score_thr=0.35, wait_time=1)
def save_det_result_bbox(config_file, out_dir, checkpoint_file=None, img_dir=None): cfg = Config.fromfile(config_file) data_test = cfg.data.test dataset = build_dataset(data_test) classnames = dataset.CLASSES # import ipdb;ipdb.set_trace() # use checkpoint path in cfg if not checkpoint_file: checkpoint_file = osp.join(cfg.work_dir, 'latest.pth') # use testset in cfg if not img_dir: img_dir = data_test.img_prefix model = init_detector(config_file, checkpoint_file, device='cuda:0') img_list = os.listdir(img_dir) for img_name in img_list: img_path = osp.join(img_dir, img_name) img_out_path = osp.join(out_dir, img_name) result = inference_detector(model, img_path) show_result(img_path, result, model.CLASSES, show=False, score_thr=0.2, out_file=img_out_path) print(img_out_path)
def main(): args = parse_args() model = init_detector(args.config, args.checkpoint, device=torch.device('cuda', args.device)) camera = cv2.VideoCapture(args.camera) print('Press "Esc", "q" or "Q" to exit.') while True: ret_val, img = camera.read() tic = time.time() result = inference_detector(model, img) cost = time.time() - tic print('cost: {}, fps: {}'.format(cost, 1 / cost)) ch = cv2.waitKey(1) if ch == 27 or ch == ord('q') or ch == ord('Q'): break show_result(img, result, model.CLASSES, score_thr=args.score_thr, wait_time=1)
def main(): parser = ArgumentParser() #parser.add_argument('img_dir',help='Image file Dir') parser.add_argument('config', help='Config file') parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--score-thr', type=float, default=0.7, help='bbox score threshold') args = parser.parse_args() import os img_dir = r"/home/amax/anaconda3/envs/mmdetection/mmdetection/demo/demo_image" out_dir = "/home/amax/anaconda3/envs/mmdetection/mmdetection/demo/"+args.config[8:-3] if(os.path.exists(out_dir) is None): os.makedirs(out_dir) for img_name in os.listdir(img_dir): img_path = img_dir + "/" +img_name out_file = out_dir + "/" + img_name img = cv2.imread(img_path) # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # test a single image result = inference_detector(model, img) # show the results #show_result_pyplot(img, result, model.CLASSES,score_thr=args.score_thr) show_result( img, result, model.CLASSES, score_thr=args.score_thr, wait_time=1,out_file=out_file)
def main(): parser = ArgumentParser() parser.add_argument('img', help='Image file') parser.add_argument('config', help='Config file') parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument('--device', default='cuda:0', help='Device used for inference') parser.add_argument('--score-thr', type=float, default=0.3, help='bbox score threshold') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # test a single image result = inference_detector(model, args.img) # show the results show_result( args.img, result, model.CLASSES, score_thr=args.score_thr, )
def verify_images(): im_list, out_dir = get_imlist2() detections = {} for imf in tqdm(im_list, total=len(im_list)): im = cv2.imread(imf) reiszed_im = cv2.resize(im, (1280, 800)) inputs = preprocess(reiszed_im) result = model(inputs.cuda(), return_loss=False, try_dummy=False) labels = [i * np.ones((len(r), 1)) for i, r in enumerate(result, 1)] bboxes = np.vstack(result) labels = np.vstack(labels) # nms for all classes bboxes, idx = nms(bboxes, 0.7) labels = labels[idx] # detection above score threshold idx = bboxes[:, -1] > thresh bboxes = bboxes[idx] labels = labels[idx] detections[imf] = np.hstack([bboxes, labels]) continue out_file = os.path.join(out_dir, os.path.basename(imf)) show_result(reiszed_im, result, class_names, score_thr=thresh, out_file=out_file) out_file = os.path.join(out_dir, 'detections.pkl') with open(out_file, 'wb') as f: pkl.dump(detections, f)
def validate_epoch(config_path, work_dir, epoch_path, pathImg, csvPath): model = init_detector(config_path, epoch_path, device='cuda:4') resultCsv = open(csvPath, "a") # 打开result csv文件,检测的结果将存放在这里面,需要提前新建好 writer = csv.writer(resultCsv) # 取testImg文件夹中的每一张图片 #pathSaveImg = csvPath.split('/')[-1][0:-4] pathSaveImg = os.path.join(work_dir, 'val_result', epoch_path.split('/')[-1][:-4]) if (os.path.exists(pathSaveImg) != 1): os.makedirs(pathSaveImg) for imgP in os.listdir(pathImg): img = os.path.join(pathImg, imgP) #args.pathImg + '/' + imgP # img的绝对路径 #print("#######img\n",img) # 将img传给model,产生输出 # test a single image result = inference_detector(model, img) # save the visualization results to image files csvOut = [] #show_result(img, result, model.CLASSES,score_thr=0.0001, show=False, out_file=args.work_dir + '/result_image/' + imgP, out_csv=csvOut) show_result(img, result, model.CLASSES, score_thr=0.0001, show=False,\ out_file=os.path.join(pathSaveImg,imgP), out_csv=csvOut) # 将结果写进csv,result array中保存了很多bbox,一次只写一个 for aBbox in csvOut: # 产成大于阈值的list并将该list命名为record writer.writerow(aBbox) # 关闭打开的两个csv resultCsv.close()
def verify_video(): out_dir = '/private/ningqingqun/results/centernet_results/senyun' if not os.path.isdir(out_dir): os.makedirs(out_dir) vfile = '/private/ningqingqun/datasets/videos/WIN_20191030_20_38_42_Pro.mp4' cap = cv2.VideoCapture(vfile) total_frame = cap.get(cv2.CAP_PROP_FRAME_COUNT) # print(total_frame) display_num = 20 frame_count = 0 base_frame_index = random.randint(0, total_frame - display_num) cap.set(cv2.CAP_PROP_POS_FRAMES, base_frame_index) print('process from index: {}'.format(base_frame_index)) while (cap.isOpened()): ret, frame = cap.read() reiszed_im = cv2.resize(frame, (1280, 800)) inputs = preprocess(reiszed_im) result = model(inputs.cuda(), return_loss=False, try_dummy=False) out_file = os.path.join( out_dir, '{:04}.jpg'.format(base_frame_index + frame_count)) show_result(reiszed_im, result, class_names, score_thr=thresh, out_file=out_file) frame_count += 1 if frame_count > display_num: break cap.release()
def mock_detector(model, image_name, output_dir): image = cv2.imread(image_name) results = inference_detector(model, image) basename = os.path.basename(image_name).split('.')[0] result_name = basename + "_result.jpg" result_name = os.path.join(output_dir, result_name) show_result(image, results, model.CLASSES, out_file=result_name)
def main(): fcsv = open('/DATA/zhanghui/underwater-obj_de/submit/test_yb.csv', 'w', encoding='utf-8') csv_writer = csv.writer(fcsv) csv_writer.writerow( ["name", "image_id", "confidence", "xmin", "ymin", "xmax", "ymax"]) imgpath = '/DATA/zhanghui/underwater-obj_de/data/test-A-image/' num = 0 # for imgname in glob.glob(imgpath + '000003.jpg'): imgname = '/DATA/zhanghui/underwater-obj_de/data/test-A-image/000003.jpg' if not os.path.exists('/DATA/zhanghui/underwater-obj_de/data/temp_path'): os.mkdir('/DATA/zhanghui/underwater-obj_de/data/temp_path') else: files = glob.glob( '/DATA/zhanghui/underwater-obj_de/data/temp_path/*.jpg') for f in files: os.remove(f) result = cut_save_img(imgname) end_result = result_sort(result) show_result(imgname, end_result, model.CLASSES) # save_csv(csv_writer, imgname, end_result) print(num) num = num + 1 fcsv.close()
def draw_boxes(input_dir, output_dir, config_file, checkpoint_file, threshold=0.5): if not os.path.exists(output_dir): os.mkdir(output_dir) images = [ f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f)) and f.endswith('.jpg') ] # build the model from a config file and a checkpoint file model = init_detector(config_file, checkpoint_file, device='cuda:0') progress_bar = mmcv.ProgressBar(len(images)) for img in images: input_img = os.path.join(input_dir, img) result = inference_detector(model, input_img) show_result(input_img, result, model.CLASSES, show=False, out_file=os.path.join(output_dir, img), score_thr=threshold) progress_bar.update()
def detect_masks(img, out_file): result = inference_detector(model, img) show_result(img, result, model.CLASSES, score_thr=score_thr, show=False, out_file=out_file)
def main(): args = parse_args() model = init_detector( args.config, args.checkpoint, device=torch.device('cuda', args.device)) # camera = cv2.VideoCapture(args.camera_id) camera = LoadStreams(args.camera_id) # tracker tracker = Sort(10, 4) frame_rate = 0 start_time = time.time() frame_count = 0 print('Press "Esc", "q" or "Q" to exit.') while True: # ret_val, img = camera.read() img = camera.__next__()[0] result = inference_detector(model, img) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) bboxes = bboxes[bboxes[:, 4] > args.score_thr] trackers = tracker.update(bboxes) trackers = trackers.astype(np.int32) for d in trackers: cv2.rectangle(img, (d[0], d[1]), (d[2], d[3]), (255, 255, 255)) cv2.putText(img, '{}'.format(d[4]), (d[0], d[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255)) ch = cv2.waitKey(1) if ch == 27 or ch == ord('q') or ch == ord('Q'): break end_time = time.time() if (end_time - start_time) >= 1: frame_rate = int(frame_count / (end_time - start_time)) start_time = time.time() frame_count = 0 cv2.putText(img, str(frame_rate) + " fps", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), thickness=2, lineType=2) frame_count += 1 show_result( img, result, model.CLASSES, score_thr=args.score_thr, wait_time=1)
def detect_images(config_file, checkpoint_file, imgs_path): #imgs_path 包含image 路径的txt文件 f = open(img_path) imgs_list = f.readlines(imgs_path) f.close() # test a list of images and write the results to image files for i, result in enumerate(inference_detector(model, imgs_list)): show_result(imgs[i], result, model.CLASSES, out_file='result_{}.jpg'.format(i))
def verify_image(): imf = '/private/ningqingqun/bags/crane/howo1_2019_11_05_09_06_22_6.msg/front_right/201911050907/201911050907_00000417_1572916023857.jpg' im = cv2.imread(imf) reiszed_im = cv2.resize(im, (1280, 800)) inputs = preprocess(reiszed_im) result = model(inputs.cuda(), return_loss=False, try_dummy=False) show_result(reiszed_im, result, class_names, score_thr=thresh, out_file='centernet_verify.jpg')
def main(): args = parse_args() model = init_detector(args.config, args.checkpoint, device='cuda:0') result = inference_detector(model, args.input) result = result[:-1] # ignore dummy show_result( args.input, result, CLASS_NAMES, show=False, out_file=args.output, )
def main(): args = parse_args() model = init_detector( args.config, args.checkpoint, device=torch.device('cuda', args.device)) camera = cv2.VideoCapture(args.camera_id) while True: ret_val, img = camera.read() result = inference_detector(model, img) show_result( img, result, model.CLASSES, score_thr=args.score_thr, wait_time=1)
def test_and_draw_from_single_file(sample_file, ext_name, bgr_file, out_file, config_file, checkpoint_file, score_threhold): model = init_detector(config_file, checkpoint_file, device='cuda:0') sample = None if ext_name == 'bgr': sample = mmcv.imread(sample_file) elif ext_name == 'tiff': sample = load_pol_sub_image(sample_file) else: sample = LoadPolNPZImageFromFile(sample_file) print(sample) result = inference_detector(model, sample) img = mmcv.imread(bgr_file) show_result(img, result, model.CLASSES, out_file=out_file, score_thr=score_threhold)
def main(): args = parse_args() # build the model from a config file and a checkpoint file model = init_detector(args.config_file, args.checkpoint, device='cuda:0') # test a single image and show the results img = args.input result = inference_detector(model, img) # visualize the results in a new window # or save the visualization results to image files show_result( img, result, model.CLASSES, out_file=img.split('.')[0] + '_result.jpg')
def predict_with_txt(txt_name): path = '/data2/yeliang/data/leakage_data/line_10_719/Camera7' files = os.listdir(path) imgs = [os.path.join(path, i) for i in files] print("start inference") for i, result in enumerate(inference_detector(model, imgs)): show_result( imgs[i], result, model.CLASSES, out_path= '/data2/yeliang/data/tunnel_camera/20190719_line10_camera/20190719_model/Camera7/', out_file='{}'.format(files[i][:-3] + 'jpg'), txt=txt_name)
def mask_rcnn(): cfg_file = 'configs/mask_rcnn_r50_fpn_1x.py' ckp_file = '../../checkpoints/' \ 'mask_rcnn_r50_fpn_1x_20181010-069fa190.pth' model = apis.init_detector(cfg_file, ckp_file, device='cuda:0') img_file = '/home/lhhuang/data/coco/val2017/000000397133.jpg' img = mmcv.imread(img_file) result = apis.inference_detector(model, img) out_file = 'out.jpg' apis.show_result( img, result, model.CLASSES, show=False, out_file=out_file)
def visualize(): args = parse_args() model = load_model(args) root = '/home/fengyao/MSCOCO2017dataset/test/test2017/' save_path = '/home/fengyao/MSCOCO2017dataset/test/test2017/output/' imgs = ['000000000001.jpg', '000000000016.jpg'] for index in range(0, len(imgs)): result = inference_detector(model, root + imgs[index]) show_result(root + imgs[index], result, model.CLASSES, show=False, out_file=save_path + imgs[index]) print('[INFO]Done.')
def visual_image_reulsts(images_files, cfg_file, model_file, result_dir, device="cuda:0"): if not osp.exists(result_dir): os.makedirs(result_dir) if not osp.exists(cfg_file) or not osp.exists(model_file): print("Error: no such file: %s or %s"%(cfg_file, model_file)) return None cfg = mmcv.Config.fromfile(cfg_file) cfg.model.pretrained = None model = build_detector(cfg.model, test_cfg=cfg.test_cfg) _ = load_checkpoint(model, model_file) #set specified score thresh for better results visualization cfg.test_cfg["score_thr"] = 0.35 for i, result in enumerate(tqdm(inference_detector(model, images_files, cfg, device=device))): show_result(images_files[i], result, dataset="hangkongbei", out_file=osp.join(result_dir, osp.basename(images_files[i])))
def faster_rcnn(): cfg_file = 'configs/faster_rcnn_r50_fpn_1x.py' ckp_file = '../../checkpoints/' \ 'faster_rcnn_r50_fpn_2x_20181010-443129e1.pth' model = apis.init_detector(cfg_file, ckp_file, device='cuda:0') img_file = '/home/lhhuang/data/VOCdevkit/VOC2007/JPEGImages/007663.jpg' img = mmcv.imread(img_file) result = apis.inference_detector(model, img) out_file = 'out.jpg' apis.show_result( img, result, model.CLASSES, show=False, out_file=out_file)
def test_and_draw_from_xmls(xml_dir, ext_name, sample_dir, bgr_dir, out_dir, config_file, checkpoint_file, score_threhold): model = init_detector(config_file, checkpoint_file, device='cuda:0') xlms = os.listdir(xml_dir) sample_ids = [ i.split('_')[0] + '_' + i.split('_')[1] for i in xlms if i.endswith('.xml') ] for xlm_filename in xlms: sample_file = '' sample_path = '' sample = None sample_id = xlm_filename.split('.')[0] if ext_name == 'bgr': sample_file = xlm_filename.split('.')[0] + '.tiff' elif ext_name == 'pol' or ext_name == 'sub': sample_file = sample_id.split('_')[0] + '_' + sample_id.split( '_')[1] + '.' + 'tiff' else: sample_file = sample_id.split('_')[0] + '_' + sample_id.split( '_')[1] + '.' + 'ext_name' + '.npz' sample_path = os.path.join(sample_dir, sample_file) if ext_name == 'bgr': sample = mmcv.imread(sample_path) elif ext_name == 'pol' or ext_name == 'sub': sample = load_pol_sub_image(sample_path) else: sample = load_pol_npz_image(sample_path) img_path = os.path.join(bgr_dir, xlm_filename.split('.')[0] + '.tiff') img = mmcv.imread(img_path) result = inference_detector(model, sample) out_file = sample_id.split('_')[0] + '_' + sample_id.split( '_')[1] + '.' + ext_name + '.jpg' out_path = os.path.join(out_dir, out_file) show_result(img, result, model.CLASSES, show=False, out_file=out_path, score_thr=score_threhold) print(out_path)
def main(): args = parse_args() if not os.path.exists(args.test_folder): os.makedirs(args.test_folder) config_file = os.path.join(args.model_folder, args.config_filename) checkpoint_file = os.path.join(args.model_folder, args.checkpoint_filename) # build the model from a config file and a checkpoint file model = init_detector(config_file, checkpoint_file, device='cuda:0') # test a single image and show the results imgs = [os.path.join(args.test_folder, img) for img in os.listdir(args.test_folder) if (img.endswith('.jpg') or img.endswith('.jpeg'))] print(imgs) for i, result in enumerate(inference_detector(model, imgs)): show_result(imgs[i], result, model.CLASSES, out_file=os.path.join(args.prediction_folder, 'result_{}.jpg'.format(i)))
def result(): cfg = mmcv.Config.fromfile(config2make_json) cfg.model.pretrained = None # construct the model and load checkpoint model = build_detector(cfg.model, test_cfg=cfg.test_cfg) _ = load_checkpoint(model, model2make_json) # test a single image imgs = os.listdir(pic_path) meta = [] from tqdm import tqdm for im in tqdm(imgs): img = pic_path + im img = mmcv.imread(img) result = inference_detector(model, img) re,img = show_result(img, result, dataset='cloths', show=False,score_thr = 0.5) if len(re): for box in re: anno = {} anno['name'] = im anno['category'] = int(box[5]) anno['bbox'] = [round(float(i), 2) for i in box[0:4]] anno['score'] = float(box[4]) meta.append(anno) with open(json_path, 'w') as fp: json.dump(meta, fp, cls=MyEncoder,indent=4, separators=(',', ': '))
def vis(): cfg = [mmcv.Config.fromfile(config_) for config_ in config] for cfg_ in cfg: cfg_.model.pretrained = None # construct the model and load checkpoint model = [build_detector(cfg_.model, test_cfg=cfg_.test_cfg) for cfg_ in cfg] _ = [load_checkpoint(model[i], model_path[i]) for i in range(len(model))] # test a single image imgs = os.listdir(pic_path) from tqdm import tqdm for im in tqdm(imgs): img = pic_path+im img = mmcv.imread(img) for i in range(len(model)): result = inference_detector(model[i], img, cfg[i]) re,image = show_result(img,result,dataset='cloths',show = False,score_thr = 0.5) if im in anno.keys(): bboxes = anno[im] for bbox in bboxes: box = bbox[0] label = bbox[1] x1 = int(box[0]) y1 = int(box[1]) x2 = int(box[2]) y2 = int(box[3]) cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) # cv2.putText(image, '%d' % cat[label], (x2, y2), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 0, 255), 1) cv2.putText(image, '%d' % label, (x2, y2), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 0, 255), 1) name = config[i].split('_')[-1].split('.')[0] cv2.namedWindow(str(name),0) cv2.resizeWindow(str(name),1920,1080) cv2.imshow(str(name),image) cv2.waitKey(0)