|
| 1 | +# Bounding Box Detectors |
| 2 | + |
| 3 | +Bounding box detectors are used to detect and localize objects in an image by returning bounding boxes around each detected object. |
| 4 | + |
| 5 | +```python notest |
| 6 | +from groundlight import ExperimentalApi |
| 7 | +gl = ExperimentalApi() |
| 8 | + |
| 9 | +# highlight-start |
| 10 | +detector = gl.create_bounding_box_detector( |
| 11 | + name="dog-detector", |
| 12 | + query="Draw a bounding box around each dog in the image", |
| 13 | + class_name="dog", |
| 14 | + max_num_bboxes=25, |
| 15 | + confidence_threshold=0.6, |
| 16 | +) |
| 17 | +# highlight-end |
| 18 | +``` |
| 19 | + |
| 20 | +Bounding box detectors should be provided with a query that asks the model to identify and localize objects in an image, such as "Draw a bounding box around each dog in the image". |
| 21 | + |
| 22 | +The `class_name` parameter specifies the type of object to detect, and this label will be assigned to each returned bounding box. |
| 23 | + |
| 24 | +The `max_num_bboxes` parameter sets the maximum number of bounding boxes that the detector will return (default: 10). If there are more objects in the image than the maximum, the result label will be `GREATER_THAN_MAX`. |
| 25 | + |
| 26 | +The `confidence_threshold` parameter sets the minimum confidence level required for the ML model's predictions. If the model's confidence falls below this threshold, the query will be sent for human review. |
| 27 | + |
| 28 | +:::note |
| 29 | +Bounding Box Detectors are currently in beta and available through the `ExperimentalApi`. They are available on [Business and Enterprise plans](https://www.groundlight.ai/pricing). |
| 30 | +::: |
| 31 | + |
| 32 | +## Submit an Image Query to a Bounding Box Detector |
| 33 | + |
| 34 | +Now that you have created a bounding box detector, you can submit an image query to it. |
| 35 | + |
| 36 | +```python notest |
| 37 | +from groundlight import ExperimentalApi |
| 38 | +gl = ExperimentalApi() |
| 39 | + |
| 40 | +detector = gl.get_detector_by_name("dog-detector") |
| 41 | + |
| 42 | +# highlight-start |
| 43 | +# Detect dogs in an image |
| 44 | +image_query = gl.submit_image_query(detector, "path/to/image.jpg") |
| 45 | +# highlight-end |
| 46 | + |
| 47 | +print(f"Label: {image_query.result.label}") |
| 48 | +print(f"Confidence: {image_query.result.confidence}") |
| 49 | +print(f"Bounding Boxes: {image_query.rois}") |
| 50 | +``` |
| 51 | + |
| 52 | +For bounding box detectors, the `label` attribute of the result object will be one of: |
| 53 | +- `NO_OBJECTS`: No objects of the specified class were detected in the image |
| 54 | +- `BOUNDING_BOX`: Objects were detected and bounding boxes are available in `image_query.rois` |
| 55 | +- `GREATER_THAN_MAX`: More objects were detected than the `max_num_bboxes` limit |
| 56 | +- `UNCLEAR`: The result was unclear |
| 57 | + |
| 58 | +The `rois` (regions of interest) attribute contains the list of bounding boxes, each with: |
| 59 | +- `geometry`: Bounding box coordinates (`left`, `top`, `right`, `bottom`) as values between 0 and 1 |
| 60 | +- `label`: The class name of the detected object |
| 61 | +- `score`: Confidence score for this specific object |
| 62 | + |
| 63 | +<!-- TODO: display an example image with bounding boxes --> |
| 64 | + |
| 65 | +:::tip Drawing Bounding Boxes |
| 66 | +You can visualize the bounding boxes returned by bounding box detectors using a library like OpenCV. Here's an example of how to draw bounding boxes on an image: |
| 67 | + |
| 68 | +```python notest |
| 69 | +import cv2 |
| 70 | +import numpy as np |
| 71 | + |
| 72 | +def draw_bounding_boxes(image_path, rois): |
| 73 | + """ |
| 74 | + Draw bounding boxes on an image based on ROIs returned from a bounding box detector. |
| 75 | +
|
| 76 | + Args: |
| 77 | + image_path: Path to the image file |
| 78 | + rois: List of ROI objects returned from image_query.rois |
| 79 | + """ |
| 80 | + image = cv2.imread(image_path) |
| 81 | + if image is None: |
| 82 | + raise ValueError(f"Could not read image from {image_path}") |
| 83 | + height, width = image.shape[:2] |
| 84 | + |
| 85 | + # Draw bounding boxes |
| 86 | + for roi in rois: |
| 87 | + x1 = int(roi.geometry.left * width) |
| 88 | + y1 = int(roi.geometry.top * height) |
| 89 | + x2 = int(roi.geometry.right * width) |
| 90 | + y2 = int(roi.geometry.bottom * height) |
| 91 | + cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) |
| 92 | + label_text = f"{roi.label}: {roi.score:.2f}" |
| 93 | + cv2.putText(image, label_text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) |
| 94 | + |
| 95 | + # Display the image |
| 96 | + cv2.imshow("Image with Bounding Boxes", image) |
| 97 | + cv2.waitKey(0) |
| 98 | + cv2.destroyAllWindows() |
| 99 | + |
| 100 | +# Example usage: |
| 101 | +# image_query = gl.submit_image_query(detector, "path/to/image.jpg") |
| 102 | +# draw_bounding_boxes("path/to/image.jpg", image_query.rois) |
| 103 | +``` |
| 104 | +::: |
| 105 | + |
| 106 | +## Add a Label to a Bounding Box Detector |
| 107 | + |
| 108 | +The Groundlight API allows you to add labels to image queries, including Region of Interest (ROI) data. |
| 109 | +When adding a label to a bounding box detector, you must include the ROIs that correspond to the objects in the image. |
| 110 | + |
| 111 | +```python notest |
| 112 | +from groundlight import ExperimentalApi |
| 113 | +gl = ExperimentalApi() |
| 114 | + |
| 115 | +# highlight-start |
| 116 | +# Add a bounding box label with corresponding ROIs to the image query from the previous example. |
| 117 | +# ROIs are specified as (left, top) and (right, bottom) coordinates, with values |
| 118 | +# between 0 and 1 representing the percentage of the image width and height. |
| 119 | +roi1 = gl.create_roi("dog", (0.1, 0.2), (0.3, 0.4)) |
| 120 | +roi2 = gl.create_roi("dog", (0.5, 0.3), (0.7, 0.6)) |
| 121 | +rois = [roi1, roi2] |
| 122 | +gl.add_label(image_query, label="BOUNDING_BOX", rois=rois) |
| 123 | +# highlight-end |
| 124 | +``` |
| 125 | + |
| 126 | +Valid label values for bounding box detectors are: |
| 127 | +- `"NO_OBJECTS"`: Use when there are no objects of the target class in the image (no ROIs needed) |
| 128 | +- `"BOUNDING_BOX"`: Use when objects are present and you are providing ROIs |
| 129 | +- `"GREATER_THAN_MAX"`: Use when there are more objects than `max_num_bboxes` |
| 130 | +- `"UNCLEAR"`: Use when the image is unclear or the answer cannot be determined |
0 commit comments