-
Notifications
You must be signed in to change notification settings - Fork 151
/
inference_bbox.py
53 lines (45 loc) · 1.97 KB
/
inference_bbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from os.path import join, isfile, isdir
from os import listdir
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from argparse import ArgumentParser
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
import numpy as np
import cv2
# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
import torch
from tqdm import tqdm
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml")
predictor = DefaultPredictor(cfg)
parser = ArgumentParser()
parser.add_argument("--test_img_dir", type=str, default='example', help='testing images folder')
parser.add_argument('--filter_no_obj', action='store_true')
args = parser.parse_args()
input_dir = args.test_img_dir
image_list = [f for f in listdir(input_dir) if isfile(join(input_dir, f))]
output_npz_dir = "{0}_bbox".format(input_dir)
if os.path.isdir(output_npz_dir) is False:
print('Create path: {0}'.format(output_npz_dir))
os.makedirs(output_npz_dir)
for image_path in tqdm(image_list):
img = cv2.imread(join(input_dir, image_path))
lab_image = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l_channel, a_channel, b_channel = cv2.split(lab_image)
l_stack = np.stack([l_channel, l_channel, l_channel], axis=2)
outputs = predictor(l_stack)
save_path = join(output_npz_dir, image_path.split('.')[0])
pred_bbox = outputs["instances"].pred_boxes.to(torch.device('cpu')).tensor.numpy()
pred_scores = outputs["instances"].scores.cpu().data.numpy()
if args.filter_no_obj is True and pred_bbox.shape[0] == 0:
print('delete {0}'.format(image_path))
os.remove(join(input_dir, image_path))
continue
np.savez(save_path, bbox = pred_bbox, scores = pred_scores)