Skip to content

Commit

Permalink
Add support for object-detection models in gr.load() (#7716)
Browse files Browse the repository at this point in the history
* add support for object-detection models in gr.load

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
abidlabs and gradio-pr-bot committed Mar 15, 2024
1 parent 28342a2 commit 188b86b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-ends-feel.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Add support for object-detection models in `gr.load()`
5 changes: 5 additions & 0 deletions gradio/external.py
Expand Up @@ -343,6 +343,11 @@ def custom_post_binary(data):
label="Predictions", type="array", headers=["prediction"]
)
fn = external_utils.tabular_wrapper
# example model: microsoft/table-transformer-detection
elif p == "object-detection":
inputs = components.Image(type="filepath", label="Input Image")
outputs = components.AnnotatedImage(label="Annotations")
fn = external_utils.object_detection_wrapper(client)
else:
raise ValueError(f"Unsupported pipeline type: {p}")

Expand Down
20 changes: 20 additions & 0 deletions gradio/external_utils.py
Expand Up @@ -169,6 +169,26 @@ def token_classification_inner(input: str):
return token_classification_inner


def object_detection_wrapper(client: InferenceClient):
def object_detection_inner(input: str):
annotations = client.object_detection(input)
formatted_annotations = [
(
(
a["box"]["xmin"],
a["box"]["ymin"],
a["box"]["xmax"],
a["box"]["ymax"],
),
a["label"],
)
for a in annotations
]
return (input, formatted_annotations)

return object_detection_inner


def chatbot_preprocess(text, state):
if not state:
return text, [], []
Expand Down

0 comments on commit 188b86b

Please sign in to comment.