AIQuest commited on
Commit
004faf4
·
verified ·
1 Parent(s): d0fb98f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -19
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
- custom_model = YOLO("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()
 
 
 
 
 
 
 
 
 
 
 
 
 
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()