-
Notifications
You must be signed in to change notification settings - Fork 1
/
detectron_ex.py
63 lines (49 loc) · 2.17 KB
/
detectron_ex.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
54
55
56
57
58
59
60
61
62
63
import torch
import torchvision
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
# import some common libraries
import numpy as np
import os, json, cv2, random
# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.data import MetadataCatalog
# Get metadata for COCO dataset
metadata = MetadataCatalog.get("coco_2017_train")
# Access class names
class_names = metadata.thing_classes
print(class_names) # This will print the class names in the COCO dataset
import sys
if len(sys.argv) < 2:
print("Usage: python upload_image.py <image_path>")
sys.exit(1)
image_path = sys.argv[1]
image = cv2.imread(image_path)
cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
cfg.MODEL.DEVICE = "cpu"
predictor = DefaultPredictor(cfg)
outputs = predictor(image)
print(outputs["instances"].pred_classes)
print(outputs["instances"].pred_boxes)
# v = Visualizer(image[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
# out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
# cv2.imshow(out.get_image()[:, :, ::-1])
v = Visualizer(image[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
# Convert the output image to a format that OpenCV can use
output_image = out.get_image()[:, :, ::-1] # Convert BGR to RGB format for OpenCV
# Display the image
cv2.imshow("Detected Objects", output_image)
# cv2.imshow("Uploaded Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()