opencv篇---目标检测得到的目标框快速绘制(配置文件批量)

opencv篇—目标检测得到的目标框快速绘制(配置文件批量)

import cv2
import colorsys


def show_pic(image, result_dict):
    save_img = image.split("/")[-1]
    print(save_img)
    img = cv2.imread(image)

    num_classes = 20
    hsv_tuples = [(x / num_classes, 1., 1.) for x in range(num_classes)]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))

    cls_li = []
    for i in range(len(result_dict["cls"])):
        cls, x, y, w, h = result_dict["cls"][i], result_dict["x"][i], result_dict["y"][i], result_dict["w"][i], \
                          result_dict["h"][i]
        left_top = (x, y)
        right_bottom = (x + w, y + h)

        cv2.rectangle(img, left_top, right_bottom, color=colors[cls], thickness=3)
        cv2.putText(img, 'key', (0, 0), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 0, 0))

    cv2.imwrite(save_img, img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()


if __name__ == '__main__':
    file_object1 = open("2022-11-11.txt", "r")
    try:
      while True:
          line = file_object1.readline()
          if line:
              print("line=",line)
              show_pic(eval(line)["url"], eval(line)["result"])
          else:
              break
    finally:
        file_object1.close()

2022-11-11.txt

{"aitype": "litter", "cameraID": "", "nodeName": "58", "processdate": "2022-11-11 15:56:27", "ptype": "localfile", "recorddate": "2022-11-11 15:56:27", "result": {"cls": [1], "h": [68], "message": "ok", "pro": [0.96], "w": [110], "x": [842], "y": [174]}, "url": "/app/yyq/dataset/first_project/yichuan/pro_result/2022-11-11-15-56-27_0372.jpg"}
{"aitype": "litter", "cameraID": "", "nodeName": "58", "processdate": "2022-11-11 15:58:52", "ptype": "localfile", "recorddate": "2022-11-11 15:58:52", "result": {"cls": [1], "h": [63], "message": "ok", "pro": [0.552], "w": [52], "x": [812], "y": [204]}, "url": "/app/yyq/dataset/first_project/yichuan/pro_result/2022-11-11-15-58-52_0248.jpg"}

opencv篇---目标检测得到的目标框快速绘制(配置文件批量)_第1张图片

你可能感兴趣的:(opencv,计算机视觉,python)