Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,34 +6,123 @@ import numpy as np
|
|
6 |
|
7 |
# function which is returning the number of object detected
|
8 |
def number_object_detected(image):
|
9 |
-
|
10 |
-
|
|
|
11 |
|
12 |
dic = results[0].names
|
13 |
-
|
|
|
|
|
14 |
class_count = {}
|
15 |
-
unique_elements, counts = np.unique(
|
16 |
for e , count in zip(unique_elements,counts):
|
17 |
a = dic[e]
|
18 |
class_count[a] = count
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
return class_count , results
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# Define function to process input image and return annotated image
|
25 |
-
def
|
26 |
-
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
plt.show()
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# function which is returning the number of object detected
|
8 |
def number_object_detected(image):
|
9 |
+
|
10 |
+
custom_model = YOLO('D:/COMSATS/FYP/Coding Part/ModelMaking/Computer_Vision_Part/3rd Try/runs/detect/train9/weights/best.pt') # custome yolo model path
|
11 |
+
results = custom_model(image,verbose= False)
|
12 |
|
13 |
dic = results[0].names
|
14 |
+
classes = results[0].boxes.cls.cpu().numpy()
|
15 |
+
probability = results[0].boxes.conf
|
16 |
+
|
17 |
class_count = {}
|
18 |
+
unique_elements, counts = np.unique(classes, return_counts=True)
|
19 |
for e , count in zip(unique_elements,counts):
|
20 |
a = dic[e]
|
21 |
class_count[a] = count
|
22 |
+
return class_count
|
23 |
+
|
24 |
+
|
25 |
+
def car_detection_and_Cropping(image_path):
|
26 |
+
simple_yolo = YOLO('yolov8m.pt')
|
27 |
+
r = simple_yolo(image_path,verbose = False)
|
28 |
|
|
|
29 |
|
30 |
+
names = r[0].names
|
31 |
+
boxes = r[0].boxes.xyxy.cpu().numpy().astype(int)
|
32 |
+
classes = set(r[0].boxes.cls.cpu().numpy())
|
33 |
+
classes2 = [names[i] for i in classes]
|
34 |
+
|
35 |
+
# checking if the detected object is the car or not
|
36 |
+
# if it is car then crop if not then pass the image as it is
|
37 |
+
if boxes.size != 0 and 'car' in classes2:
|
38 |
+
|
39 |
+
area = []
|
40 |
+
for x1, y1, x2, y2 in boxes:
|
41 |
+
area.append((x2 - x1) * (y2 - y1))
|
42 |
+
max_index, max_a = max(enumerate(area), key=lambda x: x[1])
|
43 |
+
|
44 |
+
|
45 |
+
# Load the image using OpenCV
|
46 |
+
image = cv2.imread(image_path)
|
47 |
+
|
48 |
+
|
49 |
+
# Crop the image
|
50 |
+
crop_image = image[boxes[max_index][1]:boxes[max_index][3], boxes[max_index][0]:boxes[max_index][2]]
|
51 |
+
|
52 |
+
# passing the crop image to the detection model
|
53 |
|
54 |
+
class_c = number_object_detected(crop_image)
|
55 |
+
else:
|
56 |
+
class_c = number_object_detected(image_path)
|
57 |
+
return class_c
|
58 |
+
|
59 |
+
## this function will take the image url and call all the related functions
|
60 |
+
def estimate_condition(detections):
|
61 |
+
print("Detedtion list",detections)
|
62 |
+
max_possible_score = sum(severity_points.values()) # Assuming all types of damage detected
|
63 |
+
score = calculate_condition_score(detections)
|
64 |
+
normalized_score = normalize_score(score, max_possible_score)
|
65 |
+
|
66 |
+
if normalized_score <= 2: # If score is low, condition is Excellent
|
67 |
+
print("Condition Excellent")
|
68 |
+
return "Excellent"
|
69 |
+
elif (normalized_score >2 and normalized_score <=7): # If score is moderately low, condition is Good
|
70 |
+
print("Condition Good")
|
71 |
+
return "Good"
|
72 |
+
elif (normalized_score >7 and normalized_score <15): # If score is moderate, condition is Fair
|
73 |
+
print("Condition Fair")
|
74 |
+
return "Fair"
|
75 |
+
elif (normalized_score >15 and normalized_score<=20): # If score is moderately high, condition is Poor
|
76 |
+
print("Condition Poor")
|
77 |
+
return "Poor"
|
78 |
+
else: # If score is high, condition is Very Poor
|
79 |
+
print("Condition Very Poor")
|
80 |
+
return "Very Poor"
|
81 |
+
|
82 |
+
## loading the model
|
83 |
+
with open('Price_prediction_decision_tree.pkl', 'rb') as file:
|
84 |
+
loaded_pipe_lr = pickle.load(file)
|
85 |
+
|
86 |
# Define function to process input image and return annotated image
|
87 |
+
def process_data(files,car_brand, car_name, model_year, mileage, city_registered, color, engine_c, trans, fuel_type, Cate):
|
88 |
+
print(car_brand, car_name, model_year, mileage, city_registered, color, engine_c, trans, fuel_type, Cate)
|
89 |
+
# Process uploaded filespr
|
90 |
+
print(files)
|
91 |
+
file_names = [f for f in files[0]]
|
92 |
+
print(file_names)
|
93 |
+
# Process text input
|
94 |
+
damage_dic = {}
|
|
|
95 |
|
96 |
+
for i in file_names:
|
97 |
+
data = car_detection_and_Cropping(i)
|
98 |
+
print(data)
|
99 |
+
for key in data.keys():
|
100 |
+
if key in damage_dic:
|
101 |
+
damage_dic[key] += data[key]
|
102 |
+
else:
|
103 |
+
damage_dic[key] = data[key]
|
104 |
+
condition = estimate_condition(damage_dic)
|
105 |
+
#[[2022,46000 , 'punjba','white',1500,'changan','alsvin','automatic','petrol','excellent','sedan']])
|
106 |
+
price = loaded_pipe_lr.predict([[model_year,mileage,city_registered,color,engine_c,car_brand,car_name,trans,fuel_type,condition,Cate]])
|
107 |
+
if price[0] >= 100:
|
108 |
+
price[0] = price[0]/100
|
109 |
+
|
110 |
+
return (damage_dic,condition , str(price[0])+'lacs')
|
111 |
+
|
112 |
+
|
113 |
|
114 |
+
years_list = list(range(2024, 1899, -1))
|
115 |
+
gr.Interface(fn = process_data,
|
116 |
+
inputs=[gr.Gallery(label="Upload Files", type="pil"),
|
117 |
+
gr.Dropdown(['suzuki','toyota','honda','kia'], label='Brand'),
|
118 |
+
gr.Textbox(lines=1, label="Car Name"),
|
119 |
+
gr.Dropdown(choices=years_list, label='Model Year'),
|
120 |
+
gr.Number(label="Mileage Km"),
|
121 |
+
gr.Textbox(lines=1, label="City Register"),
|
122 |
+
gr.Textbox(lines=1, label="Color"),
|
123 |
+
gr.Number(label="Engine Capacity in CC"),
|
124 |
+
gr.Radio(["automatic", "manual"], label="Transmission Type"),
|
125 |
+
# gr.Radio(["imported", "local"], label="Assembly Type"),
|
126 |
+
gr.Radio(["hybrid", "petrol",'diesel'], label="Fuel Type"),
|
127 |
+
gr.Radio(["hatchback", "sedan",'suv','croosover'], label="Category")],
|
128 |
+
outputs=[gr.Textbox(label="Damage List"),gr.Textbox(label="Condition"),gr.Textbox(label="Predicted Price")]).launch()
|