Spaces:
Sleeping
Sleeping
the digit extractor model changed
Browse files- app.py +3 -0
- digit_yolov8_best_float16.tflite +2 -2
- models.py +42 -9
app.py
CHANGED
|
@@ -49,12 +49,15 @@ def main():
|
|
| 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 |
|
|
|
|
| 49 |
else:
|
| 50 |
cropped_image = gray[y1:y2, x1:x2]
|
| 51 |
# cropped_image = resized_image[y1:y2, x1:x2]
|
| 52 |
+
cropped_image = cv2.resize(cropped_image, (640, 640))
|
| 53 |
cv2.imwrite('odometer_number_image.jpg', cropped_image)
|
| 54 |
+
|
| 55 |
extracted_digit = get_digit(
|
| 56 |
model_path="digit_yolov8_best_float16.tflite",
|
| 57 |
image_path='odometer_number_image.jpg',
|
| 58 |
threshold=0.4
|
| 59 |
)
|
| 60 |
+
|
| 61 |
display_text = f'Here is the zoomed odometer value: {extracted_digit}.'
|
| 62 |
st.image('odometer_number_image.jpg', caption=f"{display_text}", use_column_width=True)
|
| 63 |
|
digit_yolov8_best_float16.tflite
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8dea8a7f16ed9569b0fbc527b04026fb3c06683ab81f51d1365ed1ae578a41e4
|
| 3 |
+
size 6195448
|
models.py
CHANGED
|
@@ -85,9 +85,9 @@ def get_digit(model_path, image_path, threshold=0.5):
|
|
| 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(
|
| 91 |
image_np = np.true_divide(image_np, 255, dtype=np.float32)
|
| 92 |
image_np = image_np[np.newaxis, :]
|
| 93 |
|
|
@@ -101,25 +101,58 @@ def get_digit(model_path, image_path, threshold=0.5):
|
|
| 101 |
output = output.T
|
| 102 |
|
| 103 |
boxes_xywh = output[:, :4] #Get coordinates of bounding box, first 4 columns of output tensor
|
| 104 |
-
scores =
|
| 105 |
-
classes = np.argmax(output[:,
|
| 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 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 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) #
|
| 91 |
image_np = np.true_divide(image_np, 255, dtype=np.float32)
|
| 92 |
image_np = image_np[np.newaxis, :]
|
| 93 |
|
|
|
|
| 101 |
output = output.T
|
| 102 |
|
| 103 |
boxes_xywh = output[:, :4] #Get coordinates of bounding box, first 4 columns of output tensor
|
| 104 |
+
scores = np.max(output[:, 4:], axis=1) #Get score value, 5th column of output tensor
|
| 105 |
+
classes = np.argmax(output[:, 4:], 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 |
+
prob_threshold = threshold
|
| 110 |
+
|
| 111 |
for box, score, cls in zip(boxes_xywh, scores, classes):
|
| 112 |
+
|
| 113 |
+
if score < prob_threshold:
|
| 114 |
+
continue
|
| 115 |
+
|
| 116 |
x_center, y_center, width, height = box
|
| 117 |
x1 = int((x_center - width / 2) * image_width)
|
| 118 |
y1 = int((y_center - height / 2) * image_height)
|
| 119 |
x2 = int((x_center + width / 2) * image_width)
|
| 120 |
y2 = int((y_center + height / 2) * image_height)
|
|
|
|
| 121 |
|
| 122 |
+
pred_list.append((x1, x2, cls, score))
|
| 123 |
+
|
| 124 |
+
pred_list = sorted(pred_list, key=lambda x: x[0])
|
| 125 |
+
|
| 126 |
+
num_list = []
|
| 127 |
+
|
| 128 |
+
temp_pred_list =[]
|
| 129 |
+
|
| 130 |
+
x_prev = 0
|
| 131 |
+
|
| 132 |
+
x_diff = min([elem[1] - elem[0] for elem in pred_list]) - 10
|
| 133 |
+
|
| 134 |
+
for idx, pred in enumerate(pred_list):
|
| 135 |
+
|
| 136 |
+
if idx == 0:
|
| 137 |
+
temp_pred_list.append(pred)
|
| 138 |
+
x_prev = pred[0]
|
| 139 |
+
elif idx == len(pred_list) - 1:
|
| 140 |
+
temp_final_num = sorted(temp_pred_list, key=lambda x: x[-1], reverse=True)[0]
|
| 141 |
+
num_list.append(temp_final_num)
|
| 142 |
+
elif pred[0] - x_prev < x_diff:
|
| 143 |
+
temp_pred_list.append(pred)
|
| 144 |
+
x_prev = pred[0]
|
| 145 |
+
else:
|
| 146 |
+
temp_final_num = sorted(temp_pred_list, key=lambda x: x[-1], reverse=True)[0]
|
| 147 |
+
num_list.append(temp_final_num)
|
| 148 |
+
temp_pred_list = []
|
| 149 |
+
x_prev = pred[0]
|
| 150 |
+
temp_pred_list.append(pred)
|
| 151 |
+
|
| 152 |
+
sorted_number_list = sorted(num_list, key=lambda x: x[0])
|
| 153 |
# sorted_number_list = sorted(sorted_number_list, reverse=True, key= lambda x: x[-1])
|
| 154 |
# output_digit = float(''.join([str(int(i[2])) if i[2]!=10 else '.' for i in sorted_number_list]))
|
| 155 |
+
output_digit = float(''.join([str(int(i[2])) if i[2]!=10 else '.' for i in sorted_number_list]))
|
| 156 |
# output_digit = ''.join([str(int(i[2])) if i[2]!=10 else '.' for i in sorted_number_list[:10]])
|
| 157 |
|
| 158 |
return output_digit
|