Exemplo n.º 1
0
def main():
    cfg = setup_cfg()
    demo = VisualizationDemo(cfg)

    args = get_parser().parse_args()
    img_path = args.img_path
    new_img_path = args.new_img_path
    save_path = args.save_path

    for index, path in enumerate(tqdm(os.listdir(img_path))):
        if path.endswith('.jpg'):
            fullpath = os.path.join(img_path, path)
            img = read_image(fullpath, format='BGR')
            # remove the text 'google' from image
            img = img[:620, :, :]
            pred, out, poly = demo.run_on_image(img)
            if poly == []:
                cv2.imwrite(os.path.join(save_path, path), img,
                            [cv2.IMWRITE_JPEG_QUALITY, 75])
                os.remove(fullpath)

            else:
                cv2.imwrite(os.path.join(new_img_path, path), img,
                            [cv2.IMWRITE_JPEG_QUALITY, 75])
                os.remove(fullpath)
Exemplo n.º 2
0
def predict_API():
    '''
    供外部调用,返回一个初始化完成的模型,可以直接用来预测。搭建后台服务器用来响应前端页面的时候会用到。
    :return:
    '''
    args = get_parser().parse_args()
    cfg = setup_cfg(args)
    register_dataset()
    demo = VisualizationDemo(cfg, instance_mode=ColorMode.SEGMENTATION)
    return demo
Exemplo n.º 3
0
def do_crop():
    args = get_parser().parse_args()
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    # 注册数据集
    register_dataset()

    model_demo = VisualizationDemo(cfg)

    face_img_path = 'face_img/'
    all_img_path = 'all_img_json/'
    for index, imgfile in enumerate(os.listdir(all_img_path)):

        # use PIL, to be consistent with evaluation
        img_fullName = os.path.join(all_img_path, imgfile)
        img = read_image(img_fullName, format="BGR")
        start_time = time.time()
        predictions = model_demo.predictor(img)
        bbox = model_demo.highest_only(predictions).get('pred_boxes')
        if len(bbox.tensor) == 0:
            print(imgfile)
            continue
        bbox = bbox.tensor[0].tolist()
        all_label = tools.get_all_class_label_infos()
        json_template = tools.labelme_template()
        json_template["imageHeight"], json_template["imageWidth"] = img.shape[:2]
        json_template["imagePath"] = imgfile
        json_template["imageData"] = str(img_base64(img_fullName), encoding="utf-8")
        json_template["shapes"][0]["label"] = all_label[imgfile]
        json_template["shapes"][0]["points"] = [[bbox[0], bbox[1]], [bbox[2], bbox[3]]]
        json.dump(json_template, open(os.path.join(all_img_path, imgfile.replace('.jpg', '.json')), 'w'), indent=1)
        # img = Image.open(img_fullName)
        # img = img.crop((bbox[0], bbox[1], bbox[2], bbox[3]))
        # img.save(os.path.join(face_img_path, imgfile))
        if index % 1000 == 0:
            print(index)
Exemplo n.º 4
0
        default=[],
        nargs=argparse.REMAINDER,
    )
    return parser


if __name__ == "__main__":
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    setup_logger(name="fvcore")
    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, args.confidence_threshold)
            logger.info("{}: {} in {:.2f}s".format(
                path,
                "detected {} instances".format(len(predictions["instances"]))
                if "instances" in predictions else "finished",
Exemplo n.º 5
0
        default=[],
        nargs=argparse.REMAINDER,
    )
    return parser


if __name__ == "__main__":
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    setup_logger(name="fvcore")
    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,
Exemplo n.º 6
0
    return predictions


if __name__ == "__main__":
    ALL_START = time.time()
    print("Start counting time. ALL_START")
    # mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    setup_logger(name="fvcore")
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    if args.config_file.endswith("yaml"):
        cfg = setup_cfg(args)
        model = VisualizationDemo(cfg)
    elif args.config_file.endswith("py"):
        model = model_zoo.get(args.config_file, trained=True)
        model.cuda()
        model.eval()
    else:
        raise Exception(f"{args.config_file} is not supported.")

    if args.input:
        if len(args.input) == 1:
            args.input = glob.glob(osp.expanduser(args.input[0]))
            assert args.input, "The input path(s) was not found"

        data_dir = osp.join(args.input[0], args.split)
        if args.video_src_names:  # only process the given video sources
            video_src_names = args.video_src_names
Exemplo n.º 7
0
from demo.predictor import VisualizationDemo

with open('data/objects_vocab.txt', 'r') as f:
    obj_vocab = f.read().splitlines()

# Registering metadata for Visual Genome Object Classes
# MetadataCatalog.get("VisualGenomeObjects").thing_classes = obj_vocab

cfg = get_cfg()
cfg.merge_from_file(
    'configs/COCO-Detection/faster_rcnn_X_101_64x4d_FPN_2x_vlp.yaml')
cfg.freeze()

predictor = DefaultPredictor(cfg)
demo = VisualizationDemo(cfg)

#model = build_model(cfg)
#model.eval()

#checkpointer = DetectionCheckpointer(model)
#checkpointer.load(os.path.join('model_weights', 'e2e_faster_rcnn_X-101-64x4d-FPN_2x-vlp.pkl'))

img1 = read_image(os.path.join('test_images', '12283150_12d37e6389_z.jpg'),
                  format="BGR")
img2 = read_image(os.path.join('test_images', '25691390_f9944f61b5_z.jpg'),
                  format="BGR")
img3 = read_image(os.path.join('test_images', '9247489789_132c0d534a_z.jpg'),
                  format="BGR")

preds, vis_out = demo.run_on_image(img3, obj_vocab)
Exemplo n.º 8
0
    )
    return parser


if __name__ == "__main__":
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    # 注册数据集
    register_dataset()

    demo = VisualizationDemo(cfg, instance_mode=ColorMode.SEGMENTATION)

    # for path in tqdm.tqdm(args.input, disable=not args.output):
    for imgfile in os.listdir(INPUT_IMG_PATH):

        # use PIL, to be consistent with evaluation
        img_fullName = os.path.join(INPUT_IMG_PATH, imgfile)
        img = read_image(img_fullName, format="BGR")
        start_time = time.time()
        predictions, visualized_output = demo.run_on_image(img)
        logger.info("{}: detected {} instances in {:.2f}s".format(
            imgfile, len(predictions["instances"]),
            time.time() - start_time))

        if args.output:
            if os.path.isdir(args.output):
        "is_thresh"]
    cfg.freeze()
    return cfg


if __name__ == "__main__":

    yaml_path = os.path.join(
        Path(__file__).parent.parent, "params", "azure_centermask_SNU.yaml")
    with open(yaml_path) as f:
        params = yaml.load(f, Loader=yaml.FullLoader)
    cfg = setup_cfg(params)
    os.environ["CUDA_VISIBLE_DEVICES"] = params["is_gpu_id"]
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print("==> Loading CenterMask on", device, params["is_gpu_id"])
    demo = VisualizationDemo(cfg)
    sock = su.initialize_client(params["tcp_ip"],
                                params["centermask_tcp_port"])

    while True:
        img = su.recvall_image(sock)
        predictions, vis_output = demo.run_on_image(img)
        pred_masks = predictions["instances"].pred_masks.cpu().detach().numpy(
        )  # (N, H, W),
        pred_boxes = predictions["instances"].pred_boxes.tensor.cpu().detach(
        ).numpy()  # (x1, y1, x2, y2)
        pred_scores = predictions["instances"].scores.cpu().detach().numpy(
        )  # a vector of N confidence scores.
        pred_classes = predictions["instances"].pred_classes.cpu().detach(
        ).numpy()  # [0, num_categories).
        vis_img = cv2.resize(vis_output.get_image()[:, :, ::-1],
        cfg.merge_from_file(model_zoo.get_config_file("COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml"))
        cfg.MODEL.WEIGHTS = ROOT_DIR + "/configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x_model.pkl"
    elif segmentation == 'mrcnn':
        cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml"))
        cfg.MODEL.WEIGHTS = '/home/users/t/tim.schroeder/object_rep/project_code/detectron2/configs/COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x_model.pkl'
    if not hpc:
        cfg.MODEL.DEVICE = 'cpu' #only on local device

    cfg.freeze()
    return cfg


if __name__ == "__main__":
    cfg = setup_cfg()
    predictor = DefaultPredictor(cfg)
    demo = VisualizationDemo(cfg)

    video_path_list = [video for video in glob.glob(DATA_DIR + '/*')]
    video_path_list.sort()
    for video_path in [video_path_list[12]]:
        output_data = []
        output_info = []
        
        video = cv2.VideoCapture(video_path)
        width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
        frames_per_second = video.get(cv2.CAP_PROP_FPS)
        num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
        basename = basename = os.path.splitext(os.path.basename(video_path))[0]
        
        if os.path.isdir(RESULTS_DIR):
Exemplo n.º 11
0
def matchScore(x, y, mask):
    center = center_of_mass(mask)
    distanceFromCenter = euclidean(center, (y, x))
    size = np.count_nonzero(mask)
    maxDist = 1468
    maxSize = 921600
    score = (((1 / (maxSize**2)) * (size - maxSize)**2) +
             (1 - distanceFromCenter / maxDist)) / 2
    return score


if __name__ == '__main__':

    cfg = setup_cfg()  #Define Classifier for Panoptic Segmentation
    predictor = DefaultPredictor(cfg)
    demo = VisualizationDemo(cfg)
    metadata = demo.metadata

    GAZE_FOLDER_DIR = '/mnt/raid/data/SCIoI/GazeCom_ground_truth/'
    gaze_folder_list = [video for video in glob.glob(GAZE_FOLDER_DIR + '/*')]
    gaze_folder_list.sort()

    VIDEO_DIR = '/mnt/raid/data/SCIoI/GazeCom_videos/'
    video_path_list = [video for video in glob.glob(VIDEO_DIR + '/*')]
    video_path_list.sort()

    PROTO_OBJECTS = '/mnt/raid/data/SCIoI/tim/salData/'

    detect_path_list = [
        video for video in glob.glob(RESULTS_DIR + 'detect_video/' + '*.avi')
    ]
Exemplo n.º 12
0
def do_test():
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    # 注册数据集
    register_dataset()

    model_demo = VisualizationDemo(cfg)

    predict_list = []
    real_list = []
    # for path in tqdm.tqdm(args.input, disable=not args.output):
    for imgfile in os.listdir(INPUT_IMG_PATH):

        # use PIL, to be consistent with evaluation
        img_fullName = os.path.join(INPUT_IMG_PATH, imgfile)
        img = read_image(img_fullName, format="BGR")
        start_time = time.time()
        predictions, visualized_output = model_demo.run_on_image_detection(img)
        # imgpath.json这个文件里面是图片文件名和类别真值的对应表
        # 模型输出的类别中 0和1 分别代表水印和正常元件,而 2,3,4,5,6 分别是那五种瑕疵,
        # 还缺少一个表示false的数字,所以我们用7来表示
        # 所以在imgpath.json文件中你只能看到2,3,4,5,6,7这6种数字,其实分别就代表了我们最终要分类的6个类别

        img_paths = tools.get_all_class_label_infos()
        pred = predictions.get('pred_classes').tolist()
        if len(pred) == 0:
            print(imgfile)
            predict_list.append('1')
        else:
            predict_list.append(str(pred[0] + 1))
        # temp = str(imgfile).split('_')
        # if temp[1].endswith('.jpg'):
        #     imgfile = temp[0] + '_' + temp[1]
        # else:
        #     imgfile = temp[0] + '_' + temp[1] + '.jpg'
        real_list.append(img_paths[imgfile])
        #visualized_output.save(os.path.join('imgout', imgfile))
        # print(flaw_only(predictions))
        # log会在控制台输出预测所花的时间等等信息
        # logger.info(
        #     "{}: detected {} instances in {:.2f}s".format(
        #         imgfile, len(predictions["instances"]), time.time() - start_time
        #     )
        # )
        # 这个是保存预测错误的图片,会在原图上额外绘画出识别的矩形框、mask、类别、分数等等,好让我们看看预测错误的图片长什么样并且是哪里预测错了
        # if determine_final_class(predictions) != img_paths[imgfile]:
        #     temp = ['watermark', 'commom1', '01', '02', '05', '07', 'other', 'false']
        #     tempstr = temp[img_paths[imgfile]] + '-' + temp[determine_final_class(predictions)]
        #     out_filename = os.path.join(OUTPUT_IMG_PATH, tempstr + imgfile)
        #     visualized_output.save(out_filename)

    # 根据上面两种函数,determine_final_class和contain_class两种方案来统计的情况,生成混淆矩阵、precision、recall
    cm = confusion_matrix(real_list, predict_list)
    ps = precision_score(real_list, predict_list, average=None)
    rc = recall_score(real_list, predict_list, average=None)
    print(cm)
    print(['{:.2%}'.format(x) for x in ps])
    print(['{:.2%}'.format(x) for x in rc])
    '''