dalybuilds commited on
Commit
e458ce7
·
verified ·
1 Parent(s): 551b3dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -25
app.py CHANGED
@@ -1,37 +1,26 @@
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),
@@ -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.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)
 
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()