Soham Chandratre commited on
Commit
49ebad1
·
1 Parent(s): 59c748e

minor changes

Browse files
model/__pycache__/pothole_model.cpython-311.pyc CHANGED
Binary files a/model/__pycache__/pothole_model.cpython-311.pyc and b/model/__pycache__/pothole_model.cpython-311.pyc differ
 
model/pothole_model.py CHANGED
@@ -1,55 +1,10 @@
1
- from ultralyticsplus import YOLO, render_result
2
- from PIL import Image
3
- from io import BytesIO
4
- import numpy as np
5
-
6
- def load_model(image):
7
- # image_bytes = image.content
8
- model = YOLO('keremberke/yolov8n-pothole-segmentation')
9
- model.overrides['conf'] = 0.25
10
- model.overrides['iou'] = 0.45
11
- model.overrides['agnostic_nms'] = False
12
- model.overrides['max_det'] = 1000
13
-
14
- # Load image using PIL
15
- image = Image.open(BytesIO(image))
16
- image_array = np.array(image)
17
- # pil_image = pil_image.convert("RGB") # Ensure image is in RGB format
18
-
19
- # Convert PIL image to bytes
20
- # with io.BytesIO() as output:
21
- # pil_image.save(output, format='JPEG')
22
- # image_bytes = output.getvalue()
23
-
24
- results = model.predict(image_array)
25
- for result in results:
26
- boxes = result.boxes.xyxy
27
- conf = result.boxes.conf
28
- cls = result.boxes.cls
29
- obj_info = []
30
- for i, bbox in enumerate(boxes):
31
- label = result.names[int(cls[i])]
32
- obj_info.append({
33
- "Object": i+1,
34
- "Label": label,
35
- "Confidence": conf[i],
36
- "Bounding Box": bbox
37
- })
38
- render = render_result(model=model, image=image, result=results[0])
39
- if label:
40
- print(label)
41
- render.show()
42
- return label
43
-
44
-
45
  # from PIL import Image
46
  # from io import BytesIO
 
47
 
48
- # # Load model directly
49
- # from transformers import AutoImageProcessor, AutoModelForObjectDetection
50
-
51
- # processor = AutoImageProcessor.from_pretrained("savioratharv/pothole_detection")
52
- # model = AutoModelForObjectDetection.from_pretrained("savioratharv/pothole_detection")
53
 
54
  # # Function to predict if an image contains a pothole
55
  # def predict_pothole(image_url):
@@ -67,3 +22,50 @@ def load_model(image):
67
 
68
  # return predicted_class
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # from PIL import Image
2
  # from io import BytesIO
3
+ # from transformers import AutoImageProcessor, AutoModelForImageClassification
4
 
5
+ # # Load model
6
+ # processor = AutoImageProcessor.from_pretrained("taroii/pothole-detection-model")
7
+ # model = AutoModelForImageClassification.from_pretrained("taroii/pothole-detection-model")
 
 
8
 
9
  # # Function to predict if an image contains a pothole
10
  # def predict_pothole(image_url):
 
22
 
23
  # return predicted_class
24
 
25
+
26
+ from keras.models import load_model # TensorFlow is required for Keras to work
27
+ from PIL import Image, ImageOps # Install pillow instead of PIL
28
+ import numpy as np
29
+
30
+
31
+ def load_image_model(image):
32
+ # Disable scientific notation for clarity
33
+ np.set_printoptions(suppress=True)
34
+
35
+ # Load the model
36
+ model = load_model("https://huggingface.co/spaces/Soham0708/pothole_detect/blob/main/keras_model.h5")
37
+
38
+ # Load the labels
39
+ class_names = open("labels.txt", "r").readlines()
40
+
41
+ # Create the array of the right shape to feed into the keras model
42
+ # The 'length' or number of images you can put into the array is
43
+ # determined by the first position in the shape tuple, in this case 1
44
+ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
45
+
46
+ # Replace this with the path to your image
47
+ image = Image.open("<IMAGE_PATH>").convert("RGB")
48
+
49
+ # resizing the image to be at least 224x224 and then cropping from the center
50
+ size = (224, 224)
51
+ image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
52
+
53
+ # turn the image into a numpy array
54
+ image_array = np.asarray(image)
55
+
56
+ # Normalize the image
57
+ normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
58
+
59
+ # Load the image into the array
60
+ data[0] = normalized_image_array
61
+
62
+ # Predicts the model
63
+ prediction = model.predict(data)
64
+ index = np.argmax(prediction)
65
+ class_name = class_names[index]
66
+ confidence_score = prediction[0][index]
67
+
68
+ # Print prediction and confidence score
69
+ print("Class:", class_name[2:], end="")
70
+ print("Confidence Score:", confidence_score)
71
+
routes/__pycache__/route.cpython-311.pyc CHANGED
Binary files a/routes/__pycache__/route.cpython-311.pyc and b/routes/__pycache__/route.cpython-311.pyc differ
 
routes/route.py CHANGED
@@ -1,7 +1,7 @@
1
  from fastapi import APIRouter, HTTPException,Depends,File, UploadFile
2
  from fastapi.responses import JSONResponse
3
  from config.database import admin_collection, user_collection,notification_collection,pothole_image_collection
4
- from model.pothole_model import predict_pothole
5
  from utils.auth import create_access_token, hash_password, verify_password, verify_token
6
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
7
  from schema.model import Admin, PoholeInfo, PotInfoById, PotholeFilters, PotholeModel, UpdatePotholeInfo, User, UserLogin, VerifyOtp
@@ -262,7 +262,7 @@ async def verify_pothole(potholeModel: PotholeModel, token: HTTPAuthorizationCre
262
  image_bytes = response.content
263
 
264
  # Pass image bytes to your model function
265
- results = predict_pothole(image_bytes)
266
 
267
  # if results == 1:
268
  # return JSONResponse(content={"response": "Pothole"})
 
1
  from fastapi import APIRouter, HTTPException,Depends,File, UploadFile
2
  from fastapi.responses import JSONResponse
3
  from config.database import admin_collection, user_collection,notification_collection,pothole_image_collection
4
+ from model.pothole_model import load_image_model
5
  from utils.auth import create_access_token, hash_password, verify_password, verify_token
6
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
7
  from schema.model import Admin, PoholeInfo, PotInfoById, PotholeFilters, PotholeModel, UpdatePotholeInfo, User, UserLogin, VerifyOtp
 
262
  image_bytes = response.content
263
 
264
  # Pass image bytes to your model function
265
+ results = load_image_model(image_bytes)
266
 
267
  # if results == 1:
268
  # return JSONResponse(content={"response": "Pothole"})