Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
from ultralytics import YOLO ## for Yolov8
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# function which is returning the number of object detected
|
8 |
+
def number_object_detected(image):
|
9 |
+
custom_model = YOLO("runs/detect/train9/weights/best.pt")
|
10 |
+
results = custom_model(image)
|
11 |
+
|
12 |
+
dic = results[0].names
|
13 |
+
cls = results[0].boxes.cls.cpu().numpy()
|
14 |
+
class_count = {}
|
15 |
+
unique_elements, counts = np.unique(cls, return_counts=True)
|
16 |
+
for e , count in zip(unique_elements,counts):
|
17 |
+
a = dic[e]
|
18 |
+
class_count[a] = count
|
19 |
+
print(f"the car has {count} {a}")
|
20 |
+
|
21 |
+
return class_count , results
|
22 |
+
|
23 |
+
|
24 |
+
# Define function to process input image and return annotated image
|
25 |
+
def detect_objects(input_image):
|
26 |
+
cls , result = number_object_detected(input_image)
|
27 |
+
# Plot the results and return annotated image
|
28 |
+
for r in result:
|
29 |
+
im_array = r.plot(pil = True) # plot a BGR numpy array of predictions
|
30 |
+
array = im_array[..., ::-1] # Convert BGR to RGB PIL image
|
31 |
+
plt.axis("off")
|
32 |
+
plt.imshow(array)
|
33 |
+
plt.show()
|
34 |
+
|
35 |
+
return array , cls
|
36 |
+
|
37 |
+
# Define Gradio interface
|
38 |
+
interface = gr.Interface(fn=detect_objects, inputs=gr.Image(type= 'pil', label='Upload Image of Car'), outputs=[gr.Image(),gr.Textbox(label="Number of Objects detected ")], title=" 🚘Car Scratch and Dent Detection")
|
39 |
+
interface.launch()
|