Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,26 @@
|
|
| 1 |
-
|
| 2 |
-
"""Untitled1.ipynb
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colab.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1ZlMhMCHSQcigQYuNzsGtJAda3vE_K5R5
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
!pip install transformers datasets gradio
|
| 11 |
-
|
| 12 |
from transformers import DetrFeatureExtractor, DetrForObjectDetection
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
| 14 |
feature_extractor = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
|
| 15 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 16 |
|
| 17 |
-
import torch
|
| 18 |
-
from PIL import Image
|
| 19 |
-
import requests
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 24 |
outputs = model(**inputs)
|
| 25 |
|
| 26 |
-
# convert outputs (bounding boxes and class logits) to COCO API output
|
| 27 |
target_sizes = torch.tensor([image.size[::-1]])
|
| 28 |
results = feature_extractor.post_process(outputs, target_sizes=target_sizes)[0]
|
| 29 |
|
| 30 |
potholes = []
|
| 31 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 32 |
box = [round(i, 2) for i in box.tolist()]
|
| 33 |
-
if score > 0.7:
|
| 34 |
-
# Here, we'll simply identify everything. Ideally, you'd filter by the "pothole" class.
|
| 35 |
potholes.append({
|
| 36 |
"box": box,
|
| 37 |
"score": round(score.item(), 3),
|
|
@@ -40,14 +29,12 @@ def predict(image_url):
|
|
| 40 |
|
| 41 |
return potholes
|
| 42 |
|
| 43 |
-
import gradio as gr
|
| 44 |
-
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=predict,
|
| 47 |
-
inputs=gr.
|
| 48 |
-
outputs=gr.JSON(label="Detected Potholes"),
|
| 49 |
title="Pothole Detection POC",
|
| 50 |
-
description="
|
| 51 |
)
|
| 52 |
|
| 53 |
-
iface.launch(
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import DetrFeatureExtractor, DetrForObjectDetection
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
|
| 7 |
+
# Load the pre-trained model and feature extractor
|
| 8 |
feature_extractor = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
|
| 9 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
|
| 13 |
+
def predict(image):
|
| 14 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 15 |
outputs = model(**inputs)
|
| 16 |
|
|
|
|
| 17 |
target_sizes = torch.tensor([image.size[::-1]])
|
| 18 |
results = feature_extractor.post_process(outputs, target_sizes=target_sizes)[0]
|
| 19 |
|
| 20 |
potholes = []
|
| 21 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 22 |
box = [round(i, 2) for i in box.tolist()]
|
| 23 |
+
if score > 0.7:
|
|
|
|
| 24 |
potholes.append({
|
| 25 |
"box": box,
|
| 26 |
"score": round(score.item(), 3),
|
|
|
|
| 29 |
|
| 30 |
return potholes
|
| 31 |
|
|
|
|
|
|
|
| 32 |
iface = gr.Interface(
|
| 33 |
fn=predict,
|
| 34 |
+
inputs=gr.inputs.Image(type="pil"), # Image upload input
|
| 35 |
+
outputs=gr.outputs.JSON(label="Detected Potholes"),
|
| 36 |
title="Pothole Detection POC",
|
| 37 |
+
description="Upload an image to detect potholes."
|
| 38 |
)
|
| 39 |
|
| 40 |
+
iface.launch()
|