-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
LIN Yun
committed
Jun 30, 2020
1 parent
80974bc
commit 715221e
Showing
8 changed files
with
6,664 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""Inference on a single image. | ||
""" | ||
|
||
from argparse import ArgumentParser | ||
|
||
import cv2 | ||
from detectron2.config import get_cfg | ||
from detectron2.data import MetadataCatalog | ||
from detectron2.engine import DefaultPredictor | ||
from detectron2.utils.visualizer import Visualizer | ||
from PIL import Image | ||
|
||
import detectron2_1 | ||
|
||
|
||
def main(args): | ||
# Configure weights and confidence threshold | ||
cfg = get_cfg() | ||
cfg.merge_from_file(args.config_path) | ||
cfg.MODEL.WEIGHTS = args.weights_path | ||
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.conf_threshold | ||
|
||
# Initialize model | ||
predictor = DefaultPredictor(cfg) | ||
|
||
# Load image as numpy array | ||
im = cv2.imread(args.img_path) | ||
|
||
# Perform inference | ||
outputs = predictor(im) | ||
|
||
# Set dataset categories | ||
# FIXME: Specifc to this task | ||
MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes = ["box", "logo"] | ||
|
||
# Draw instance predictions | ||
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0])) | ||
out = v.draw_instance_predictions(outputs["instances"].to("cpu")) | ||
|
||
# Image with instance predictions as numpy array | ||
pred = out.get_image() | ||
|
||
# Save image with instance predictions | ||
Image.fromarray(pred).save(args.output_path) | ||
|
||
|
||
def get_args(): | ||
parser = ArgumentParser() | ||
|
||
parser.add_argument("--img-path", help="Path to image to perform inference on") | ||
parser.add_argument("--config-path", help="Path to config file of model") | ||
parser.add_argument("--weights-path", help="Path to model weights") | ||
parser.add_argument( | ||
"--output-path", help="Path to save image with instance predictions" | ||
) | ||
parser.add_argument( | ||
"--conf-threshold", | ||
type=float, | ||
default="0.05", | ||
help="Confidence threshold of predictions, default 0.05", | ||
) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
if __name__ == "__main__": | ||
args = get_args() | ||
main(args) |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.