-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Custom Tools & Overlay Heatmap (#100)
* added overlay heatmap * added overlays to utilities * added register tool * removed old examples * fix readme for mask app * fix issues for custom tools for example case * fix issues for custom tool imports * update readme * fix azure doc * fix type error
- Loading branch information
1 parent
503b3e0
commit 91206b0
Showing
16 changed files
with
340 additions
and
751 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,50 @@ | ||
import numpy as np | ||
from template_match import template_matching_with_rotation | ||
|
||
import vision_agent as va | ||
from vision_agent.image_utils import get_image_size, normalize_bbox | ||
from vision_agent.tools import Tool, register_tool | ||
|
||
|
||
@register_tool | ||
class TemplateMatch(Tool): | ||
name = "template_match_" | ||
description = "'template_match_' takes a template image and finds all locations where that template appears in the input image." | ||
usage = { | ||
"required_parameters": [ | ||
{"name": "target_image", "type": "str"}, | ||
{"name": "template_image", "type": "str"}, | ||
], | ||
"examples": [ | ||
{ | ||
"scenario": "Can you detect the location of the template in the target image? Image name: target.png Reference image: template.png", | ||
"parameters": { | ||
"target_image": "target.png", | ||
"template_image": "template.png", | ||
}, | ||
}, | ||
], | ||
} | ||
from vision_agent.utils.image_utils import get_image_size, normalize_bbox | ||
|
||
|
||
@va.tools.register_tool( | ||
imports=[ | ||
"import numpy as np", | ||
"from vision_agent.utils.image_utils import get_image_size, normalize_bbox", | ||
"from template_match import template_matching_with_rotation", | ||
] | ||
) | ||
def template_match(target_image: np.ndarray, template_image: np.ndarray) -> dict: | ||
"""'template_match' tool that finds the locations of the template image in the | ||
target image. | ||
Parameters: | ||
target_image (np.ndarray): The target image. | ||
template_image (np.ndarray): The template image. | ||
Returns: | ||
dict: A dictionary containing the bounding boxes of the matches. | ||
Example | ||
------- | ||
>>> import cv2 | ||
>>> target_image = cv2.imread("pid.png") | ||
>>> template_image = cv2.imread("pid_template.png") | ||
>>> matches = template_match(target_image, template_image) | ||
""" | ||
|
||
def __call__(self, target_image: str, template_image: str) -> dict: | ||
image_size = get_image_size(target_image) | ||
matches = template_matching_with_rotation(target_image, template_image) | ||
matches["bboxes"] = [ | ||
normalize_bbox(box, image_size) for box in matches["bboxes"] | ||
] | ||
return matches | ||
image_size = get_image_size(target_image) | ||
matches = template_matching_with_rotation(target_image, template_image) | ||
matches["bboxes"] = [normalize_bbox(box, image_size) for box in matches["bboxes"]] | ||
return matches | ||
|
||
|
||
if __name__ == "__main__": | ||
agent = va.agent.VisionAgent(verbose=True) | ||
resp, tools = agent.chat_with_workflow( | ||
agent = va.agent.VisionAgent(verbosity=2) | ||
result = agent.chat_with_workflow( | ||
[ | ||
{ | ||
"role": "user", | ||
"content": "Can you find the locations of the pid_template.png in pid.png and tell me if any are nearby 'NOTE 5'?", | ||
} | ||
], | ||
image="pid.png", | ||
reference_data={"image": "pid_template.png"}, | ||
visualize_output=True, | ||
media="pid.png", | ||
) |
Oops, something went wrong.