You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.8 KiB
64 lines
2.8 KiB
2 years ago
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||
2 years ago
|
|
||
2 years ago
|
import torch
|
||
|
|
||
1 year ago
|
from ultralytics.engine.results import Results
|
||
|
from ultralytics.models.yolo.detect.predict import DetectionPredictor
|
||
|
from ultralytics.utils import DEFAULT_CFG, ROOT, ops
|
||
2 years ago
|
|
||
|
|
||
|
class SegmentationPredictor(DetectionPredictor):
|
||
|
|
||
2 years ago
|
def __init__(self, cfg=DEFAULT_CFG, overrides=None, _callbacks=None):
|
||
|
super().__init__(cfg, overrides, _callbacks)
|
||
|
self.args.task = 'segment'
|
||
|
|
||
2 years ago
|
def postprocess(self, preds, img, orig_imgs):
|
||
2 years ago
|
"""TODO: filter by classes."""
|
||
2 years ago
|
p = ops.non_max_suppression(preds[0],
|
||
2 years ago
|
self.args.conf,
|
||
|
self.args.iou,
|
||
2 years ago
|
agnostic=self.args.agnostic_nms,
|
||
|
max_det=self.args.max_det,
|
||
2 years ago
|
nc=len(self.model.names),
|
||
2 years ago
|
classes=self.args.classes)
|
||
2 years ago
|
results = []
|
||
2 years ago
|
proto = preds[1][-1] if len(preds[1]) == 3 else preds[1] # second output is len 3 if pt, but only 1 if exported
|
||
2 years ago
|
for i, pred in enumerate(p):
|
||
2 years ago
|
orig_img = orig_imgs[i] if isinstance(orig_imgs, list) else orig_imgs
|
||
2 years ago
|
path = self.batch[0]
|
||
2 years ago
|
img_path = path[i] if isinstance(path, list) else path
|
||
|
if not len(pred): # save empty boxes
|
||
|
results.append(Results(orig_img=orig_img, path=img_path, names=self.model.names, boxes=pred[:, :6]))
|
||
2 years ago
|
continue
|
||
|
if self.args.retina_masks:
|
||
2 years ago
|
if not isinstance(orig_imgs, torch.Tensor):
|
||
|
pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape)
|
||
|
masks = ops.process_mask_native(proto[i], pred[:, 6:], pred[:, :4], orig_img.shape[:2]) # HWC
|
||
2 years ago
|
else:
|
||
2 years ago
|
masks = ops.process_mask(proto[i], pred[:, 6:], pred[:, :4], img.shape[2:], upsample=True) # HWC
|
||
2 years ago
|
if not isinstance(orig_imgs, torch.Tensor):
|
||
|
pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape)
|
||
2 years ago
|
results.append(
|
||
|
Results(orig_img=orig_img, path=img_path, names=self.model.names, boxes=pred[:, :6], masks=masks))
|
||
2 years ago
|
return results
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
def predict(cfg=DEFAULT_CFG, use_python=False):
|
||
2 years ago
|
"""Runs YOLO object detection on an image or video source."""
|
||
2 years ago
|
model = cfg.model or 'yolov8n-seg.pt'
|
||
|
source = cfg.source if cfg.source is not None else ROOT / 'assets' if (ROOT / 'assets').exists() \
|
||
|
else 'https://ultralytics.com/images/bus.jpg'
|
||
2 years ago
|
|
||
2 years ago
|
args = dict(model=model, source=source)
|
||
2 years ago
|
if use_python:
|
||
|
from ultralytics import YOLO
|
||
|
YOLO(model)(**args)
|
||
|
else:
|
||
2 years ago
|
predictor = SegmentationPredictor(overrides=args)
|
||
2 years ago
|
predictor.predict_cli()
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
if __name__ == '__main__':
|
||
2 years ago
|
predict()
|