|
| 1 | +# Count Detectors |
| 2 | + |
| 3 | +Counting detectors are used to count the number of objects in an image. Groundlight's counting detectors also return bounding boxes around the objects they count. |
| 4 | + |
| 5 | +```python notest |
| 6 | +from groundlight import ExperimentalApi |
| 7 | +gl_exp = ExperimentalApi() |
| 8 | + |
| 9 | +# highlight-start |
| 10 | +detector = gl_exp.create_counting_detector( |
| 11 | + name="car-counter", |
| 12 | + query="How many cars are in the parking lot?", |
| 13 | + class_name="car", |
| 14 | + max_count=20, |
| 15 | + confidence_threshold=0.2, |
| 16 | +) |
| 17 | +# highlight-end |
| 18 | +``` |
| 19 | + |
| 20 | +Counting detectors should be provided with a query that asks "how many" objects are in the image. |
| 21 | + |
| 22 | +A maximum count (of 25 or fewer) must be specified when creating a counting detector. This is the maximum number of objects that the detector will count in an image. Groundlight's ML models are optimized for counting up to 20 objects, but you can increase the maximum count to 25 if needed. If you have an application that requires counting more than 25 objects, please [contact us ](mailto:[email protected]). |
| 23 | + |
| 24 | +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. Count detectors can have a `confidence_threshold` set to any value between `1/(max_count + 2)` and 1. |
| 25 | + |
| 26 | +:::note |
| 27 | +Counting Detectors are available on [Business and Enterprise plans](https://www.groundlight.ai/pricing). |
| 28 | +::: |
| 29 | + |
| 30 | +## Submit an Image Query to a Counting Detector |
| 31 | + |
| 32 | +Now that you have created a counting detector, you can submit an image query to it. |
| 33 | + |
| 34 | +```python notest |
| 35 | +from groundlight import ExperimentalApi |
| 36 | +gl_exp = ExperimentalApi() |
| 37 | + |
| 38 | +detector = gl_exp.get_detector_by_name("car-counter") |
| 39 | + |
| 40 | +# highlight-start |
| 41 | +# Count the number of cars in an image |
| 42 | +image_query = gl_exp.submit_image_query(detector, "path/to/image.jpg") |
| 43 | +# highlight-end |
| 44 | + |
| 45 | +print(f"Counted {image_query.result.count} cars") |
| 46 | +print(f"Confidence: {image_query.result.confidence}") |
| 47 | +print(f"Bounding Boxes: {image_query.rois}") |
| 48 | +``` |
| 49 | + |
| 50 | +In the case of counting detectors, the `count` attribute of the result object will contain the number of objects counted in the image. The `confidence` attribute represents the confidence level in the specific count. Note that this implies that confidences may be lower (on average) for counting detectors with a higher maximum count. |
| 51 | + |
| 52 | +<!-- TODO: display an example image with bounding boxes --> |
| 53 | + |
| 54 | +:::tip Drawing Bounding Boxes |
| 55 | +You can visualize the bounding boxes returned by counting detectors using a library like OpenCV. Here's an example of how to draw bounding boxes on an image: |
| 56 | + |
| 57 | +```python notest |
| 58 | +import cv2 |
| 59 | +import numpy as np |
| 60 | + |
| 61 | +def draw_bounding_boxes(image_path, rois): |
| 62 | + """ |
| 63 | + Draw bounding boxes on an image based on ROIs returned from a counting detector. |
| 64 | +
|
| 65 | + Args: |
| 66 | + image_path: Path to the image file |
| 67 | + rois: List of ROI objects returned from image_query.rois |
| 68 | + """ |
| 69 | + image = cv2.imread(image_path) |
| 70 | + if image is None: |
| 71 | + raise ValueError(f"Could not read image from {image_path}") |
| 72 | + height, width = image.shape[:2] |
| 73 | + |
| 74 | + # Draw bounding boxes |
| 75 | + for roi in rois: |
| 76 | + x1 = int(roi.geometry.left * width) |
| 77 | + y1 = int(roi.geometry.top * height) |
| 78 | + x2 = int(roi.geometry.right * width) |
| 79 | + y2 = int(roi.geometry.bottom * height) |
| 80 | + cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) |
| 81 | + label_text = f"{roi.label}: {roi.score:.2f}" |
| 82 | + cv2.putText(image, label_text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) |
| 83 | + |
| 84 | + # Display the image |
| 85 | + cv2.imshow("Image with Bounding Boxes", image) |
| 86 | + cv2.waitKey(0) |
| 87 | + cv2.destroyAllWindows() |
| 88 | + |
| 89 | +# Example usage: |
| 90 | +# image_query = gl.submit_image_query(detector, "path/to/image.jpg") |
| 91 | +# draw_bounding_boxes("path/to/image.jpg", image_query.rois) |
| 92 | +``` |
| 93 | +::: |
| 94 | + |
| 95 | +## Add a label to a Counting Detector |
| 96 | + |
| 97 | +The Groundlight API allows you to add labels to image queries, including Region of Interest (ROI) data. |
| 98 | +When adding a label to a counting detector, if you include ROIs, the number of ROIs should match |
| 99 | +the count you are labeling. |
| 100 | + |
| 101 | +```python notest |
| 102 | +from groundlight import ExperimentalApi |
| 103 | +gl_exp = ExperimentalApi() |
| 104 | + |
| 105 | +# highlight-start |
| 106 | +# Add a count label with corresponding ROIs to the image query from the previous example. |
| 107 | +# ROIs are specified as (left, top) and (right, bottom) coordinates, with values |
| 108 | +# between 0 and 1 representing the percentage of the image width and height. |
| 109 | +roi1 = gl_exp.create_roi("car", (0.1, 0.2), (0.2, 0.3)) |
| 110 | +roi2 = gl_exp.create_roi("car", (0.4, 0.4), (0.5, 0.6)) |
| 111 | +roi3 = gl_exp.create_roi("car", (0.6, 0.5), (0.8, 0.9)) |
| 112 | +rois = [roi1, roi2, roi3] |
| 113 | +gl_exp.add_label(image_query, label=len(rois), rois=rois) |
| 114 | +# highlight-end |
| 115 | +``` |
0 commit comments