Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import gradio as gr
|
|
| 6 |
from PIL import Image
|
| 7 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 8 |
import supervision as sv
|
| 9 |
-
import spaces
|
| 10 |
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
|
|
@@ -18,28 +17,21 @@ MASK_ANNOTATOR = sv.MaskAnnotator()
|
|
| 18 |
LABEL_ANNOTATOR = sv.LabelAnnotator()
|
| 19 |
TRACKER = sv.ByteTrack()
|
| 20 |
|
| 21 |
-
def annotate_image(
|
| 22 |
-
input_image,
|
| 23 |
-
detections,
|
| 24 |
-
labels
|
| 25 |
-
) -> np.ndarray:
|
| 26 |
output_image = MASK_ANNOTATOR.annotate(input_image, detections)
|
| 27 |
output_image = BOUNDING_BOX_ANNOTATOR.annotate(output_image, detections)
|
| 28 |
output_image = LABEL_ANNOTATOR.annotate(output_image, detections, labels=labels)
|
| 29 |
return output_image
|
| 30 |
|
| 31 |
-
def process_image(
|
| 32 |
-
input_image,
|
| 33 |
-
confidence_threshold,
|
| 34 |
-
):
|
| 35 |
-
results = query(input_image, confidence_threshold)
|
| 36 |
detections = sv.Detections.from_transformers(results[0])
|
| 37 |
detections = TRACKER.update_with_detections(detections)
|
| 38 |
final_labels = [model.config.id2label[label] for label in detections.class_id.tolist()]
|
| 39 |
output_image = annotate_image(input_image, detections, final_labels)
|
| 40 |
-
return output_image
|
| 41 |
|
| 42 |
-
def query(image, confidence_threshold):
|
| 43 |
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 44 |
with torch.no_grad():
|
| 45 |
outputs = model(**inputs)
|
|
@@ -48,19 +40,22 @@ def query(image, confidence_threshold):
|
|
| 48 |
return results
|
| 49 |
|
| 50 |
def run_demo():
|
| 51 |
-
input_image = gr.
|
| 52 |
-
conf = gr.
|
| 53 |
-
output_image = gr.
|
|
|
|
| 54 |
|
| 55 |
def process_and_display(input_image, conf):
|
| 56 |
-
output_img = process_image(input_image, conf)
|
| 57 |
-
return output_img
|
| 58 |
|
| 59 |
gr.Interface(
|
| 60 |
fn=process_and_display,
|
| 61 |
inputs=[input_image, conf],
|
| 62 |
-
outputs=output_image,
|
| 63 |
title="Real Time Object Detection with RT-DETR",
|
| 64 |
-
description="This
|
| 65 |
-
capture_session=True,
|
| 66 |
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 8 |
import supervision as sv
|
|
|
|
| 9 |
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
|
|
|
|
| 17 |
LABEL_ANNOTATOR = sv.LabelAnnotator()
|
| 18 |
TRACKER = sv.ByteTrack()
|
| 19 |
|
| 20 |
+
def annotate_image(input_image: np.ndarray, detections, labels: List[str]) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
output_image = MASK_ANNOTATOR.annotate(input_image, detections)
|
| 22 |
output_image = BOUNDING_BOX_ANNOTATOR.annotate(output_image, detections)
|
| 23 |
output_image = LABEL_ANNOTATOR.annotate(output_image, detections, labels=labels)
|
| 24 |
return output_image
|
| 25 |
|
| 26 |
+
def process_image(input_image: np.ndarray, confidence_threshold: float):
|
| 27 |
+
results = query(Image.fromarray(input_image), confidence_threshold)
|
|
|
|
|
|
|
|
|
|
| 28 |
detections = sv.Detections.from_transformers(results[0])
|
| 29 |
detections = TRACKER.update_with_detections(detections)
|
| 30 |
final_labels = [model.config.id2label[label] for label in detections.class_id.tolist()]
|
| 31 |
output_image = annotate_image(input_image, detections, final_labels)
|
| 32 |
+
return output_image, ", ".join(final_labels)
|
| 33 |
|
| 34 |
+
def query(image: Image.Image, confidence_threshold: float):
|
| 35 |
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 36 |
with torch.no_grad():
|
| 37 |
outputs = model(**inputs)
|
|
|
|
| 40 |
return results
|
| 41 |
|
| 42 |
def run_demo():
|
| 43 |
+
input_image = gr.Image(label="Input Image", type="numpy")
|
| 44 |
+
conf = gr.Slider(label="Confidence Threshold", minimum=0.1, maximum=1.0, value=0.6, step=0.05)
|
| 45 |
+
output_image = gr.Image(label="Output Image", type="numpy")
|
| 46 |
+
output_text = gr.Textbox(label="Detected Classes")
|
| 47 |
|
| 48 |
def process_and_display(input_image, conf):
|
| 49 |
+
output_img, detected_classes = process_image(input_image, conf)
|
| 50 |
+
return output_img, detected_classes
|
| 51 |
|
| 52 |
gr.Interface(
|
| 53 |
fn=process_and_display,
|
| 54 |
inputs=[input_image, conf],
|
| 55 |
+
outputs=[output_image, output_text],
|
| 56 |
title="Real Time Object Detection with RT-DETR",
|
| 57 |
+
description="This demo uses RT-DETR for object detection in images. Adjust the confidence threshold to see different results.",
|
|
|
|
| 58 |
).launch()
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
run_demo()
|