fadzwan commited on
Commit
638de5c
Β·
verified Β·
1 Parent(s): 74567cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -30
app.py CHANGED
@@ -1,55 +1,62 @@
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()
 
1
  import gradio as gr
2
  from PIL import Image
 
3
  import numpy as np
4
+ import cv2
5
  from ultralytics import YOLO
6
  from transformers import pipeline
7
+ import matplotlib.pyplot as plt
8
 
9
  # Load models
10
+ yolo_model = YOLO("yolov8n.pt")
11
  damage_pipe = pipeline("image-classification", model="beingamit99/car_damage_detection")
12
 
13
+ # License Plate Detection
14
  def detect_license_plate(img: Image.Image):
15
+ results = yolo_model.predict(img)
16
  result = results[0]
17
+ img_array = np.array(img.convert("RGB"))
18
 
 
 
19
  for box in result.boxes.xyxy:
20
+ x1, y1, x2, y2 = map(int, box)
21
+ img_array = cv2.rectangle(img_array, (x1, y1), (x2, y2), (0, 255, 0), 2)
 
22
 
23
+ return Image.fromarray(img_array)
24
 
25
+ # Car Damage Classification
26
+ def classify_damage(image: Image.Image):
27
+ results = damage_pipe(image)
28
+ labels = [res["label"] for res in results]
29
+ scores = [res["score"] for res in results]
 
 
30
 
31
  fig, ax = plt.subplots()
32
+ ax.barh(labels, scores, color="crimson")
 
33
  ax.set_xlabel("Confidence")
34
+ ax.set_xlim(0, 1)
35
+ ax.set_title("Damage Type Classification")
36
  plt.tight_layout()
37
 
38
  return fig
39
 
40
+ # Build UI
41
  with gr.Blocks() as app:
42
+ gr.Markdown("# 🚘 Car Analyzer\nChoose a tool below:")
43
+
44
+ with gr.Tab("πŸ›‚ License Plate Detection"):
45
+ with gr.Row():
46
+ with gr.Column():
47
+ lp_input = gr.Image(type="pil", label="Upload Car Image")
48
+ lp_btn = gr.Button("Detect")
49
+ with gr.Column():
50
+ lp_output = gr.Image(label="Detected License Plate")
51
+ lp_btn.click(detect_license_plate, inputs=lp_input, outputs=lp_output)
52
+
53
+ with gr.Tab("πŸ’₯ Car Damage Classification"):
54
+ with gr.Row():
55
+ with gr.Column():
56
+ dmg_input = gr.Image(type="pil", label="Upload Damaged Car Image")
57
+ dmg_btn = gr.Button("Classify")
58
+ with gr.Column():
59
+ dmg_output = gr.Plot(label="Classification Results")
60
+ dmg_btn.click(classify_damage, inputs=dmg_input, outputs=dmg_output)
61
 
62
  app.launch()