Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,41 @@
|
|
| 1 |
-
# Install and update the necessary libraries
|
| 2 |
-
#import logging
|
| 3 |
-
#import sys
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from transformers import pipeline
|
| 6 |
-
import os
|
| 7 |
import gradio as gr
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Suppress non-critical
|
| 10 |
-
from transformers.utils import logging
|
| 11 |
-
logging.set_verbosity_error()
|
| 12 |
-
|
| 13 |
-
#from helper import ignore_warnings
|
| 14 |
-
#ignore_warnings()
|
| 15 |
-
#import sys
|
| 16 |
-
#sys.path.append("./Object-Detection/helper.py") # Adjust the path as necessary to point to the directory where helper.py is located
|
| 17 |
-
|
| 18 |
-
#from Object-Detection.helper import ignore_warnings
|
| 19 |
-
#from helper import ignore_warnings
|
| 20 |
-
from helper import *
|
| 21 |
ignore_warnings()
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#sys.path.append("./Open_Source_Models_with_Hugging_Face/Object_Detection/helper.py") # Adjust the path as necessary to point to the directory where helper.py is located
|
| 30 |
-
|
| 31 |
-
#from Object-Detection.helper import import load_image_from_url, render_results_in_image
|
| 32 |
-
#from helper import load_image_from_url, render_results_in_image
|
| 33 |
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# Resize the image
|
| 38 |
-
#raw_image.resize((569, 491))
|
| 39 |
|
| 40 |
-
|
| 41 |
-
#processed_image = render_results_in_image(raw_image, pipeline_output)
|
| 42 |
-
#processed_image
|
| 43 |
|
| 44 |
-
# gradio interface
|
| 45 |
-
def get_pipeline_prediction(pil_image):
|
| 46 |
-
pipeline_output = pipe(pil_image)
|
| 47 |
-
processed_image = render_results_in_image(pil_image, pipeline_output)
|
| 48 |
-
return processed_image
|
| 49 |
|
|
|
|
| 50 |
iface = gr.Interface(
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from helper import render_results_in_image, summarize_predictions_natural_language, ignore_warnings
|
| 4 |
|
| 5 |
+
# Suppress non-critical warnings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
ignore_warnings()
|
| 7 |
|
| 8 |
+
# Load DETR object detection pipeline with lower threshold
|
| 9 |
+
pipe = pipeline("object-detection", model="facebook/detr-resnet-50", threshold=0.3)
|
| 10 |
|
| 11 |
+
# Function to get predictions and process image + summary
|
| 12 |
+
def get_pipeline_prediction(pil_image):
|
| 13 |
+
pipeline_output = pipe(pil_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
if not pipeline_output:
|
| 16 |
+
return pil_image, "No objects detected."
|
| 17 |
|
| 18 |
+
processed_image = render_results_in_image(pil_image, pipeline_output)
|
| 19 |
+
summary = summarize_predictions_natural_language(pipeline_output)
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
return processed_image, summary
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Gradio interface
|
| 25 |
iface = gr.Interface(
|
| 26 |
+
fn=get_pipeline_prediction,
|
| 27 |
+
inputs=gr.Image(label="Upload an Image", type="pil"),
|
| 28 |
+
outputs=[
|
| 29 |
+
gr.Image(label="Image with Detected Objects", type="pil"),
|
| 30 |
+
gr.Textbox(label="Summary of Detected Objects")
|
| 31 |
+
],
|
| 32 |
+
title="Object Detection with DETR",
|
| 33 |
+
description="Upload an image to detect objects. Bounding boxes will be drawn and a natural language summary will be provided.",
|
| 34 |
+
flagging_mode="never"
|
| 35 |
)
|
| 36 |
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
iface.launch()
|
| 39 |
|
| 40 |
|
| 41 |
|