yolo-world-onnx


Nameyolo-world-onnx JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/Ziad-Algrafi/onnx-yolo
SummaryONNX-YOLO is a Python package for running inference on YOLO-WORLD open-vocabulary object detection models using ONNX runtime.
upload_time2024-05-19 05:31:33
maintainerNone
docs_urlNone
authorZiad-Algrafi
requires_python>=3.9
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">

# YOLO-World-ONNX

[![PyPI version](https://badge.fury.io/py/yolo-world-onnx.svg)](https://badge.fury.io/py/yolo-world-onnx)
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Ziad-Algrafi/yolo-world-onnx/blob/main/assets/YOLO_World_ONNX.ipynb)
[![Python Version](https://img.shields.io/badge/python-3.9%2B-blue)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

</div>

![Prompt is red car](https://raw.githubusercontent.com/Ziad-Algrafi/yolo-world-onnx/main/assets/Object%20Detection.png)

YOLO-World-ONNX is a Python package that enables running inference on YOLO-WORLD open-vocabulary object detection models using ONNX runtime. It provides a user-friendly interface for performing object detection on images or videos. The package leverages ONNX models to deliver fast inference time, making it suitable for a wide range of object detection applications.

## Installation

You can install YOLO-World-ONNX using pip:

```
pip install yolo-world-onnx
```

## Usage

### Inference

Here's an example of how to perform inference using YOLO-World-ONNX:

```python
import cv2 as cv
from yolo_world_onnx import YOLOWORLD

# Load the YOLO model
model_path = "path/to/your/model.onnx"

# Select a device 0 for GPU and for a CPU is cpu
model = YOLOWORLD(model_path, device="0")

# Set the class names
class_names = ["person", "car", "dog", "cat"]
model.set_classes(class_names)

# Retrieve the names
names = model.names

# Load an image
image = cv.imread("path/to/your/image.jpg")

# Perform object detection
boxes, scores, class_ids = model(image, conf=0.35, imgsz=640, iou=0.7)

# Process the results
for box, score, class_id in zip(boxes, scores, class_ids):
    x, y, w, h = box
    x1, y1 = int(x - w / 2), int(y - h / 2)
    x2, y2 = int(x + w / 2), int(y + h / 2)
    class_name = names[class_id]
    print(f"Detected {class_name} with confidence {score:.2f} at coordinates (x1={x1}, y1={y1}, x2={x2}, y2={y2})")
```

The `model` function performs object detection on the input image and returns three values:

1. `boxes`: A list of bounding box coordinates for each detected object. Each box is represented as a tuple of four values `(x, y, w, h)`, where:

   - `x` and `y` are the coordinates of the center of the bounding box.
   - `w` and `h` are the width and height of the bounding box.
   - The coordinates are in the original image size.

2. `scores`: A list of confidence scores for each detected object. The confidence score represents the model's confidence in the detection, ranging from 0 to 1.

3. `class_ids`: A list of class indices for each detected object. The class index corresponds to the index of the class name in the `names` list.

The `names` list contains the class names that were set using the `set_classes` method. It is used to map the class indices to their corresponding class names.

In the example code, the results are processed by iterating over the `boxes`, `scores`, and `class_ids` lists simultaneously using `zip`. For each detected object:

- The bounding box coordinates `(x, y, w, h)` are extracted from the `box` tuple.
- The top-left and bottom-right coordinates of the bounding box are calculated using `(x1, y1)` and `(x2, y2)`, respectively.
- The class name is obtained by indexing the `names` list with the `class_id`.
- The class name, confidence score, and bounding box coordinates are printed.

You can customize the processing of the results based on your specific requirements, such as drawing the bounding boxes on the image, filtering the detections based on confidence scores, or performing further analysis on the detected objects.

### Image Inference

Here's an example of performing inference on an image and drawing the results:

```python
import cv2 as cv
from yolo_world_onnx import YOLOWORLD

# Load the YOLO model
model_path = "path/to/your/model.onnx"

# Select a device 0 for GPU and for a CPU is cpu
model = YOLOWORLD(model_path, device="0")

# Set the class names
class_names = ["person", "car", "dog", "cat"]
model.set_classes(class_names)

# Retrieve the names
names = model.names

# Load an image
image = cv.imread("path/to/your/image.jpg")

# Perform object detection
boxes, scores, class_ids = model(image, conf=0.35, imgsz=640, iou=0.7)

# Draw bounding boxes on the image
for box, score, class_id in zip(boxes, scores, class_ids):
    x, y, w, h = box
    x1, y1 = int(x - w / 2), int(y - h / 2)
    x2, y2 = int(x + w / 2), int(y + h / 2)
    cv.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
    class_name = names[class_id]
    cv.putText(image, f"{class_name}: {score:.2f}", (x1, y1 - 10), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

# Display the image
cv.imshow("Object Detection", image)
cv.waitKey(0)
cv.destroyAllWindows()
```

### Video Inference

Here's an example of performing inference on a video and drawing the results:

```python
import cv2 as cv
from yolo_world_onnx import YOLOWORLD

# Load the YOLO model
model_path = "path/to/your/model.onnx"

# Select a device 0 for GPU and for a CPU is cpu
model = YOLOWORLD(model_path, device="0")

# Set the class names
class_names = ["person", "car", "dog", "cat"]
model.set_classes(class_names)

# Retrieve the names
names = model.names

# Open a video file or capture from a camera
video_path = "path/to/your/video.mp4"
cap = cv.VideoCapture(video_path)

while True:
    # Read a frame from the video
    ret, frame = cap.read()
    if not ret:
        break

    # Perform object detection
    boxes, scores, class_ids = model(frame, conf=0.35, imgsz=640, iou=0.7)

    # Draw bounding boxes on the frame
    for box, score, class_id in zip(boxes, scores, class_ids):
        x, y, w, h = box
        x1, y1 = int(x - w / 2), int(y - h / 2)
        x2, y2 = int(x + w / 2), int(y + h / 2)
        cv.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
        class_name = names[class_id]
        cv.putText(frame, f"{class_name}: {score:.2f}", (x1, y1 - 10), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

    # Display the frame
    cv.imshow("Object Detection", frame)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()
```

## ONNX Models

| Model Type      | mAP  | mAP50 | mAP75 | Image Size | Model                                                                                              |
| --------------- | ---- | ----- | ----- | ---------- | -------------------------------------------------------------------------------------------------- |
| yolov8s-worldv2 | 37.7 | 52.2  | 41.0  | 640        | [Download](https://github.com/Ziad-Algrafi/ODLabel/raw/main/assets/yolov8s-worldv2.onnx?download=) |
| yolov8m-worldv2 | 43.0 | 58.4  | 46.8  | 640        | [Download](https://github.com/Ziad-Algrafi/ODLabel/raw/main/assets/yolov8m-worldv2.onnx?download=) |
| yolov8l-worldv2 | 45.8 | 61.3  | 49.8  | 640        | [Download](https://github.com/Ziad-Algrafi/ODLabel/raw/main/assets/yolov8l-worldv2.onnx?download=) |
| yolov8x-worldv2 | 47.1 | 62.8  | 51.4  | 640        | [Download](https://github.com/Ziad-Algrafi/ODLabel/raw/main/assets/yolov8x-worldv2.onnx?download=) |

## Custom Models

YOLO-World-ONNX supports custom ONNX models that are exported in the same format as the models provided in this repository. The code is designed to work dynamically with models of any number of classes. Even if the model is exported on 100 classes and the user specifies only 3 classes to be detected in the run, YOLO-World-ONNX will detect those 3 classes accordingly.

If you want to use a custom model with a different resolution or detect more classes, you can follow the guide on exporting custom models in the [ONNX-YOLO-World-Open-Vocabulary-Object-Detection](https://github.com/ibaiGorordo/ONNX-YOLO-World-Open-Vocabulary-Object-Detection) repository.

## Credits

The original source code for this package is based on the work by [Ibai Gorordo](https://github.com/ibaiGorordo) in the [ONNX-YOLO-World-Open-Vocabulary-Object-Detection](https://github.com/ibaiGorordo/ONNX-YOLO-World-Open-Vocabulary-Object-Detection) repository.

Image reference is [Here](https://unsplash.com/photos/aerial-photography-of-cars-on-parking-lot-WPVtT0MEM00)

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Ziad-Algrafi/onnx-yolo",
    "name": "yolo-world-onnx",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Ziad-Algrafi",
    "author_email": "ZiadAlgrafi@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/87/3f/1fd84c0871d80577cdc215dc59c4246137fb1376a1166c9921798e6a1395/yolo_world_onnx-0.1.1.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n\n# YOLO-World-ONNX\n\n[![PyPI version](https://badge.fury.io/py/yolo-world-onnx.svg)](https://badge.fury.io/py/yolo-world-onnx)\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Ziad-Algrafi/yolo-world-onnx/blob/main/assets/YOLO_World_ONNX.ipynb)\n[![Python Version](https://img.shields.io/badge/python-3.9%2B-blue)](https://www.python.org/downloads/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n</div>\n\n![Prompt is red car](https://raw.githubusercontent.com/Ziad-Algrafi/yolo-world-onnx/main/assets/Object%20Detection.png)\n\nYOLO-World-ONNX is a Python package that enables running inference on YOLO-WORLD open-vocabulary object detection models using ONNX runtime. It provides a user-friendly interface for performing object detection on images or videos. The package leverages ONNX models to deliver fast inference time, making it suitable for a wide range of object detection applications.\n\n## Installation\n\nYou can install YOLO-World-ONNX using pip:\n\n```\npip install yolo-world-onnx\n```\n\n## Usage\n\n### Inference\n\nHere's an example of how to perform inference using YOLO-World-ONNX:\n\n```python\nimport cv2 as cv\nfrom yolo_world_onnx import YOLOWORLD\n\n# Load the YOLO model\nmodel_path = \"path/to/your/model.onnx\"\n\n# Select a device 0 for GPU and for a CPU is cpu\nmodel = YOLOWORLD(model_path, device=\"0\")\n\n# Set the class names\nclass_names = [\"person\", \"car\", \"dog\", \"cat\"]\nmodel.set_classes(class_names)\n\n# Retrieve the names\nnames = model.names\n\n# Load an image\nimage = cv.imread(\"path/to/your/image.jpg\")\n\n# Perform object detection\nboxes, scores, class_ids = model(image, conf=0.35, imgsz=640, iou=0.7)\n\n# Process the results\nfor box, score, class_id in zip(boxes, scores, class_ids):\n    x, y, w, h = box\n    x1, y1 = int(x - w / 2), int(y - h / 2)\n    x2, y2 = int(x + w / 2), int(y + h / 2)\n    class_name = names[class_id]\n    print(f\"Detected {class_name} with confidence {score:.2f} at coordinates (x1={x1}, y1={y1}, x2={x2}, y2={y2})\")\n```\n\nThe `model` function performs object detection on the input image and returns three values:\n\n1. `boxes`: A list of bounding box coordinates for each detected object. Each box is represented as a tuple of four values `(x, y, w, h)`, where:\n\n   - `x` and `y` are the coordinates of the center of the bounding box.\n   - `w` and `h` are the width and height of the bounding box.\n   - The coordinates are in the original image size.\n\n2. `scores`: A list of confidence scores for each detected object. The confidence score represents the model's confidence in the detection, ranging from 0 to 1.\n\n3. `class_ids`: A list of class indices for each detected object. The class index corresponds to the index of the class name in the `names` list.\n\nThe `names` list contains the class names that were set using the `set_classes` method. It is used to map the class indices to their corresponding class names.\n\nIn the example code, the results are processed by iterating over the `boxes`, `scores`, and `class_ids` lists simultaneously using `zip`. For each detected object:\n\n- The bounding box coordinates `(x, y, w, h)` are extracted from the `box` tuple.\n- The top-left and bottom-right coordinates of the bounding box are calculated using `(x1, y1)` and `(x2, y2)`, respectively.\n- The class name is obtained by indexing the `names` list with the `class_id`.\n- The class name, confidence score, and bounding box coordinates are printed.\n\nYou can customize the processing of the results based on your specific requirements, such as drawing the bounding boxes on the image, filtering the detections based on confidence scores, or performing further analysis on the detected objects.\n\n### Image Inference\n\nHere's an example of performing inference on an image and drawing the results:\n\n```python\nimport cv2 as cv\nfrom yolo_world_onnx import YOLOWORLD\n\n# Load the YOLO model\nmodel_path = \"path/to/your/model.onnx\"\n\n# Select a device 0 for GPU and for a CPU is cpu\nmodel = YOLOWORLD(model_path, device=\"0\")\n\n# Set the class names\nclass_names = [\"person\", \"car\", \"dog\", \"cat\"]\nmodel.set_classes(class_names)\n\n# Retrieve the names\nnames = model.names\n\n# Load an image\nimage = cv.imread(\"path/to/your/image.jpg\")\n\n# Perform object detection\nboxes, scores, class_ids = model(image, conf=0.35, imgsz=640, iou=0.7)\n\n# Draw bounding boxes on the image\nfor box, score, class_id in zip(boxes, scores, class_ids):\n    x, y, w, h = box\n    x1, y1 = int(x - w / 2), int(y - h / 2)\n    x2, y2 = int(x + w / 2), int(y + h / 2)\n    cv.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)\n    class_name = names[class_id]\n    cv.putText(image, f\"{class_name}: {score:.2f}\", (x1, y1 - 10), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)\n\n# Display the image\ncv.imshow(\"Object Detection\", image)\ncv.waitKey(0)\ncv.destroyAllWindows()\n```\n\n### Video Inference\n\nHere's an example of performing inference on a video and drawing the results:\n\n```python\nimport cv2 as cv\nfrom yolo_world_onnx import YOLOWORLD\n\n# Load the YOLO model\nmodel_path = \"path/to/your/model.onnx\"\n\n# Select a device 0 for GPU and for a CPU is cpu\nmodel = YOLOWORLD(model_path, device=\"0\")\n\n# Set the class names\nclass_names = [\"person\", \"car\", \"dog\", \"cat\"]\nmodel.set_classes(class_names)\n\n# Retrieve the names\nnames = model.names\n\n# Open a video file or capture from a camera\nvideo_path = \"path/to/your/video.mp4\"\ncap = cv.VideoCapture(video_path)\n\nwhile True:\n    # Read a frame from the video\n    ret, frame = cap.read()\n    if not ret:\n        break\n\n    # Perform object detection\n    boxes, scores, class_ids = model(frame, conf=0.35, imgsz=640, iou=0.7)\n\n    # Draw bounding boxes on the frame\n    for box, score, class_id in zip(boxes, scores, class_ids):\n        x, y, w, h = box\n        x1, y1 = int(x - w / 2), int(y - h / 2)\n        x2, y2 = int(x + w / 2), int(y + h / 2)\n        cv.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)\n        class_name = names[class_id]\n        cv.putText(frame, f\"{class_name}: {score:.2f}\", (x1, y1 - 10), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)\n\n    # Display the frame\n    cv.imshow(\"Object Detection\", frame)\n    if cv.waitKey(1) & 0xFF == ord('q'):\n        break\n\ncap.release()\ncv.destroyAllWindows()\n```\n\n## ONNX Models\n\n| Model Type      | mAP  | mAP50 | mAP75 | Image Size | Model                                                                                              |\n| --------------- | ---- | ----- | ----- | ---------- | -------------------------------------------------------------------------------------------------- |\n| yolov8s-worldv2 | 37.7 | 52.2  | 41.0  | 640        | [Download](https://github.com/Ziad-Algrafi/ODLabel/raw/main/assets/yolov8s-worldv2.onnx?download=) |\n| yolov8m-worldv2 | 43.0 | 58.4  | 46.8  | 640        | [Download](https://github.com/Ziad-Algrafi/ODLabel/raw/main/assets/yolov8m-worldv2.onnx?download=) |\n| yolov8l-worldv2 | 45.8 | 61.3  | 49.8  | 640        | [Download](https://github.com/Ziad-Algrafi/ODLabel/raw/main/assets/yolov8l-worldv2.onnx?download=) |\n| yolov8x-worldv2 | 47.1 | 62.8  | 51.4  | 640        | [Download](https://github.com/Ziad-Algrafi/ODLabel/raw/main/assets/yolov8x-worldv2.onnx?download=) |\n\n## Custom Models\n\nYOLO-World-ONNX supports custom ONNX models that are exported in the same format as the models provided in this repository. The code is designed to work dynamically with models of any number of classes. Even if the model is exported on 100 classes and the user specifies only 3 classes to be detected in the run, YOLO-World-ONNX will detect those 3 classes accordingly.\n\nIf you want to use a custom model with a different resolution or detect more classes, you can follow the guide on exporting custom models in the [ONNX-YOLO-World-Open-Vocabulary-Object-Detection](https://github.com/ibaiGorordo/ONNX-YOLO-World-Open-Vocabulary-Object-Detection) repository.\n\n## Credits\n\nThe original source code for this package is based on the work by [Ibai Gorordo](https://github.com/ibaiGorordo) in the [ONNX-YOLO-World-Open-Vocabulary-Object-Detection](https://github.com/ibaiGorordo/ONNX-YOLO-World-Open-Vocabulary-Object-Detection) repository.\n\nImage reference is [Here](https://unsplash.com/photos/aerial-photography-of-cars-on-parking-lot-WPVtT0MEM00)\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "ONNX-YOLO is a Python package for running inference on YOLO-WORLD open-vocabulary object detection models using ONNX runtime.",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/Ziad-Algrafi/onnx-yolo"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "09b10302e0a5e1d25d41761c52d28b57298e256f1a6c386652f3efad402d2fd9",
                "md5": "59e88a92d4eb71999199b5c8b81d70e1",
                "sha256": "f0937880be96017d7f807587a92b6b63f911ea45e4572a9995233ab3c4c610fd"
            },
            "downloads": -1,
            "filename": "yolo_world_onnx-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "59e88a92d4eb71999199b5c8b81d70e1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 6294,
            "upload_time": "2024-05-19T05:31:30",
            "upload_time_iso_8601": "2024-05-19T05:31:30.302380Z",
            "url": "https://files.pythonhosted.org/packages/09/b1/0302e0a5e1d25d41761c52d28b57298e256f1a6c386652f3efad402d2fd9/yolo_world_onnx-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "873f1fd84c0871d80577cdc215dc59c4246137fb1376a1166c9921798e6a1395",
                "md5": "cccfa7bb648fb6d1c26dd2dfc5d57d8a",
                "sha256": "7fe4eca696242c620490ce65661251946b42a0fc00299cf830dbafd96dd671e7"
            },
            "downloads": -1,
            "filename": "yolo_world_onnx-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "cccfa7bb648fb6d1c26dd2dfc5d57d8a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 6431,
            "upload_time": "2024-05-19T05:31:33",
            "upload_time_iso_8601": "2024-05-19T05:31:33.065475Z",
            "url": "https://files.pythonhosted.org/packages/87/3f/1fd84c0871d80577cdc215dc59c4246137fb1376a1166c9921798e6a1395/yolo_world_onnx-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-19 05:31:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Ziad-Algrafi",
    "github_project": "onnx-yolo",
    "github_not_found": true,
    "lcname": "yolo-world-onnx"
}
        
Elapsed time: 0.37307s