Soham Chandratre commited on
Commit
14c15c7
·
1 Parent(s): aafa470

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,44 +1,68 @@
1
- from keras.models import load_model # TensorFlow is required for Keras to work
2
- from PIL import Image, ImageOps # Install pillow instead of PIL
3
- import numpy as np
4
 
 
 
 
 
 
 
 
5
 
6
- def load_image_model(image):
7
- np.set_printoptions(suppress=True)
 
 
8
 
9
- # Load the model
10
- model = load_model("keras_Model.h5", compile=False)
 
 
11
 
12
- # Load the labels
13
- class_names = open("labels.txt", "r").readlines()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # Create the array of the right shape to feed into the keras model
16
- # The 'length' or number of images you can put into the array is
17
- # determined by the first position in the shape tuple, in this case 1
18
- data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
19
 
20
- # Replace this with the path to your image
21
- image = Image.open(image).convert("RGB")
22
 
23
- # resizing the image to be at least 224x224 and then cropping from the center
24
- size = (224, 224)
25
- image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
26
 
27
- # turn the image into a numpy array
28
- image_array = np.asarray(image)
29
 
30
- # Normalize the image
31
- normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
 
 
32
 
33
- # Load the image into the array
34
- data[0] = normalized_image_array
 
 
 
 
 
 
35
 
36
- # Predicts the model
37
- prediction = model.predict(data)
38
- index = np.argmax(prediction)
39
- class_name = class_names[index]
40
- confidence_score = prediction[0][index]
41
 
42
- # Print prediction and confidence score
43
- print("Class:", class_name[2:], end="")
44
- print("Confidence Score:", confidence_score)
 
1
+ # from ultralyticsplus import YOLO, render_result
2
+ # from PIL import Image
3
+ # import numpy as np
4
 
5
+ # def load_model(image):
6
+ # # image_bytes = image.content
7
+ # model = YOLO('keremberke/yolov8n-pothole-segmentation')
8
+ # model.overrides['conf'] = 0.25
9
+ # model.overrides['iou'] = 0.45
10
+ # model.overrides['agnostic_nms'] = False
11
+ # model.overrides['max_det'] = 1000
12
 
13
+ # # Load image using PIL
14
+ # image = Image.open((image))
15
+ # image_array = np.array(image)
16
+ # # pil_image = pil_image.convert("RGB") # Ensure image is in RGB format
17
 
18
+ # # Convert PIL image to bytes
19
+ # # with io.BytesIO() as output:
20
+ # # pil_image.save(output, format='JPEG')
21
+ # # image_bytes = output.getvalue()
22
 
23
+ # results = model.predict(image_array)
24
+ # for result in results:
25
+ # boxes = result.boxes.xyxy
26
+ # conf = result.boxes.conf
27
+ # cls = result.boxes.cls
28
+ # obj_info = []
29
+ # for i, bbox in enumerate(boxes):
30
+ # label = result.names[int(cls[i])]
31
+ # obj_info.append({
32
+ # "Object": i+1,
33
+ # "Label": label,
34
+ # "Confidence": conf[i],
35
+ # "Bounding Box": bbox
36
+ # })
37
+ # render = render_result(model=model, image=image, result=results[0])
38
+ # if label:
39
+ # print(label)
40
+ # render.show()
41
+ # return label
42
 
 
 
 
 
43
 
44
+ from PIL import Image
45
+ from io import BytesIO
46
 
47
+ # Load model directly
48
+ from transformers import AutoImageProcessor, AutoModelForObjectDetection
 
49
 
50
+ processor = AutoImageProcessor.from_pretrained("savioratharv/pothole_detection")
51
+ model = AutoModelForObjectDetection.from_pretrained("savioratharv/pothole_detection")
52
 
53
+ # Function to predict if an image contains a pothole
54
+ def predict_pothole(image_url):
55
+ image = Image.open(BytesIO(image_url))
56
+ inputs = processor(images=image, return_tensors="pt")
57
 
58
+ # Perform inference
59
+ outputs = model(**inputs)
60
+ logits = outputs.logits
61
+ probabilities = logits.softmax(dim=1)
62
+
63
+ # Get predicted class (0: No pothole, 1: Pothole)
64
+ predicted_class = probabilities.argmax().item()
65
+ confidence = probabilities[0, predicted_class].item()
66
 
67
+ return predicted_class
 
 
 
 
68
 
 
 
 
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 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,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 = load_image_model(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 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
  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"})