نموذج Fast Segment Anything (FastSAM)
نموذج Fast Segment Anything (FastSAM) هو حل قائم على الشبكات العصبية الالتفافية (CNN) لتنفيذ مهمة تقسيم أي شيء في الوقت الفعلي. وتهدف هذه المهمة إلى تقسيم أي كائن في الصورة استنادًا إلى مطالبات تفاعلية مختلفة يقدمها المستخدم. ويقلل FastSAM متطلبات الحوسبة بدرجة كبيرة مع الحفاظ على أداء تنافسي، ما يجعله خيارًا عمليًا لمجموعة متنوعة من مهام الرؤية الحاسوبية.
شاهد: تتبع الكائنات باستخدام FastSAM مع Ultralytics
بنية النموذج#

نظرة عامة#
صُمم FastSAM لمعالجة قيود نموذج تقسيم أي شيء (SAM)، وهو نموذج Transformer ضخم يتطلب موارد حوسبة كبيرة. ويفصل FastSAM مهمة تقسيم أي شيء إلى مرحلتين متتاليتين: تقسيم جميع الكائنات ثم الاختيار الموجّه بالمطالبات. تستخدم المرحلة الأولى YOLOv8-seg لإنتاج أقنعة تقسيم جميع الكائنات في الصورة. وفي المرحلة الثانية، يُخرج النموذج المنطقة محل الاهتمام الموافقة للمطالبة.
الميزات الرئيسية#
-
حل يعمل في الوقت الفعلي: بفضل الكفاءة الحاسوبية للشبكات العصبية الالتفافية، يقدم FastSAM حلًا يعمل في الوقت الفعلي لمهمة تقسيم أي شيء، ما يجعله مفيدًا للتطبيقات الصناعية التي تتطلب نتائج سريعة.
-
الكفاءة والأداء: يقلل FastSAM متطلبات الحوسبة والموارد بدرجة كبيرة مع تحقيق أداء يضاهي SAM، ما يتيح استخدامه في تطبيقات الوقت الفعلي.
-
التقسيم الموجّه بالمطالبات: يستطيع FastSAM تقسيم أي كائن في الصورة استنادًا إلى مطالبات تفاعلية متنوعة يقدمها المستخدم، ما يوفر المرونة والقدرة على التكيف في مختلف السيناريوهات.
-
يعتمد على YOLOv8-seg: يعتمد FastSAM على YOLOv8-seg، وهو كاشف للكائنات مزوّد بفرع لتقسيم الكائنات. وهذا يتيح له إنتاج أقنعة تقسيم جميع الكائنات في الصورة بفعالية.
-
نتائج تنافسية على المعايير: في مهمة اقتراح الكائنات على MS COCO، يحقق FastSAM درجات مرتفعة بسرعة تفوق SAM بدرجة كبيرة على NVIDIA RTX 3090 واحد، ما يبرهن على كفاءته وقدراته.
-
تطبيقات عملية: يقدم هذا النهج حلًا عمليًا للعديد من مهام الرؤية الحاسوبية، إذ يعمل بسرعة تفوق أساليب تقسيم أي شيء السابقة بعشرات إلى مئات المرات.
-
إمكانية ضغط النماذج: يبرهن FastSAM على إمكانية اتباع نهج يقلل الجهد الحاسوبي بدرجة كبيرة عبر إدخال انحياز مسبق اصطناعي إلى البنية، ما يفتح آفاقًا جديدة لبنى النماذج الكبيرة لمهام الرؤية العامة.
النماذج المتاحة والمهام المدعومة وأوضاع التشغيل#
يعرض هذا الجدول النماذج المتاحة وأوزانها المدرّبة مسبقًا والمهام التي تدعمها ومدى توافقها مع أوضاع التشغيل المختلفة مثل التدريب والتحقق والاستدلال والتصدير. تشير ✅ إلى الأوضاع المدعومة و❌ إلى الأوضاع غير المدعومة.
| نوع النموذج | الأوزان المدرّبة مسبقاً | المهام المدعومة | التدريب | التحقق | الاستدلال | التصدير |
|---|---|---|---|---|---|---|
| FastSAM-s | FastSAM-s.pt | تجزئة الكائنات | ❌ | ✅ | ✅ | ✅ |
| FastSAM-x | FastSAM-x.pt | تجزئة الكائنات | ❌ | ✅ | ✅ | ✅ |
مقارنة FastSAM بنماذج YOLO#
نقارن هنا FastSAM-s بمتغيرات SAM من Meta ونماذج التقسيم من Ultralytics، ومنها YOLO26:
| النموذج | الحجم (MB) | المعلمات (M) | السرعة (CPU) (مللي ثانية/صورة) |
|---|---|---|---|
| Meta SAM-b | 375 | 93.7 | 41703 |
| Meta SAM2-b | 162 | 80.8 | 28867 |
| Meta SAM2-t | 78.1 | 38.9 | 23430 |
| MobileSAM | 40.7 | 10.1 | 23802 |
| FastSAM-s مع بنية YOLOv8 | 23.9 | 11.8 | 58.0 |
| Ultralytics YOLOv8n-seg | 7.1 (أصغر 11.0 مرة) | 3.4 (أقل 11.4 مرة) | 24.8 (أسرع 945 مرة) |
| Ultralytics YOLO11n-seg | 6.2 (أصغر 12.6 مرة) | 2.9 (أقل 13.4 مرة) | 24.3 (أسرع 964 مرة) |
| Ultralytics YOLO26n-seg | 6.7 (أصغر 11.7 مرة) | 2.7 (أقل 14.4 مرة) | 25.2 (أسرع 930 مرة) |
تُظهر هذه المقارنة الفروق الكبيرة في أحجام النماذج وسرعاتها بين إصدارات SAM ونماذج تقسيم YOLO. وعلى الرغم من أن SAM يوفر إمكانات فريدة للتقسيم التلقائي، فإن نماذج YOLO، ولا سيما YOLOv8n-seg وYOLO11n-seg وYOLO26n-seg، أصغر وأسرع وأكثر كفاءة حسابيًا بدرجة كبيرة.
قِيسَت سرعات SAM باستخدام PyTorch، وسرعات YOLO باستخدام ONNX Runtime. أُجريت الاختبارات على Apple M4 Air لعام 2025 بذاكرة RAM سعتها 16GB باستخدام torch==2.10.0 وultralytics==8.4.31 وonnxruntime==1.24.4. لإعادة إجراء هذا الاختبار:
from ultralytics import ASSETS, SAM, YOLO, FastSAM
# تحليل أداء SAM2-t وSAM2-b وSAM-b وMobileSAM
for file in ["sam_b.pt", "sam2_b.pt", "sam2_t.pt", "mobile_sam.pt"]:
model = SAM(file)
model.info()
model(ASSETS)
# أنشئ ملفًا تعريفيًا لـ FastSAM-s
model = FastSAM("FastSAM-s.pt")
model.info()
model(ASSETS)
# تحليل أداء نماذج YOLO (ONNX)
for file_name in ["yolov8n-seg.pt", "yolo11n-seg.pt", "yolo26n-seg.pt"]:
model = YOLO(file_name)
model.info()
onnx_path = model.export(format="onnx", dynamic=True, nms=False) # رأس NMS-free في YOLO26؛ لا إجراء لـ YOLOv8/YOLO11
model = YOLO(onnx_path)
model(ASSETS)أمثلة الاستخدام#
يسهل دمج نماذج FastSAM في تطبيقات Python. توفّر Ultralytics واجهة برمجة تطبيقات Python وأوامر CLI.
الاستخدام للتنبؤ#
لتجزئة صورة، استخدم الطريقة predict كما هو موضح أدناه:
from ultralytics import FastSAM
# تحديد مصدر الاستدلال
source = "path/to/bus.jpg"
# إنشاء نموذج FastSAM
model = FastSAM("FastSAM-s.pt") # أو FastSAM-x.pt
# تشغيل الاستدلال على صورة
everything_results = model(source, device="cpu", retina_masks=True, imgsz=1024, conf=0.4, iou=0.9)
# تنفيذ الاستدلال باستخدام مطالبة المربعات المحيطة
results = model(source, bboxes=[439, 437, 524, 709])
# تشغيل الاستدلال باستخدام مطالبة نقاط
results = model(source, points=[[200, 200]], labels=[1])
# تشغيل الاستدلال باستخدام مطالبة نصية
results = model(source, texts="a photo of a dog")
# تشغيل الاستدلال باستخدام مطالبات مربعات الإحاطة والنقاط والنصوص في الوقت نفسه
results = model(source, bboxes=[439, 437, 524, 709], points=[[200, 200]], labels=[1], texts="a photo of a dog")يوضح هذا المقتطف سهولة تحميل نموذج مدرّب مسبقًا وإجراء تنبؤ على صورة.
نفّذ الاستدلال على الصورة مرة واحدة للحصول على جميع عناصر results المقسّمة، ثم طبّق المطالبات على هذه النتائج عدة مرات دون إعادة تشغيل الاستدلال.
from ultralytics.models.fastsam import FastSAMPredictor
# إنشاء FastSAMPredictor
overrides = {"conf": 0.25, "task": "segment", "mode": "predict", "model": "FastSAM-s.pt", "save": False, "imgsz": 1024}
predictor = FastSAMPredictor(overrides=overrides)
# تجزئة كل شيء
everything_results = predictor("ultralytics/assets/bus.jpg")
# استدلال المطالبات
bbox_results = predictor.prompt(everything_results, bboxes=[[200, 200, 300, 300]])
point_results = predictor.prompt(everything_results, points=[200, 200])
text_results = predictor.prompt(everything_results, texts="a photo of a dog")جميع عناصر results المعادة في الأمثلة أعلاه هي كائنات Results تتيح الوصول إلى الأقنعة المتوقعة والصورة المصدر.
الاستخدام للتحقق#
لا يدعم FastSAM سوى كشف فئة واحدة من الكائنات وتقسيمها، لذا يتعرف على جميع الكائنات ويقسّمها باعتبارها من الفئة نفسها. عند إعداد مجموعة البيانات، حوّل جميع معرّفات فئات الكائنات إلى 0.
يمكن إجراء التحقق من صحة النموذج على مجموعة بيانات كما يلي:
from ultralytics import FastSAM
# إنشاء نموذج FastSAM
model = FastSAM("FastSAM-s.pt") # أو FastSAM-x.pt
# تحقّق من النموذج
results = model.val(data="coco8-seg.yaml")تتبُّع الاستخدام#
لإجراء تتبُّع الكائنات في صورة، استخدم طريقة track كما هو موضح أدناه:
from ultralytics import FastSAM
# إنشاء نموذج FastSAM
model = FastSAM("FastSAM-s.pt") # أو FastSAM-x.pt
# تتبُّع باستخدام نموذج FastSAM على مقطع فيديو
results = model.track(source="path/to/video.mp4", imgsz=640)الاستخدام الرسمي لـ FastSAM#
يتوفر FastSAM أيضًا مباشرةً من مستودع CASIA-LMC-Lab/FastSAM. إليك لمحة موجزة عن الخطوات المعتادة التي يمكنك اتباعها لاستخدام FastSAM:
التثبيت#
-
استنسخ مستودع FastSAM:
git clone https://github.com/CASIA-LMC-Lab/FastSAM.git -
أنشئ بيئة Conda باستخدام Python 3.9 ونشّطها:
conda create -n FastSAM python=3.9 conda activate FastSAM -
انتقل إلى المستودع المستنسخ وثبّت الحزم المطلوبة:
cd FastSAM pip install -r requirements.txt -
ثبّت نموذج CLIP:
pip install git+https://github.com/ultralytics/CLIP.git
مثال على الاستخدام#
-
نزّل نقطة تحقق النموذج.
-
استخدم FastSAM للاستدلال. أمثلة على الأوامر:
-
قسّم جميع الكائنات في صورة:
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg -
قسّم كائنات محددة باستخدام مطالبة نصية:
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg --text_prompt "the yellow dog" -
قسّم الكائنات داخل مربع إحاطة (قدّم إحداثيات المربع بتنسيق xywh):
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg --box_prompt "[570,200,230,400]" -
قسّم الكائنات القريبة من نقاط محددة:
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg --point_prompt "[[520,360],[620,300]]" --point_label "[1,0]"
-
بالإضافة إلى ذلك، يمكنك تجربة FastSAM من خلال عرض Colab التوضيحي الخاص بـ CASIA-LMC-Lab.
الاستشهادات والشكر والتقدير#
نود أن نعرب عن تقديرنا لمؤلفي FastSAM على إسهاماتهم المهمة في مجال تجزئة المثيلات في الوقت الفعلي:
@misc{zhao2023fast,
title={Fast Segment Anything},
author={Xu Zhao and Wenchao Ding and Yongqi An and Yinglong Du and Tao Yu and Min Li and Ming Tang and Jinqiao Wang},
year={2023},
eprint={2306.12156},
archivePrefix={arXiv},
primaryClass={cs.CV}
}يمكن العثور على ورقة FastSAM الأصلية على arXiv. أتاح المؤلفون عملهم للجمهور، ويمكن الوصول إلى قاعدة الشفرة على GitHub. نقدّر جهودهم في تطوير هذا المجال وإتاحة عملهم للمجتمع الأوسع.
الأسئلة الشائعة#
FastSAM، اختصارًا لـ Fast Segment Anything Model، هو حل قائم على الشبكات العصبية الالتفافية (CNN) يعمل في الوقت الفعلي، وقد صُمم لتقليل متطلبات الحوسبة مع الحفاظ على أداء عالٍ في مهام تقسيم الكائنات. وعلى خلاف نموذج تقسيم أي شيء (SAM)، الذي يستخدم بنية أثقل قائمة على Transformer، يستخدم FastSAM نموذج Ultralytics YOLOv8-seg لتنفيذ تقسيم الكائنات بكفاءة على مرحلتين: تقسيم جميع الكائنات، ثم الاختيار الموجّه بالمطالبات.
يحقق FastSAM التقسيم في الوقت الفعلي من خلال فصل مهمة التقسيم إلى مرحلتي تقسيم جميع الكائنات باستخدام YOLOv8-seg والاختيار الموجّه بالمطالبات. وبفضل الكفاءة الحاسوبية للشبكات العصبية الالتفافية، يقلل FastSAM متطلبات الحوسبة والموارد بدرجة كبيرة مع الحفاظ على أداء تنافسي. ويتيح هذا النهج المؤلف من مرحلتين لـ FastSAM تقديم تقسيم سريع وفعّال يناسب التطبيقات التي تتطلب نتائج عاجلة.
يُعد FastSAM عمليًا لمجموعة متنوعة من مهام الرؤية الحاسوبية التي تتطلب أداء تجزئة في الوقت الفعلي. وتشمل التطبيقات:
- الأتمتة الصناعية لمراقبة الجودة وضمانها
- تحليل الفيديو في الوقت الفعلي للأمن والمراقبة
- المركبات ذاتية القيادة لاكتشاف الكائنات وتجزيئها
- التصوير الطبي لمهام التجزئة الدقيقة والسريعة
تجعل قدرته على التعامل مع مطالبات التفاعل المختلفة من المستخدم FastSAM قابلًا للتكيّف ومرنًا في مختلف السيناريوهات.
لاستخدام FastSAM للاستدلال في Python، يمكنك اتباع المثال أدناه:
from ultralytics import FastSAM # تحديد مصدر الاستدلال source = "path/to/bus.jpg" # إنشاء نموذج FastSAM model = FastSAM("FastSAM-s.pt") # أو FastSAM-x.pt # تشغيل الاستدلال على صورة everything_results = model(source, device="cpu", retina_masks=True, imgsz=1024, conf=0.4, iou=0.9) # تنفيذ الاستدلال باستخدام مطالبة المربعات المحيطة results = model(source, bboxes=[439, 437, 524, 709]) # تشغيل الاستدلال باستخدام مطالبة نقاط results = model(source, points=[[200, 200]], labels=[1]) # تشغيل الاستدلال باستخدام مطالبة نصية results = model(source, texts="a photo of a dog") # تشغيل الاستدلال باستخدام مطالبات مربعات الإحاطة والنقاط والنصوص في الوقت نفسه results = model(source, bboxes=[439, 437, 524, 709], points=[[200, 200]], labels=[1], texts="a photo of a dog")لمزيد من التفاصيل حول أساليب الاستدلال، راجع قسم استخدام التنبؤ في الوثائق.
يدعم FastSAM أنواعًا متعددة من المطالبات لتوجيه مهام التجزئة:
- مطالبة «الكل»: تُنشئ تجزئة لجميع الكائنات الظاهرة.
- مطالبة مربع الإحاطة (BBox): تحدد الكائنات داخل مربع إحاطة محدد. إذا لم يتداخل المربع مع أي قناع مكتشف، فلن يحدد أي شيء.
- المطالبة النصية: تستخدم نصًا وصفيًا لتقسيم الكائنات المطابقة للوصف.
- مطالبة النقطة: تقسّم الكائنات القريبة من نقاط يحددها المستخدم.
تتيح هذه المرونة لـ FastSAM التكيّف مع مجموعة واسعة من سيناريوهات تفاعل المستخدم، مما يعزز فائدته في مختلف التطبيقات. لمزيد من المعلومات حول استخدام هذه المطالبات، راجع قسم الميزات الرئيسية.
التعليقات26
اطرح سؤالًا، وشارك نتائجك، أو ساعد شخصًا آخر.
qinfendekaizhou
why is the postprocess times is so long
PrashantDixit0(تم التعديل)
@qinfendekaizhou, can you share the reproducible code for it? Or the reference from where u noticed this.
In general, post-process time gets increased because of the post-processes involved to show the results, like
- NMS
- filtering by confidence score/ labels
- Scaling results to the original given inputs
- Number of objects to be annotated,
- Type of annotation, like BB or Segmentation
juliopchile
Will someone try to do a FastSAM for SAM2?
shahedmomenzadeh
Hi, I have a question Can I use the generated bbox cordinates by a yolo detection model like yolov11 as bboxes prompt for FastSAM ?
Glenn JocherUltralytics@shahedmomenzadeh yes, you can use YOLO detection model-generated bbox coordinates as prompts for FastSAM. The FastSAM documentation shows examples of passing bbox coordinates directly to the model using either the Python API or CLI. Just ensure your coordinates are in XYXY format as shown in the examples.
ismailwale
How can I make an inference of a video using frame by frame in loop using FastSam ?because I tried multiple times making inference directly with video predictor I constantly running out of memory despite setting stream =True since SAM is trying to keep the memory of 2000 frames of the video at ones . I will appreciate any ideal on how to predict and segment the video as a single frame image at every instance and put everything in a loop.
Similar question win MobileSam
Glenn JocherUltralytics@ismailwale to process a video frame by frame and avoid memory overload with FastSAM or MobileSAM, load the video as a frame sequence using a tool like OpenCV. Loop through each frame, run inference on it individually, and clear any cached results to free up memory between frames. For reference on frame-wise usage, consult OpenCV and FastSAM integration. Let me know if you need further clarifications!
ismailwale
Thanks so much for your guide .I appreciate it sir.
llu025
Thank you all for your great work! I am having troubles with prompting with a bounding box, it seems the FastSAM model ignores it and detects objects all over the video frames. This is how I obtain the results:
results = model( input_filename, device=device, imgsz=(736, 1280), verbose=False, stream=True, bboxes=[0, 0, 360, 640], retina_masks=True, conf=0.4, iou=0.9, )I also tried with
bboxes=[[0, 0, 360, 640]],without success. The model should detect objects in the top left quadrant of the video, instead it detects everywhere. Also, it oddly does not raise any error or warnings, both withbboxesas list of integers orbboxesas list of lists of integers.
Glenn JocherUltralyticsIt seems the bounding box prompts aren't being applied as expected. Ensure the
bboxesformat is correct (e.g.,[[x1, y1, x2, y2]]as a list of lists). Additionally, try using theFastSAMPredictorapproach for more controlled prompt application, as shown in this example. Let me know if the issue persists!llu025
Thank you @glenn-jocher for the "prompt" response ;-)
I need to use
stream=Truesince I am working with large videos. I cannot use it coupled withFastSAMPredictor, because it does not support it. My only alternative is to grab frames on my own with OpenCV, is that correct?By the way, in your examples in here
bboxesis passed as a list of integers, I followed those in the beginning. I see in the examples withFastSAMPredictorthat a list of lists is used instead. In any case, I ensured that I was using a list of lists, and it did not work anyway.Here's a minimal example of what I am trying to do, i.e., detecting objects in the top left part of the video:
from ultralytics import FastSAM import os import ffmpeg input_filename = "test_video.mp4" base_name = os.path.splitext(os.path.basename(input_filename))[0] output_filename = os.path.join("output", f"{base_name}.avi") model = FastSAM("models/FastSAM-x.pt") process2 = ( ffmpeg.input( "pipe:", format="rawvideo", pix_fmt="rgb24", s="1280x720", loglevel="-8", ) .output( output_filename, pix_fmt="yuv420p", vcodec="libx264", loglevel="-8", crf=17, ) .overwrite_output() .run_async(pipe_stdin=True) ) results = model( input_filename, device="cuda", imgsz=(736, 1280), verbose=False, stream=True, bboxes=[[0, 0, 360, 640]], retina_masks=True, conf=0.4, iou=0.9, ) for result in results: frame = result.plot() # Save on disk with process2 process2.stdin.write(frame.tobytes()) process2.stdin.close() process2.wait()
Glenn JocherUltralyticsThank you for the detailed explanation and example. Yes, you're correct—using OpenCV to manually grab frames is a valid and efficient approach, especially when working with
stream=Truefor large videos, as it avoids memory issues. Regardingbboxes, FastSAM expects a list of lists in the format[[x1, y1, x2, y2]]. If this still doesn't work as intended, please ensure your bounding box coordinates are correct and within the frame dimensions. Let us know if you encounter any further issues!llu025
I still encounter the same issue. I used
bboxes=[[20, 20, 500, 500]]just to be 100% sure the coordinates are within the the frame dimensions (1280x720), nothing changed. Since I have to grab frames with OpenCV anyway, I will just crop them myself, but I am quite sure this option is not working as intended.
Glenn JocherUltralytics@llu025 thank you for the update. If you’re certain the
bboxesparameter is properly configured and still not working as intended, cropping frames manually with OpenCV before passing them to the model is indeed a reliable workaround. It may also be worth checking the latest FastSAM updates or reporting this as a potential issue to ensure the bounding box prompt functionality is reviewed. Let us know if you need further assistance!
chongkuiqi
Thanks for your great work! I'm using FastSAM-x with bboxes prompt, the original image shape is [720, 1280], and I set 'imgsz' 1024. However, the results mask shape is [576, 1024], but I remembered in ultralytics-8.2.10, I can get masks with the same shape as the original image. May I ask why this change happened ?
chongkuiqi
Sorry, I forgot to set 'Retina_masks', problem solved, thanks again.
feiwenxiong
how many boxes are supported in thsi api, it cound only segment 2 boxes at most.
Glenn JocherUltralytics@feiwenxiong fastSAM should support multiple boxes. If you're experiencing limitations, please ensure your input format is correct. For further assistance, you might want to check the latest updates or documentation.
YuechengLiu
Thanks the authors for the great work!
I'm using FastSAM-x for instance segmentation (bboxes as the prompt), but I find that the shape of the predicted masks mismatch with the bounding box number. (e.g. the model takes 18 bboxes as input, but the shape of the predicted mask is (17, H, W)) . I tried to set
conf=0.01andiou=0.999, but it doesn't work. How can I solve that? Thanks!
Glenn JocherUltralytics@YuechengLiu thank you for your kind words! The issue you're experiencing might be due to a mismatch in the bounding box prompts and the segmentation results. Please ensure you're using the latest version of the FastSAM model. If the problem persists, consider checking the input format and the bounding box coordinates. For further assistance, you can refer to the FastSAM documentation: https://docs.ultralytics.com/models/fast-sam/.
YuechengLiu
Thanks for your kind reply! The bounding boxes I'm using are predicted by yolov10x (result.boxes.xyxy), so I think the input format might be right. I also tried the same bounding boxes for
mobile_sam, and the output dimension is as expected (same as the input dimension). The problem does not always occur. Do you have any idea about that? Thanks!YuechengLiu
And I'm using the latest version of ultralytics
Glenn JocherUltralyticsThanks for confirming you're using the latest version. Given that the issue is intermittent, it might be related to the specific bounding boxes or the image content. Please try running the model with a different set of bounding boxes or images to see if the issue persists. If it does, consider opening an issue on the GitHub repository with detailed information for further investigation.
nguyenvandang1998
Hi! I am using the FastSAM model and trying to obtain information about the bounding box and mask image of the object, but I don't know how to do it. I hope you can help me. Thank you very much!!
nguyenvandang1998
Currently, I can obtain information about the bounding box, but I still don't know how to get the mask image!!
Glenn JocherUltralytics(تم التعديل)Hi there! 🙌 To extract both bounding boxes and mask images using the FastSAM model, you would typically work with the
resultsobject returned by the model's prediction method. For the bounding boxes, you can access them directly as shown in the documentation or examples provided. For the mask images, since FastSAM is designed for real-time segmentation, the results should also include segmentation masks corresponding to detected objects.Here's a simplified way to access both:
from ultralytics import FastSAM # Define your image source source = 'path/to/your/image.jpg' # Load the FastSAM model (ensure you've chosen the correct model path) model = FastSAM('FastSAM-s.pt') # or another appropriate model # Perform inference results = model(source) # Accessing bounding boxes (assuming XYXY format) boxes = results[0].boxes.xyxy # Accessing segmentation masks # The masks are directly associated with detected objects in the results masks = results[0].masks # To visualize or further process masks and boxes, you would iterate through these objectsTo visualize or save the masks, you'd typically iterate over the
masksobject, and each mask can be processed according to your requirements (e.g., overlaying on the original image or saving as separate mask images).Remember, the exact attribute names (
boxes,masks) and methods might vary based on the model output specification, so it's always good to refer to the specific documentation or example codes for guidance.Hope this helps! Let us know if you have more questions. 😊
2