Spaces:
Sleeping
Sleeping
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
|
2 |
-
from PIL import Image
|
3 |
-
import numpy as np
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
|
26 |
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
|
33 |
-
#
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
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
|
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 =
|
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"})
|