Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openvino as ov
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
core = ov.Core()
|
| 7 |
+
model = core.read_model(model='./model/horizontal-text-detection-0001.xml')
|
| 8 |
+
compiled_model = core.compile_model(model = model, device_name="CPU")
|
| 9 |
+
input_layer = compiled_model.input(0)
|
| 10 |
+
output_layer = compiled_model.output("boxes")
|
| 11 |
+
|
| 12 |
+
def preprocess(image, input_layer):
|
| 13 |
+
N, C, H, W = input_layer.shape
|
| 14 |
+
resized_image = cv2.resize(image, (W, H))
|
| 15 |
+
input_image = np.expand_dims(resized_image.transpose(2, 0, 1), 0)
|
| 16 |
+
return input_image, resized_image
|
| 17 |
+
|
| 18 |
+
def predict_image(image, conf_threshold):
|
| 19 |
+
input_image, resized_image = preprocess(image, input_layer)
|
| 20 |
+
boxes = compiled_model([input_image])[output_layer]
|
| 21 |
+
boxes = boxes[~np.all(boxes == 0, axis=1)]
|
| 22 |
+
return boxes, resized_image
|
| 23 |
+
|
| 24 |
+
def convert_result_to_image(bgr_image, resized_image, boxes, threshold=0.3, conf_labels=True):
|
| 25 |
+
# Define colors for boxes and descriptions.
|
| 26 |
+
colors = {"red": (255, 0, 0), "green": (0, 255, 0)}
|
| 27 |
+
# Fetch the image shapes to calculate a ratio.
|
| 28 |
+
(real_y, real_x), (resized_y, resized_x) = (
|
| 29 |
+
bgr_image.shape[:2],
|
| 30 |
+
resized_image.shape[:2],
|
| 31 |
+
)
|
| 32 |
+
ratio_x, ratio_y = real_x / resized_x, real_y / resized_y
|
| 33 |
+
|
| 34 |
+
# Convert the base image from BGR to RGB format.
|
| 35 |
+
rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
|
| 36 |
+
|
| 37 |
+
# Iterate through non-zero boxes.
|
| 38 |
+
for box in boxes:
|
| 39 |
+
# Pick a confidence factor from the last place in an array.
|
| 40 |
+
conf = box[-1]
|
| 41 |
+
if conf > threshold:
|
| 42 |
+
# Convert float to int and multiply corner position of each box by x and y ratio.
|
| 43 |
+
# If the bounding box is found at the top of the image,
|
| 44 |
+
# position the upper box bar little lower to make it visible on the image.
|
| 45 |
+
(x_min, y_min, x_max, y_max) = [
|
| 46 |
+
(int(max(corner_position * ratio_y, 10)) if idx % 2 else int(corner_position * ratio_x)) for idx, corner_position in enumerate(box[:-1])
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
# Draw a box based on the position, parameters in rectangle function are: image, start_point, end_point, color, thickness.
|
| 50 |
+
rgb_image = cv2.rectangle(rgb_image, (x_min, y_min), (x_max, y_max), colors["green"], 3)
|
| 51 |
+
|
| 52 |
+
# Add text to the image based on position and confidence.
|
| 53 |
+
# Parameters in text function are: image, text, bottom-left_corner_textfield, font, font_scale, color, thickness, line_type.
|
| 54 |
+
if conf_labels:
|
| 55 |
+
rgb_image = cv2.putText(
|
| 56 |
+
rgb_image,
|
| 57 |
+
f"{conf:.2f}",
|
| 58 |
+
(x_min, y_min - 10),
|
| 59 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 60 |
+
0.8,
|
| 61 |
+
colors["red"],
|
| 62 |
+
1,
|
| 63 |
+
cv2.LINE_AA,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
return rgb_image
|