图像检测框
绘制图像检测框
代码示例
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import json
from pathlib import Path
import cv2
if __name__ == "__main__":
"""opencv 在图像上标注检测 box 和文字 detect"""
img_dir = r"F:\data\imgs"
file_name = r"F:\data\json\site_my.txt"
blue = (255, 0, 0)
mark_font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
with open(file_name, mode="r", encoding="utf-8") as file_obj:
for i, data in enumerate(file_obj):
# JSON字符串转py对象
json_str = json.loads(data)
img_nm = json_str["img_key"]
img_path = str(Path(img_dir).joinpath(img_nm))
print(img_path)
img = cv2.imread(img_path)
# 对图像进行mark
for idx, mark_point in enumerate(json_str["ccle"]):
# list中所有数值类型转换,且数据类型tuple
point = tuple(map(int, mark_point["ata"]))
category = mark_point["trs"]["ory"]
cv2.rectangle(img=img, pt1=point[0:2], pt2=point[2:4], color=blue, thickness=0)
cv2.putText(img=img, text=category, org=point[0:2], fontFace=mark_font, fontScale=0.5, color=(0, 0, 255))
cv2.imshow(winname="My_test_img", mat=img)
cv2.waitKey(delay=1000)
cv2.destroyWindow(winname="My_test_img")
output = str(Path(img_dir).joinpath("img_"+str(i)+".jpg"))
print(output)
cv2.imwrite(filename=output, img=img)
参考
python3 , opencv 在图像上标注检测 box 和文字https://blog.csdn.net/tutu96177/article/details/87783857