davelop commited on
Commit
08270a8
·
verified ·
1 Parent(s): fae2e8a

initial commit

Browse files
Files changed (4) hide show
  1. India_009852.jpg +0 -0
  2. Japan_011558.jpg +0 -0
  3. app.py +62 -0
  4. usa.jpg +0 -0
India_009852.jpg ADDED
Japan_011558.jpg ADDED
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ import os
5
+ import pathlib
6
+
7
+ # Fixing path issue for Windows
8
+ # temp = pathlib.PosixPath
9
+ # pathlib.PosixPath = pathlib.WindowsPath
10
+
11
+ # Load your custom weights
12
+ weights_path = 'yolov5/runs/train/road_damage_detection/weights/best.pt' # Update this path to your actual weights file
13
+ model = torch.hub.load('ultralytics/yolov5', 'custom', weights_path, force_reload=True)
14
+ model.names = ["D00", "D10", "D20", "D40"]
15
+
16
+ def yolo(im, size=640):
17
+ # Resize image while maintaining aspect ratio
18
+ g = (size / max(im.size)) # Gain
19
+ im = im.resize(tuple(int(x * g) for x in im.size)) # Resize
20
+
21
+ # Perform inference
22
+ results = model(im)
23
+
24
+ # Render results (updates results.imgs with boxes and labels)
25
+ results.render()
26
+
27
+ # Save the result image
28
+ output_path = "image0.jpg"
29
+ results.save(save_dir=".", exist_ok=True) # Save in the current directory
30
+
31
+ # Prepare Markdown text information
32
+ unique_labels = set() # To track unique labels
33
+ for i, label in enumerate(results.names):
34
+ # Collect detection information
35
+ boxes = results.xyxy[0].cpu().numpy()
36
+ for box in boxes:
37
+ if int(box[5]) == i:
38
+ label_text = f"{results.names[i]} -> {label_mapping.get(results.names[i], 'Unknown')}"
39
+ unique_labels.add(label_text) # Add unique label to set
40
+
41
+ # Join unique labels into Markdown formatted string
42
+ markdown_text = "\n".join(unique_labels) if unique_labels else "No damage detected."
43
+
44
+ return output_path, markdown_text
45
+
46
+ # Label mapping for display
47
+ label_mapping = {
48
+ "D00": "Longitudinal Crack",
49
+ "D10": "Transverse Crack",
50
+ "D20": "Alligator Crack",
51
+ "D40": "Pothole"
52
+ }
53
+
54
+ inputs = gr.Image(type="pil", label="Input Image")
55
+ outputs = [gr.Image(type="pil", label="Output Image"), gr.Markdown(label="Detection Info")]
56
+
57
+ title = "Road Damage Detection"
58
+ description = "The Road Damage Detection project utilizes the YOLO model to automate the detection and classification of road damage from images. By processing images to identify and categorize damage types such as cracks and potholes, the project aims to enhance infrastructure management through timely and efficient maintenance. The model is trained on annotated datasets and evaluated using Intersection over Union (IoU) to ensure accuracy. This approach reduces the need for manual inspections, improving road safety and maintenance efficiency."
59
+
60
+ examples = [['India_009852.jpg'], ['Japan_011558.jpg'], ['usa.jpg']]
61
+
62
+ gr.Interface(fn=yolo, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples).launch()
usa.jpg ADDED