model.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. from pathlib import Path
  3. from ultralytics.engine.model import Model
  4. from .predict import FastSAMPredictor
  5. from .val import FastSAMValidator
  6. class FastSAM(Model):
  7. """
  8. FastSAM model interface.
  9. Example:
  10. ```python
  11. from ultralytics import FastSAM
  12. model = FastSAM("last.pt")
  13. results = model.predict("ultralytics/assets/bus.jpg")
  14. ```
  15. """
  16. def __init__(self, model="FastSAM-x.pt"):
  17. """Call the __init__ method of the parent class (YOLO) with the updated default model."""
  18. if str(model) == "FastSAM.pt":
  19. model = "FastSAM-x.pt"
  20. assert Path(model).suffix not in {".yaml", ".yml"}, "FastSAM models only support pre-trained models."
  21. super().__init__(model=model, task="segment")
  22. def predict(self, source, stream=False, bboxes=None, points=None, labels=None, texts=None, **kwargs):
  23. """
  24. Perform segmentation prediction on image or video source.
  25. Supports prompted segmentation with bounding boxes, points, labels, and texts.
  26. Args:
  27. source (str | PIL.Image | numpy.ndarray): Input source.
  28. stream (bool): Enable real-time streaming.
  29. bboxes (list): Bounding box coordinates for prompted segmentation.
  30. points (list): Points for prompted segmentation.
  31. labels (list): Labels for prompted segmentation.
  32. texts (list): Texts for prompted segmentation.
  33. **kwargs (Any): Additional keyword arguments.
  34. Returns:
  35. (list): Model predictions.
  36. """
  37. prompts = dict(bboxes=bboxes, points=points, labels=labels, texts=texts)
  38. return super().predict(source, stream, prompts=prompts, **kwargs)
  39. @property
  40. def task_map(self):
  41. """Returns a dictionary mapping segment task to corresponding predictor and validator classes."""
  42. return {"segment": {"predictor": FastSAMPredictor, "validator": FastSAMValidator}}