Vahe commited on
Commit
5e23bb3
·
1 Parent(s): 7f98cae

digit extractor added

Browse files
Files changed (3) hide show
  1. app.py +8 -2
  2. digit_yolov8_best_float16.tflite +3 -0
  3. models.py +56 -0
app.py CHANGED
@@ -9,7 +9,7 @@ from PIL import Image, ImageDraw
9
  # import platform
10
  # from xyxy_converter import yolov5_to_image_coordinates
11
  # import shutil
12
- from models import get_odometer_xy
13
 
14
  # system_platform = platform.system()
15
  # if system_platform == 'Windows': pathlib.PosixPath = pathlib.WindowsPath
@@ -48,8 +48,14 @@ def main():
48
  st.image('odometer_image.jpg', caption=f"{display_text}", use_column_width=True)
49
  else:
50
  cropped_image = gray[y1:y2, x1:x2]
 
51
  cv2.imwrite('odometer_number_image.jpg', cropped_image)
52
- display_text = 'Here is the zoomed odometer value'
 
 
 
 
 
53
  st.image('odometer_number_image.jpg', caption=f"{display_text}", use_column_width=True)
54
 
55
  image = Image.open('odometer_image.jpg')
 
9
  # import platform
10
  # from xyxy_converter import yolov5_to_image_coordinates
11
  # import shutil
12
+ from models import get_odometer_xy, get_digit
13
 
14
  # system_platform = platform.system()
15
  # if system_platform == 'Windows': pathlib.PosixPath = pathlib.WindowsPath
 
48
  st.image('odometer_image.jpg', caption=f"{display_text}", use_column_width=True)
49
  else:
50
  cropped_image = gray[y1:y2, x1:x2]
51
+ # cropped_image = resized_image[y1:y2, x1:x2]
52
  cv2.imwrite('odometer_number_image.jpg', cropped_image)
53
+ extracted_digit = get_digit(
54
+ model_path="digit_yolov8_best_float16.tflite",
55
+ image_path='odometer_number_image.jpg',
56
+ threshold=0.4
57
+ )
58
+ display_text = f'Here is the zoomed odometer value: {extracted_digit}.'
59
  st.image('odometer_number_image.jpg', caption=f"{display_text}", use_column_width=True)
60
 
61
  image = Image.open('odometer_image.jpg')
digit_yolov8_best_float16.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:64673733adce09ce682b4527410ece79f62108e33cea42e8b14166e695e67d55
3
+ size 6195116
models.py CHANGED
@@ -1,6 +1,7 @@
1
  import tensorflow as tf
2
  import numpy as np
3
  from PIL import Image
 
4
 
5
  def get_odometer_xy(model_path, image_path):
6
  #model_path = 'odo_detector.tflite'
@@ -67,4 +68,59 @@ def get_odometer_xy(model_path, image_path):
67
 
68
  return x1, y1, x2, y2, final_score
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
 
1
  import tensorflow as tf
2
  import numpy as np
3
  from PIL import Image
4
+ import cv2
5
 
6
  def get_odometer_xy(model_path, image_path):
7
  #model_path = 'odo_detector.tflite'
 
68
 
69
  return x1, y1, x2, y2, final_score
70
 
71
+ def get_digit(model_path, image_path, threshold=0.5):
72
+
73
+ interpreter = tf.lite.Interpreter(model_path=model_path)
74
+ interpreter.allocate_tensors()
75
+
76
+ input_details = interpreter.get_input_details()
77
+ output_details = interpreter.get_output_details()
78
+
79
+ # Obtain the height and width of the corresponding image from the input tensor
80
+ image_height = input_details[0]['shape'][1] # 640
81
+ image_width = input_details[0]['shape'][2] # 640
82
+
83
+ # Image Preparation
84
+ # image_name = 'car.jpg'
85
+ # image = Image.open(image_path2)
86
+ # image_resized = image.resize((image_width, image_height)) # Resize the image to the corresponding size of the input tensor and store it in a new variable
87
+ image = cv2.imread(image_path)
88
+ image_resized = np.resize(image, (image_width, image_height, 3))
89
+
90
+ image_np = np.array(image_resized) #
91
+ image_np = np.true_divide(image_np, 255, dtype=np.float32)
92
+ image_np = image_np[np.newaxis, :]
93
+
94
+ # inference
95
+ interpreter.set_tensor(input_details[0]['index'], image_np)
96
+ interpreter.invoke()
97
+
98
+ # Obtaining output results
99
+ output = interpreter.get_tensor(output_details[0]['index'])
100
+ output = output[0]
101
+ output = output.T
102
+
103
+ boxes_xywh = output[:, :4] #Get coordinates of bounding box, first 4 columns of output tensor
104
+ scores = output[:, 4]#np.max(output[..., 5:], axis=1) #Get score value, 5th column of output tensor
105
+ classes = np.argmax(output[:, 5:], axis=1) # Get the class value, get the 6th and subsequent columns of the output tensor, and store the largest value in the output tensor.
106
+
107
+ pred_list = []
108
+
109
+ for box, score, cls in zip(boxes_xywh, scores, classes):
110
+ x_center, y_center, width, height = box
111
+ x1 = int((x_center - width / 2) * image_width)
112
+ y1 = int((y_center - height / 2) * image_height)
113
+ x2 = int((x_center + width / 2) * image_width)
114
+ y2 = int((y_center + height / 2) * image_height)
115
+ pred_list.append(((x1, x2, cls, score)))
116
+
117
+ pred_list = [i for i in pred_list if i[-1] > threshold]
118
+ sorted_number_list = [i for i in sorted_number_list if i[0]>0 and i[1]>0]
119
+ sorted_number_list = sorted(pred_list, key=lambda x: x[0])
120
+ # sorted_number_list = sorted(sorted_number_list, reverse=True, key= lambda x: x[-1])
121
+ # output_digit = float(''.join([str(int(i[2])) if i[2]!=10 else '.' for i in sorted_number_list]))
122
+ output_digit = ''.join([str(int(i[2])) if i[2]!=10 else '.' for i in sorted_number_list])
123
+ # output_digit = ''.join([str(int(i[2])) if i[2]!=10 else '.' for i in sorted_number_list[:10]])
124
+
125
+ return output_digit
126