Skip to content

Commit

Permalink
Issue 2085: Transformers object detection pipeline added (#6423)
Browse files Browse the repository at this point in the history
* Object detection pipeline added

* Removed app.py

* add changeset

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 16, 2023
1 parent ac4ca59 commit 62d35c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rare-rocks-yell.md
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Issue 2085: Transformers object detection pipeline added
37 changes: 36 additions & 1 deletion gradio/pipelines.py
Expand Up @@ -202,6 +202,33 @@ def load_from_pipeline(pipeline: pipelines.base.Pipeline) -> dict:
"preprocess": lambda i: {"images": i},
"postprocess": lambda r: r[0]["generated_text"],
}
elif hasattr(transformers, "ObjectDetectionPipeline") and isinstance(
pipeline, pipelines.object_detection.ObjectDetectionPipeline
):
pipeline_info = {
"inputs": components.Image(
type="filepath", label="Input Image", render=False
),
"outputs": components.AnnotatedImage(
label="Objects Detected", render=False
),
"preprocess": lambda i: {"inputs": i},
"postprocess": lambda r, img: (
img,
[
(
(
i["box"]["xmin"],
i["box"]["ymin"],
i["box"]["xmax"],
i["box"]["ymax"],
),
i["label"],
)
for i in r
],
),
}
else:
raise ValueError(f"Unsupported pipeline type: {type(pipeline)}")

Expand All @@ -220,7 +247,15 @@ def fn(*params):
data = pipeline(*data)
else:
data = pipeline(**data)
output = pipeline_info["postprocess"](data)
# special case for object-detection
# original input image sent to postprocess function
if isinstance(
pipeline,
pipelines.object_detection.ObjectDetectionPipeline,
):
output = pipeline_info["postprocess"](data, params[0])
else:
output = pipeline_info["postprocess"](data)
return output

interface_info = pipeline_info.copy()
Expand Down

0 comments on commit 62d35c3

Please sign in to comment.