Update utils.py
Browse files
utils.py
CHANGED
|
@@ -18,40 +18,40 @@ def predict_image(image, conf_threshold):
|
|
| 18 |
input_image, resized_image = preprocess_data(image, input_layer)
|
| 19 |
output_key = compiled_model.output("boxes")
|
| 20 |
boxes = compiled_model([input_image])[output_key]
|
| 21 |
-
#
|
| 22 |
boxes = boxes[~np.all(boxes == 0, axis=1)]
|
| 23 |
return boxes, resized_image
|
| 24 |
|
| 25 |
def convert_result_to_image(bgr_image, resized_image, boxes, threshold=0.3, conf_labels=True):
|
| 26 |
-
#
|
| 27 |
colors = {"red": (255, 0, 0), "green": (0, 255, 0)}
|
| 28 |
-
#
|
| 29 |
(real_y, real_x), (resized_y, resized_x) = (
|
| 30 |
bgr_image.shape[:2],
|
| 31 |
resized_image.shape[:2],
|
| 32 |
)
|
| 33 |
ratio_x, ratio_y = real_x / resized_x, real_y / resized_y
|
| 34 |
|
| 35 |
-
#
|
| 36 |
rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
for box in boxes:
|
| 40 |
-
#
|
| 41 |
conf = box[-1]
|
| 42 |
if conf > threshold:
|
| 43 |
-
#
|
| 44 |
-
#
|
| 45 |
-
#
|
| 46 |
(x_min, y_min, x_max, y_max) = [
|
| 47 |
(int(max(corner_position * ratio_y, 10)) if idx % 2 else int(corner_position * ratio_x)) for idx, corner_position in enumerate(box[:-1])
|
| 48 |
]
|
| 49 |
|
| 50 |
-
#
|
| 51 |
rgb_image = cv2.rectangle(rgb_image, (x_min, y_min), (x_max, y_max), colors["green"], 3)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
#
|
| 55 |
if conf_labels:
|
| 56 |
rgb_image = cv2.putText(
|
| 57 |
rgb_image,
|
|
|
|
| 18 |
input_image, resized_image = preprocess_data(image, input_layer)
|
| 19 |
output_key = compiled_model.output("boxes")
|
| 20 |
boxes = compiled_model([input_image])[output_key]
|
| 21 |
+
#0์ผ๋ก๋ง ๊ตฌ์ฑ๋ ์์ ์ ๊ฑฐ
|
| 22 |
boxes = boxes[~np.all(boxes == 0, axis=1)]
|
| 23 |
return boxes, resized_image
|
| 24 |
|
| 25 |
def convert_result_to_image(bgr_image, resized_image, boxes, threshold=0.3, conf_labels=True):
|
| 26 |
+
#๋ฐ์ด๋ฉ ๋ฐ์ค์ ๋ผ๋ฒจ์ ๋ํ ์์ ์ ์
|
| 27 |
colors = {"red": (255, 0, 0), "green": (0, 255, 0)}
|
| 28 |
+
#๋น์จ์ ๊ณ์ฐํ๊ธฐ ์ํด ์ด๋ฏธ์ง shape ๊ฐ์ ธ์ค๊ธฐ
|
| 29 |
(real_y, real_x), (resized_y, resized_x) = (
|
| 30 |
bgr_image.shape[:2],
|
| 31 |
resized_image.shape[:2],
|
| 32 |
)
|
| 33 |
ratio_x, ratio_y = real_x / resized_x, real_y / resized_y
|
| 34 |
|
| 35 |
+
#๊ธฐ๋ณธ ์ด๋ฏธ์ง๋ฅผ BGR์์ RGB ํ์์ผ๋ก ๋ณํ
|
| 36 |
rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
|
| 37 |
|
| 38 |
+
#0์ด ์๋ ์์ ๋ฐ๋ณต
|
| 39 |
for box in boxes:
|
| 40 |
+
#๋ฐฐ์ด์ ๋ง์ง๋ง ์์น์์ ์ ๋ขฐ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
|
| 41 |
conf = box[-1]
|
| 42 |
if conf > threshold:
|
| 43 |
+
#float๋ฅผ int๋ก ๋ณํํ๊ณ ๊ฐ ์์์ ๋ชจ์๋ฆฌ ์์น๋ฅผ x, y ๋น์จ๋ก ๊ณฑํ๊ธฐ
|
| 44 |
+
#์ด๋ฏธ์ง ์๋จ์ ๋ฐ์ด๋ฉ ๋ฐ๊ฐ ์๋ ๊ฒฝ์ฐ
|
| 45 |
+
#์์ชฝ ์์ ๋ง๋๋ฅผ ์กฐ๊ธ ์๋๋ก ์์น์์ผ ์ด๋ฏธ์ง์์ ๋ณด์ด๋๋ก ์์น ์ํค๊ธฐ.
|
| 46 |
(x_min, y_min, x_max, y_max) = [
|
| 47 |
(int(max(corner_position * ratio_y, 10)) if idx % 2 else int(corner_position * ratio_x)) for idx, corner_position in enumerate(box[:-1])
|
| 48 |
]
|
| 49 |
|
| 50 |
+
#์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ์ด๋ฉ ๋ฐ์ค ๊ทธ๋ฆฌ๊ธฐ. ์ฌ๊ฐํ ํจ์์ ๋งค๊ฐ๋ณ์๋ ์ด๋ฏธ์ง, ์์์ , ๋์ , ์์, ๋๊ป
|
| 51 |
rgb_image = cv2.rectangle(rgb_image, (x_min, y_min), (x_max, y_max), colors["green"], 3)
|
| 52 |
|
| 53 |
+
#์์น์ ์ ๋ขฐ๋์ ๋ฐ๋ผ ์ด๋ฏธ์ง์ ํ
์คํธ๋ฅผ ์ถ๊ฐํ๊ธฐ
|
| 54 |
+
#ํ
์คํธ ํจ์์ ๋งค๊ฐ๋ณ์: ์ด๋ฏธ์ง, ํ
์คํธ, ์ผ์ชฝ ํ๋จ ๋ชจ์๋ฆฌ ํ
์คํธ ํ๋, ๊ธ๊ผด, ๊ธ๊ผด ํฌ๊ธฐ, ์์, ๋๊ป, ์ ์ข
๋ฅ.
|
| 55 |
if conf_labels:
|
| 56 |
rgb_image = cv2.putText(
|
| 57 |
rgb_image,
|