File size: 2,962 Bytes
c6f109b 4592a37 c6f109b f1a2fac c6f109b 3ded7ef c6f109b 3ded7ef 324b868 c6f109b 324b868 c6f109b 324b868 c6f109b 324b868 c6f109b 324b868 c6f109b 324b868 c6f109b 324b868 c6f109b 324b868 c6f109b 324b868 c6f109b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import openvino as ov
import cv2
import numpy as np
core = ov.Core()
model = core.read_model(model='models/horizontal-text-detection-0001.xml')
compiled_model = core.compile_model(model = model, device_name="CPU")
input_layer = compiled_model.input(0)
output_layer = compiled_model.output(0)
def preprocess_data(image, input_layer):
N, C, H, W = input_layer.shape
resized_image = cv2.resize(image, (W, H))
input_image = np.expand_dims(resized_image.transpose(2, 0, 1), 0)
return input_image, resized_image
def predict_image(image, conf_threshold):
input_image, resized_image = preprocess_data(image, input_layer)
output_key = compiled_model.output("boxes")
boxes = compiled_model([input_image])[output_key]
#0์ผ๋ก๋ง ๊ตฌ์ฑ๋ ์์ ์ ๊ฑฐ
boxes = boxes[~np.all(boxes == 0, axis=1)]
return boxes, resized_image
def convert_result_to_image(bgr_image, resized_image, boxes, threshold=0.3, conf_labels=True):
#๋ฐ์ด๋ฉ ๋ฐ์ค์ ๋ผ๋ฒจ์ ๋ํ ์์ ์ ์
colors = {"red": (255, 0, 0), "green": (0, 255, 0)}
#๋น์จ์ ๊ณ์ฐํ๊ธฐ ์ํด ์ด๋ฏธ์ง shape ๊ฐ์ ธ์ค๊ธฐ
(real_y, real_x), (resized_y, resized_x) = (
bgr_image.shape[:2],
resized_image.shape[:2],
)
ratio_x, ratio_y = real_x / resized_x, real_y / resized_y
#๊ธฐ๋ณธ ์ด๋ฏธ์ง๋ฅผ BGR์์ RGB ํ์์ผ๋ก ๋ณํ
rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
#0์ด ์๋ ์์ ๋ฐ๋ณต
for box in boxes:
#๋ฐฐ์ด์ ๋ง์ง๋ง ์์น์์ ์ ๋ขฐ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
conf = box[-1]
if conf > threshold:
#float๋ฅผ int๋ก ๋ณํํ๊ณ ๊ฐ ์์์ ๋ชจ์๋ฆฌ ์์น๋ฅผ x, y ๋น์จ๋ก ๊ณฑํ๊ธฐ
#์ด๋ฏธ์ง ์๋จ์ ๋ฐ์ด๋ฉ ๋ฐ๊ฐ ์๋ ๊ฒฝ์ฐ
#์์ชฝ ์์ ๋ง๋๋ฅผ ์กฐ๊ธ ์๋๋ก ์์น์์ผ ์ด๋ฏธ์ง์์ ๋ณด์ด๋๋ก ์์น ์ํค๊ธฐ.
(x_min, y_min, x_max, y_max) = [
(int(max(corner_position * ratio_y, 10)) if idx % 2 else int(corner_position * ratio_x)) for idx, corner_position in enumerate(box[:-1])
]
#์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ์ด๋ฉ ๋ฐ์ค ๊ทธ๋ฆฌ๊ธฐ. ์ฌ๊ฐํ ํจ์์ ๋งค๊ฐ๋ณ์๋ ์ด๋ฏธ์ง, ์์์ , ๋์ , ์์, ๋๊ป
rgb_image = cv2.rectangle(rgb_image, (x_min, y_min), (x_max, y_max), colors["green"], 3)
#์์น์ ์ ๋ขฐ๋์ ๋ฐ๋ผ ์ด๋ฏธ์ง์ ํ
์คํธ๋ฅผ ์ถ๊ฐํ๊ธฐ
#ํ
์คํธ ํจ์์ ๋งค๊ฐ๋ณ์: ์ด๋ฏธ์ง, ํ
์คํธ, ์ผ์ชฝ ํ๋จ ๋ชจ์๋ฆฌ ํ
์คํธ ํ๋, ๊ธ๊ผด, ๊ธ๊ผด ํฌ๊ธฐ, ์์, ๋๊ป, ์ ์ข
๋ฅ.
if conf_labels:
rgb_image = cv2.putText(
rgb_image,
f"{conf:.2f}",
(x_min, y_min - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
colors["red"],
1,
cv2.LINE_AA,
)
return rgb_image |