Skip to content

LOCA (Low-shot Object Counting network with iterative prototype Adaptation).#

This example demonstrates how to use the NShot LOCA tool for object counting in images.

from vision_agent_tools.models.nshot_counting import NShotCounting

# (replace this path with your own!)
test_image = "path/to/your/image.jpg"

# Load the image
image = Image.open(test_image)
# Initialize the counting model and choose the image output size you expect.
ObjectCounting = NShotCounting(zero_shot=False, img_size=512)

# Run the inference
results = ObjectCounting(image, bbox=[12, 34, 56, 78])

# Let's find out how many objects were found in total
print("Found a total count of {results.count} objects on the image!")

CountingDetection #

Bases: BaseModel

Represents an inference result from the LOCA model.

Attributes:

Name Type Description
count int

The predicted number of detected objects.

masks list[Any]

A list of numpy arrays representing the masks of the detected objects in the image.

NShotCounting #

Bases: BaseMLModel

Model for object counting using the zeroshot and n-shot versions of the LOCA model from the paper A Low-Shot Object Counting Network With Iterative Prototype Adaptation .

__call__(image, bbox=None) #

LOCA injects shape and appearance information into object queries to precisely count objects of various sizes in densely and sparsely populated scenarios. It also extends to a zeroshot scenario and achieves excellent localization and count errors across the entire low-shot spectrum.

Parameters:

Name Type Description Default
image Image

The input image for object detection.

required
bbox BoundingBox

A list of four ints representing the bounding box coordinates (xmin, ymin, xmax, ymax) of the detected query in the image.

None

Returns:

Name Type Description
CountingDetection CountingDetection

An object type containing: - The count of the objects found similar to the bbox query. - A list of numpy arrays representing the masks of the objects found.

__init__(zero_shot=True, img_size=512) #

Initializes the LOCA model.

Parameters:

Name Type Description Default
img_size int

Size of the input image.

512