Threaded inference docs improvements (#16313)
Signed-off-by: UltralyticsAssistant <web@ultralytics.com> Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
parent
617d58d430
commit
7b19e0daa0
5 changed files with 70 additions and 95 deletions
|
|
@ -61,7 +61,7 @@ Ultralytics YOLO models return either a Python list of `Results` objects, or a m
|
|||
model = YOLO("yolov8n.pt") # pretrained YOLOv8n model
|
||||
|
||||
# Run batched inference on a list of images
|
||||
results = model(["im1.jpg", "im2.jpg"]) # return a list of Results objects
|
||||
results = model(["image1.jpg", "image2.jpg"]) # return a list of Results objects
|
||||
|
||||
# Process results list
|
||||
for result in results:
|
||||
|
|
@ -83,7 +83,7 @@ Ultralytics YOLO models return either a Python list of `Results` objects, or a m
|
|||
model = YOLO("yolov8n.pt") # pretrained YOLOv8n model
|
||||
|
||||
# Run batched inference on a list of images
|
||||
results = model(["im1.jpg", "im2.jpg"], stream=True) # return a generator of Results objects
|
||||
results = model(["image1.jpg", "image2.jpg"], stream=True) # return a generator of Results objects
|
||||
|
||||
# Process results generator
|
||||
for result in results:
|
||||
|
|
@ -109,8 +109,8 @@ YOLOv8 can process different types of input sources for inference, as shown in t
|
|||
| image | `'image.jpg'` | `str` or `Path` | Single image file. |
|
||||
| URL | `'https://ultralytics.com/images/bus.jpg'` | `str` | URL to an image. |
|
||||
| screenshot | `'screen'` | `str` | Capture a screenshot. |
|
||||
| PIL | `Image.open('im.jpg')` | `PIL.Image` | HWC format with RGB channels. |
|
||||
| OpenCV | `cv2.imread('im.jpg')` | `np.ndarray` | HWC format with BGR channels `uint8 (0-255)`. |
|
||||
| PIL | `Image.open('image.jpg')` | `PIL.Image` | HWC format with RGB channels. |
|
||||
| OpenCV | `cv2.imread('image.jpg')` | `np.ndarray` | HWC format with BGR channels `uint8 (0-255)`. |
|
||||
| numpy | `np.zeros((640,1280,3))` | `np.ndarray` | HWC format with BGR channels `uint8 (0-255)`. |
|
||||
| torch | `torch.zeros(16,3,320,640)` | `torch.Tensor` | BCHW format with RGB channels `float32 (0.0-1.0)`. |
|
||||
| CSV | `'sources.csv'` | `str` or `Path` | CSV file containing paths to images, videos, or directories. |
|
||||
|
|
@ -710,16 +710,16 @@ When using YOLO models in a multi-threaded application, it's important to instan
|
|||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def thread_safe_predict(image_path):
|
||||
def thread_safe_predict(model, image_path):
|
||||
"""Performs thread-safe prediction on an image using a locally instantiated YOLO model."""
|
||||
local_model = YOLO("yolov8n.pt")
|
||||
results = local_model.predict(image_path)
|
||||
model = YOLO(model)
|
||||
results = model.predict(image_path)
|
||||
# Process results
|
||||
|
||||
|
||||
# Starting threads that each have their own model instance
|
||||
Thread(target=thread_safe_predict, args=("image1.jpg",)).start()
|
||||
Thread(target=thread_safe_predict, args=("image2.jpg",)).start()
|
||||
Thread(target=thread_safe_predict, args=("yolov8n.pt", "image1.jpg")).start()
|
||||
Thread(target=thread_safe_predict, args=("yolov8n.pt", "image2.jpg")).start()
|
||||
```
|
||||
|
||||
For an in-depth look at thread-safe inference with YOLO models and step-by-step instructions, please refer to our [YOLO Thread-Safe Inference Guide](../guides/yolo-thread-safe-inference.md). This guide will provide you with all the necessary information to avoid common pitfalls and ensure that your multi-threaded inference runs smoothly.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue