import json import glob from pathlib import Path import cv2 import numpy as np def draw_box(pth_webp, pth_boxs): with open(pth_boxs, encoding='utf-8') as fp: boxs = json.load(fp) fp.close() imgData = np.fromfile(pth_webp, dtype=np.uint8) img = cv2.imdecode(imgData, cv2.IMREAD_UNCHANGED) if len(img.shape) != 3: # 转彩图 img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # DBNet 原版只能处理彩图,这里转一下 else: img_color = img.copy() for box in boxs: charPoly = box["charPoly"] lu = [charPoly["x0"], charPoly["y0"]] ru = [charPoly["x1"], charPoly["y1"]] rd = [charPoly["x2"], charPoly["y2"]] ld = [charPoly["x3"], charPoly["y3"]] points = np.array([lu, ru, rd, ld]) cv2.polylines(img_color, [points], isClosed=True, color=( # 多边形,框得比较全 100, 0, 255), thickness=2) # 只画线,不填充 # cv2.imshow("box", img_color) # cv2.waitKey(0) path_jpg = pth_webp.replace(".webp", ".jpg") cv2.imwrite(path_jpg, img_color) def do_drawbox(): webps = glob.glob('./out2/*.webp', recursive=False) for pth_webp in webps: pth_box = pth_webp.replace(".webp", ".boxs") draw_box(pth_webp, pth_box) if __name__ == "__main__": do_drawbox()