fadzwan commited on
Commit
74567cf
Β·
verified Β·
1 Parent(s): 08f0f35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -90
app.py CHANGED
@@ -1,106 +1,55 @@
1
  import gradio as gr
2
- from ultralytics import YOLO
3
- import cv2
4
- import easyocr
5
- import numpy as np
6
  from PIL import Image
 
 
 
7
  from transformers import pipeline
8
 
9
- # Load YOLO model for license plate detection
10
- model = YOLO('yolo11n-custom.pt')
11
- model.fuse()
12
-
13
- # Load EasyOCR
14
- reader = easyocr.Reader(['en'])
15
-
16
- # Load Transformers pipeline for car damage classification
17
  damage_pipe = pipeline("image-classification", model="beingamit99/car_damage_detection")
18
 
19
- # --- License Plate Detection Function ---
20
- def detect_license_plate(image):
21
- results = model.predict(image, conf=0.15, iou=0.3, classes=[0])
22
- plate_texts = []
23
- img_array = np.array(image)
24
- img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
25
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
26
-
27
- img_height, img_width, _ = img.shape
28
-
29
- for result in results:
30
- for bbox in result.boxes.xyxy:
31
- x1, y1, x2, y2 = map(int, bbox.tolist())
32
- plate = img[int(y1):int(y2), int(x1):int(x2)]
33
- scale = 2
34
- height, width = plate.shape[:2]
35
- plate = cv2.resize(plate, (width * scale, height * scale), interpolation=cv2.INTER_CUBIC)
36
- lab = cv2.cvtColor(plate, cv2.COLOR_RGB2LAB)
37
- l, a, b = cv2.split(lab)
38
- clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
39
- l = clahe.apply(l)
40
- plate = cv2.merge((l, a, b))
41
- plate = cv2.cvtColor(plate, cv2.COLOR_LAB2RGB)
42
-
43
- text = reader.readtext(plate, detail=0, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-")
44
- text = " ".join(text).upper()
45
 
46
- text_scale = max(1, width / 250)
47
- thickness = max(2, width // 200)
48
- cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), thickness)
49
- (text_width, text_height), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, text_scale, thickness)
50
- text_x = x1 + (width - text_width) // 2
51
- text_y = y1 - 10 if y1 > 50 else y2 + text_height + 20
52
- text_box_y1 = text_y - text_height - 5
53
- text_box_y2 = text_y + 5
54
- cv2.rectangle(img, (text_x - 8, text_box_y1 - 3), (text_x + text_width + 8, text_box_y2 + 3), (0, 0, 0), -1)
55
- cv2.rectangle(img, (text_x - 5, text_box_y1), (text_x + text_width + 5, text_box_y2), (255, 255, 255), -1)
56
- cv2.putText(img, text, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, text_scale, (0, 0, 0), thickness)
57
 
58
- plate_texts.append(text)
59
 
60
- result_img = Image.fromarray(img)
61
- return result_img, "\n".join(plate_texts) if plate_texts else "No license plates detected."
 
62
 
63
- # --- Car Damage Classification Function ---
64
- import plotly.graph_objects as go
 
65
 
66
- def classify_car_damage(image):
67
- results = damage_pipe(image)
68
- labels = [item["label"] for item in results]
69
- scores = [item["score"] for item in results]
 
 
70
 
71
- fig = go.Figure(
72
- data=[go.Bar(x=labels, y=scores, marker_color="crimson")],
73
- layout=go.Layout(
74
- title="Car Damage Classification",
75
- yaxis=dict(title="Confidence Score", range=[0, 1]),
76
- xaxis=dict(title="Damage Type"),
77
- template="plotly_white"
78
- )
79
- )
80
 
81
- return image, fig
 
 
82
 
83
- # --- Gradio Interface ---
84
- plate_tab = gr.Interface(
85
- fn=detect_license_plate,
86
- inputs=gr.Image(type="pil"),
87
- outputs=[
88
- gr.Image(type="pil", label="Detected Image"),
89
- gr.Textbox(label="Detected License Plates")
90
- ],
91
- title="🚘 License Plate Detector",
92
- description="Upload an image with license plates to detect them using YOLO and EasyOCR."
93
- )
94
 
95
- damage_tab = gr.Interface(
96
- fn=classify_car_damage,
97
- inputs=gr.Image(type="pil", label="Upload Damaged Car Image"),
98
- outputs=[
99
- gr.Image(type="pil", label="Car Image"),
100
- gr.Plot(label="Damage Classification")
101
- ],
102
- title="πŸ’₯ Car Damage Detector",
103
- description="Classifies car damage severity (minor, moderate, severe)."
104
- )
105
 
106
- gr.TabbedInterface([plate_tab, damage_tab], ["License Plate Detector", "Car Damage Detector"]).launch()
 
1
  import gradio as gr
 
 
 
 
2
  from PIL import Image
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ from ultralytics import YOLO
6
  from transformers import pipeline
7
 
8
+ # Load models
9
+ license_model = YOLO('yolov8n.pt') # public YOLOv8 model
 
 
 
 
 
 
10
  damage_pipe = pipeline("image-classification", model="beingamit99/car_damage_detection")
11
 
12
+ # License plate detection
13
+ def detect_license_plate(img: Image.Image):
14
+ results = license_model.predict(img)
15
+ result = results[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Plot boxes
18
+ img = np.array(img.convert("RGB"))
19
+ for box in result.boxes.xyxy:
20
+ x1, y1, x2, y2 = box.int().tolist()
21
+ img = img.copy()
22
+ img = cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
 
 
 
 
 
23
 
24
+ return Image.fromarray(img)
25
 
26
+ # Damage classification with bar chart
27
+ def classify_damage(img: Image.Image):
28
+ predictions = damage_pipe(img)
29
 
30
+ # Prepare bar graph
31
+ labels = [pred["label"] for pred in predictions]
32
+ scores = [pred["score"] for pred in predictions]
33
 
34
+ fig, ax = plt.subplots()
35
+ ax.barh(labels, scores, color='orange')
36
+ ax.set_xlim(0, 1)
37
+ ax.set_xlabel("Confidence")
38
+ ax.set_title("Damage Classification")
39
+ plt.tight_layout()
40
 
41
+ return fig
 
 
 
 
 
 
 
 
42
 
43
+ # Combined interface
44
+ with gr.Blocks() as app:
45
+ gr.Markdown("# πŸš— Car License Plate & Damage Detector")
46
 
47
+ with gr.Row():
48
+ image_input = gr.Image(type="pil", label="Upload Car Image")
49
+ license_output = gr.Image(label="License Plate Detection")
50
+ damage_output = gr.Plot(label="Damage Classification")
 
 
 
 
 
 
 
51
 
52
+ image_input.change(fn=detect_license_plate, inputs=image_input, outputs=license_output)
53
+ image_input.change(fn=classify_damage, inputs=image_input, outputs=damage_output)
 
 
 
 
 
 
 
 
54
 
55
+ app.launch()