Spaces:
Sleeping
Sleeping
Upload potholedetector.py
Browse files- potholedetector.py +53 -0
potholedetector.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 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 |
+
def predict(image_url):
|
| 22 |
+
image = Image.open(requests.get(image_url, stream=True).raw)
|
| 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: # Adjust threshold as needed
|
| 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),
|
| 38 |
+
"label": model.config.id2label[label.item()]
|
| 39 |
+
})
|
| 40 |
+
|
| 41 |
+
return potholes
|
| 42 |
+
|
| 43 |
+
import gradio as gr
|
| 44 |
+
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=predict,
|
| 47 |
+
inputs=gr.Textbox(label="Image URL"), # Input is a text box for the image URL
|
| 48 |
+
outputs=gr.JSON(label="Detected Potholes"), # Output the results as JSON
|
| 49 |
+
title="Pothole Detection POC",
|
| 50 |
+
description="Enter the URL of an image to detect potholes."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
iface.launch(debug=True) # Launch the interface within the Colab notebook (for testing)
|