عدّ الكائنات في المناطق باستخدام Ultralytics YOLO26
يتتبع حل RegionCounter في Ultralytics YOLO26 الكائنات عبر إطارات الفيديو، ويعدّ في كل إطار الكائنات التي يقع مركز صندوق الإحاطة الخاص بها داخل كل منطقة تحددها. وتُرسم كل منطقة على الإطار مع عددها المباشر، ما يتيح لك مراقبة عدة مناطق، مثل ممرات المتاجر أو مسارات الطرق أو مناطق الإنتاج، باستدعاء Python واحد أو أمر CLI واحد.
شاهد: عدّ الكائنات في مناطق مختلفة باستخدام Ultralytics YOLO | حلول Ultralytics
لماذا نستخدم العدّ حسب المناطق؟#
- عدة نطاقات في مرور واحد: مرّر قاموسًا من المضلعات المسماة، وسيُعرض على الإطار عدد مستقل لكل نطاق، بحيث يغطي تدفق فيديو واحد كل منطقة تهمك.
- التتبع مضمّن: ينفذ الحل اكتشاف YOLO26 وتتبع الكائنات داخليًا، فلا حاجة إلى إعداد مسار تتبع منفصل.
- أعداد قابلة للقراءة برمجيًا: يعيد كل إطار معالَج كائن نتائج يحتوي على قاموس بأعداد المناطق، جاهزًا للاستخدام في لوحات المعلومات أو التنبيهات أو التسجيل.
تطبيقات واقعية#
| البيع بالتجزئة | شوارع الأسواق |
|---|---|
![]() | ![]() |
| عدّ الأشخاص في المناطق باستخدام Ultralytics YOLO | عدّ الحشود في المناطق باستخدام Ultralytics YOLO |
كيفية عدّ الكائنات في المناطق باستخدام Ultralytics YOLO#
شغّل حلّ RegionCounter على مصدر فيديو من CLI أو Python. يمكن أن تكون المنطقة قائمة نقاط (لنطاق واحد) أو قاموسًا يربط أسماء النطاقات بقوائم النقاط. يعدّ مثال Python أدناه الكائنات في منطقتين مسمّاتين، ويكتب الإطارات المعالَجة في ملف فيديو ناتج:
# Run a region counting example
yolo solutions region show=True
# Pass a source video
yolo solutions region source="path/to/video.mp4"
# Pass a custom region
yolo solutions region region="[(20, 400), (1080, 400), (1080, 360), (20, 360)]"وسيطات RegionCounter()#
إليك جدولًا بمعاملات RegionCounter:
| المعامل | النوع | الافتراضي | الوصف |
|---|---|---|---|
model | str | None | مسار ملف نموذج Ultralytics YOLO. |
region | list أو dict | None | النقاط التي تحدد منطقة الاهتمام، سواء أكانت قائمة من الصفوف (x, y) أم قاموسًا يربط أسماء المناطق بقوائم النقاط لمناطق متعددة (في RegionCounter فقط). عندما يكون None، تستخدم الحلول التي تتطلب منطقة إعدادًا افتراضيًا محددًا مسبقًا. |
line_width | int | 2 | سماكة الخط للصناديق والنقاط المفتاحية والعدادات التي يرسمها الحل. |
verbose | bool | True | يفعّل سجل الحل لكل إطار، ويشمل شكل المدخل وعدد الفئات وسرعة المعالجة. يظل استدعاء التتبع نفسه صامتًا دائمًا. |
يتيح حلّ RegionCounter استخدام معلمات تتبع الكائنات:
| المعامل | النوع | الافتراضي | الوصف |
|---|---|---|---|
tracker | str | 'tracktrack.yaml' | يحدد خوارزمية التتبع المطلوب استخدامها. الخيارات المضمنة: botsort.yaml، bytetrack.yaml، ocsort.yaml، deepocsort.yaml، fasttrack.yaml، tracktrack.yaml. |
conf | float | 0.25 | يضبط عتبة الثقة للكشوفات؛ إذ تسمح القيم الأقل بتتبع مزيد من الكائنات، لكنها قد تتضمن إيجابيات كاذبة. |
iou | float | 0.5 | يضبط عتبة التقاطع على الاتحاد (IoU) لتصفية الكشوفات المتداخلة. |
classes | list | None | يصفّي النتائج حسب فهرس الفئة. على سبيل المثال، لا يتتبع classes=[0, 2, 3] إلا الفئات المحددة. |
device | str | None | يحدد الجهاز المستخدم للاستدلال (مثل cpu أو cuda:0 أو 0). ويتيح للمستخدمين الاختيار بين CPU أو GPU محددة أو أجهزة حوسبة أخرى لتنفيذ النموذج. |
imgsz | int or tuple | 640 | حجم صورة الاستدلال المستهدف: عدد صحيح لصورة مربعة، أو زوج (height, width). تُعدّل الأبعاد وفقًا لخطوة النموذج. |
max_det | int | 300 | الحد الأقصى لعدد الكشوفات التي يُحتفظ بها لكل إطار قبل التتبع، ما يحد من عدد الكائنات التي يعالجها الحل في المشاهد الكثيفة. |
quantize | int or str | None | دقة الاستدلال: 16 (FP16) أو 32/unset (FP32) لنماذج PyTorch وTorchScript خارج CPU؛ أما التنسيقات الأخرى فتستخدم الدقة التي يحددها الملف الاصطناعي وبيئة التشغيل. يحل هذا الوسيط محل الوسيطة half المهملة. |
بالإضافة إلى ذلك، تُدعم إعدادات التصور التالية:
| المعامل | النوع | الافتراضي | الوصف |
|---|---|---|---|
show | bool | False | إذا كان True، يعرض الصور أو مقاطع الفيديو المشروحة في نافذة. يفيد ذلك في الحصول على ملاحظات مرئية فورية أثناء التطوير أو الاختبار. |
show_conf | bool | True | يعرض درجة الثقة لكل اكتشاف بجوار التسمية، ما يوضح مدى يقين النموذج بشأن كل اكتشاف. |
show_labels | bool | True | يعرض تسميات كل اكتشاف في المخرجات المرئية، ما يتيح فهم الكائنات المكتشفة فورًا. |
آلية عمل العدّ حسب المناطق#
يحوّل حلّ RegionCounter كل منطقة تمرّرها إلى مضلع يتكون من ثلاث نقاط على الأقل. وتصبح قائمة النقاط العادية نطاقًا واحدًا اسمه Region#01، بينما يحتفظ القاموس بأسماء النطاقات التي تحددها؛ ويُرسم كل نطاق بلون مستقل من لوحة ألوان Ultralytics. بعد ذلك، تُعالج كل صورة في ثلاث خطوات:
- يكتشف التتبّع في YOLO26 كل كائن في الإطار ويتتبعه
- لكل كائن متعقَّب، يحسب الحل مركز صندوق الإحاطة ويتحقق من المضلعات التي تحتويه
- يُعرض عدد كل منطقة عند مركزها، وتُصفّر الأعداد قبل الإطار التالي
نظرًا إلى أن الأعداد تبدأ من جديد في كل إطار، فإن الرقم الظاهر على المنطقة يمثل إشغالها الحالي، أي عدد الكائنات الموجودة فيها الآن، وليس مجموعًا تراكميًا. لعدّ الكائنات التي تعبر حدًا تراكميًا، استخدم حلّ ObjectCounter بدلًا من ذلك.
يعيد كل استدعاء أيضًا كائن نتائج، حيث تحتوي السمة plot_im على الإطار المشروح، ويعين القاموس region_counts أسماء المناطق إلى أعدادها، مما يتيح لك تمرير الأرقام إلى منطق تطبيقك.
الخلاصة#
يراقب حلّ RegionCounter في Ultralytics YOLO26 أعداد الكائنات المباشرة في أي عدد تحتاج إليه من النطاقات المسماة، باستخدام بضعة أسطر من التعليمات البرمجية. وللتوسع أكثر، عدّ عمليات عبور الحدود باستخدام عدّ الكائنات، أو تتبع الكائنات داخل نطاق واحد باستخدام TrackZone، أو استكشف حلول Ultralytics الأخرى.
الأسئلة الشائعة#
يعدّ Ultralytics YOLO26 الكائنات في المناطق من خلال تتبع كل كائن في الإطار والتحقق مما إذا كان مركز صندوق الإحاطة يقع داخل كل مضلع يحدده المستخدم. ويمثل العدد الظاهر على المنطقة إشغالها الحالي في ذلك الإطار، وليس مجموعًا تراكميًا. تُدرج خيارات التهيئة في قسم وسيطات
RegionCounter().مرّر الوسيطة
regionعلى شكل قاموس يربط أسماء النطاقات بقوائم النقاط، كما في المثال الرئيسي:from ultralytics import solutions region_points = { "region-01": [(50, 50), (250, 50), (250, 250), (50, 250)], "region-02": [(640, 640), (780, 640), (780, 720), (640, 720)], } regioncounter = solutions.RegionCounter(region=region_points, model="yolo26n.pt", show=True)يُرسم كل نطاق مسمى بلون مستقل ويُعرض له عدد منفصل.
يحمل الكائن الذي يعيده كل استدعاء لـ
regioncounter(im0)قاموسregion_countsالذي يربط أسماء المناطق بأعدادها، إلى جانبplot_im(الإطار المشروح) وtotal_tracks(عدد الكائنات المتعقبة). اطبع كائن النتائج لفحص جميع الحقول المتاحة.نعم. مرّر الوسيطة
classesمع فهارس الفئات المطلوب الاحتفاظ بها، مثلclasses=[0]لاحتساب الأشخاص فقط باستخدام نموذج مُدرَّب مسبقًا على COCO. توجد القائمة الكاملة للوسائط المتعلقة بالتتبّع في قسم وسائطRegionCounter().يعرض
RegionCounterعدد العناصر الموجودة داخل كل منطقة متعددة الأضلاع في الإطار الحالي، بينما يحتسب ObjectCounter العناصر تراكميًا عند عبورها خطًا أو حد منطقة، ويتتبع إجمالي الدخول والخروج. استخدمRegionCounterلمعرفة الإشغال الفوري لمنطقة واحدة أو أكثر، واستخدمObjectCounterلاحتساب مرات الدخول والخروج؛ ولا تدعم المناطق على شكل خطوط إلاObjectCounter.
التعليقات99
اطرح سؤالًا، وشارك نتائجك، أو ساعد شخصًا آخر.
pedmindset
Been trying to figure this out for a while now. I want to ask, can I use object counting in regions with instance sementation and tracking? You want to count vehicles that cross two specific regions, like so Object 001 crossed region A and region B. Is this possible to do
1
Glenn JocherUltralytics@pedmindset yes, it is possible to count objects crossing multiple regions using Ultralytics YOLO11. You can utilize the
RegionCounterclass to define multiple regions and track objects across them. The objects' movement through these regions can be tracked using persistent IDs, enabling you to determine which object crossed which regions. For implementation details, refer to the Region Counting Guide.1
amulyaamaggididivami
import cv2 from ultralytics import solutions # Load video file cap = cv2.VideoCapture("retail-store.mp4") assert cap.isOpened(), "Error reading video file" # Define the counting line coordinates (adjust as per your scene) region_points = [(1135, 714), (1134, 416)] # Get video properties w, h, fps = (int(cap.get(x)) for x in ( cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS, )) # Setup video writer for output video_writer = cv2.VideoWriter( "object_counting_output.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h), ) # Initialize the object counter counter = solutions.ObjectCounter( show=False, # Don't show live preview region=region_points, # Line coordinates model="yolo11n.pt", # YOLO model classes=[0], # Class 0 = person line_width=3, ) # Process video frame by frame while cap.isOpened(): success, frame = cap.read() if not success: print("Video processing complete.") break # Run the counter on the frame results = counter(frame) # Write to output video video_writer.write(results.plot_im) # Cleanup cap.release() video_writer.release() cv2.destroyAllWindows() print(f"✅ Processing complete. Saved as 'object_counting_output.mp4'")I’m getting the count reversed — the number of people entering is shown as OUT, and those exiting are shown as IN. How can I fix this?
RizwanMunawar
@amulyaamaggididivami Hi, can you please try reversing the region coordinates, like mentioned? I believe this will work.
region_points = [(1134, 416), (1135, 714)]amulyaamaggididivami
I have tried that, but it is not working
RizwanMunawar(تم التعديل)
@amulyaamaggididivami, well, in that case, you can clone the
ultralyticsrepository and build it locally with the changes in the code line here: https://github.com/ultralytics/ultralytics/blob/fa5d0bffbd63adfc302e9c118ffe5caa6447a044/ultralytics/solutions/object_counter.py#L135Edit: Can you please also try using the region points, rather than line points?
amulyaamaggididivami
Sure, thanks
1
Glenn JocherUltralytics@SudhanshuBlaze IN/OUT for line counters is image-axis based, not compass: for a mostly vertical line, IN = motion to the right (+x) and OUT = left; for a mostly horizontal line, IN = down (+y) and OUT = up; you can relabel to N/S/E/W post-process like this:
def label_dirs(results, p1, p2): (x1, y1), (x2, y2) = p1, p2 vertical = abs(x1 - x2) < abs(y1 - y2) return ({"eastbound": results.in_count, "westbound": results.out_count} if vertical else {"southbound": results.in_count, "northbound": results.out_count})Details of the IN/OUT logic are in the count_objects method of the ObjectCounter reference.
Forlin12
Hello, I would like to know how it works to remove how many objects there are per instant region because I can't extract this information.
SudhanshuBlaze
Hi,
I was wondering in the case of LINE counters, if there's a thumb rule to translate the IN, OUT count to North->South, East-> West, something like this. How do I know which direction is IN and which direction is OUT in the case of LINE counters?
My coordinates: line_counter1 = [(842, 1434), (640, 2200)]
line_counter2 = [(1028, 1304), (1094, 1038)]
line_counter3 = [(1068, 1038), (684, 1048)]
line_counter4 = [(2498, 546), (3276, 572)]
line_counter5 = [(3244, 786), (3414, 904)]
line_counter6 = [(1830, 958), (1668, 710)]Thanks in advance
morussk
Hey, I am trying to use solutions.RegionCounter() with conf less than 0.25 but it seems to be ignoring it, is that by design? If I use conf > 0.25 it is taken into account and counts only detections with confidence higher than conf in particular region but lower then 0.25 seems to be ignored and those detections have not chance of being counted by solutions.RegionCounter(). Please advice.
RizwanMunawar
@morussk Hi, thanks for sharing the details. The counting in region feature relies on object tracking rather than standalone predictions. As a result, detections with a confidence score below 0.25 are ignored by default.
However, this behaviour can be modified by adjusting the track_high_thresh parameter. Currently, the
Solutionsclass doesn’t support adjusting tracking configuration core options directly, but you can adjust it by cloning the code and modifying the tracker's configurations file.Note: We always recommend using the latest version of the Ultralytics Python package. You can update it by running:
pip install -U ultralytics.Thanks.
1
dimasfadhila
hello, what dataset do you use for object detection in this region?
Glenn JocherUltralytics@dimasfadhila for region-based object counting in the Ultralytics example, we use models pre-trained on the COCO dataset by default, which includes 80 common object classes. You can see the full class list in our COCO dataset documentation. If you need to count custom objects, you'll want to train on your own dataset using YOLO's training mode.
rejens
Process video
while cap.isOpened(): success, im0 = cap.read() # Read the video frame. if not success: break im0 = region.count(im0) # Count the objects in different regions of frame. cv2.imshow("frame", im0) if cv2.waitKey(1) & 0xFF == ord('q'): break
this successfully did count and display the objects inside the region, how can i retrieve the number of counts, seems region.count(im0) returns the image only.
thanks
Glenn JocherUltralytics(تم التعديل)@rejens
RegionCounterreturns a results object, so call it on each frame and read the per-region counts fromregion_counts:results = region(im0) print(results.region_counts) # e.g. {'Region#01': 3} im0 = results.plot_imThe counts are for the current frame (they reset on every frame). Let me know if you need further clarification!
Darkbutbright
hi,counter = solutions.ObjectCounter(paras),im0 = counter.count(im0),Can the type information, font size, and position of the count in the video be set?
Glenn JocherUltralytics(تم التعديل)@Darkbutbright yes, you can customize the text information, font size and position of the count by modifying the solution source code. For
ObjectCounter, the counts are drawn in itsdisplay_counts()method (inultralytics/solutions/object_counter.py) through theSolutionAnnotator, so you can adjust the text rendering there. You can also call the solution and draw your own text on the returnedplot_imwithcv2.putText(), usingresults.in_countandresults.out_count. Let me know if you need further clarification!Darkbutbright
Thank you for your patient answer, but I really don't know where to get the detailed instructions (documents) of the solutions class. Could you please tell me how to get it (including more clear instructions for using other classes of yolo)?
Glenn JocherUltralytics@Darkbutbright thank you for your question! You can find detailed instructions and documentation for the
solutionsclass and other YOLO functionalities in the Ultralytics Docs. For specific examples, such as region counting or object tracking, check the solutions guide or the respective sections in the repository. Let me know if you need assistance with a particular use case!
kemalfrk
Hello, select the area with mouse events how to write the code that counts these classes if it enters a class.Thanks
Glenn JocherUltralytics@kemalfrk to implement object counting in specific regions using mouse events, you can modify the region points dynamically by capturing mouse click events and updating the region coordinates in real-time. You can refer to the Ultralytics YOLOv8 region counting example for guidance on how to set up and use the
RegionCounter. If you have further questions or need additional customization, feel free to ask!
MuhammedEnesGurkan
Hello, I use object counter but ı want to count only sheeps how can ı set classes_names to sheeps. Please help me
Glenn JocherUltralytics@MuhammedEnesGurkan to count only sheep, use the
--classesargument to specify the class index for sheep in your model. For example, if sheep are class 2, run:python yolov8_region_counter.py --source "path/to/video.mp4" --classes 2. Adjust the class index as needed based on your model's class mapping.

