Soham Chandratre commited on
Commit
94c7394
·
1 Parent(s): 5e6c258

minor changes

Browse files
Files changed (4) hide show
  1. keras_model.h5 +3 -0
  2. labels.txt +2 -0
  3. model/pothole_model.py +33 -56
  4. requirements.txt +1 -2
keras_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c72459c9ab862eb75b5d073e54b34dd04e81c1fc117aa64dcff248c97e97840
3
+ size 2453432
labels.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ 0 pothole
2
+ 1 notpothole
model/pothole_model.py CHANGED
@@ -1,67 +1,44 @@
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
- from transformers import AutoImageProcessor, AutoModelForImageClassification
47
 
48
- # Load model
49
- processor = AutoImageProcessor.from_pretrained("taroii/pothole-detection-model")
50
- model = AutoModelForImageClassification.from_pretrained("taroii/pothole-detection-model")
51
 
52
- # Function to predict if an image contains a pothole
53
- def predict_pothole(image_url):
54
- image = Image.open(BytesIO(image_url))
55
- inputs = processor(images=image, return_tensors="pt")
56
 
57
- # Perform inference
58
- outputs = model(**inputs)
59
- logits = outputs.logits
60
- probabilities = logits.softmax(dim=1)
61
-
62
- # Get predicted class (0: No pothole, 1: Pothole)
63
- predicted_class = probabilities.argmax().item()
64
- confidence = probabilities[0, predicted_class].item()
65
 
66
- return predicted_class
 
67
 
 
 
 
 
 
 
 
 
 
 
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)
requirements.txt CHANGED
@@ -7,6 +7,5 @@ python-jose[cryptography]
7
  python-multipart
8
  certifi
9
  firebase-admin
10
- transformers
11
  pillow
12
- torch
 
7
  python-multipart
8
  certifi
9
  firebase-admin
 
10
  pillow
11
+ tensorflow==2.13.1