def test_img_add():
normal_dir = '/newnfs/dataset/OCR/parking_number_only/test_ocr/20231113_OCR_OD-L_20231116/result_file/default/002977.json'
normal_dir2 = '/newnfs/dataset/OCR/parking_number_only/test_ocr/20231009_OCR_1-L_20231011/result_file/default/002176.json'
img_dir = normal_dir.replace("result_file", "raw_file").replace(".json", ".jpg")
img_dir2 = normal_dir2.replace("result_file", "raw_file").replace(".json", ".jpg")
normal_img = cv.imread(img_dir)
normal_img2 = cv.imread(img_dir2)
if normal_img2 is None:
print('normal_img2 is None')
return
if normal_img is None:
print('normal_img is None')
return
normal_dir_json2 = load_json(normal_dir2)
# 解析标签,获得box和text
objs = parse_json(normal_dir_json2)
new_img_dir = '/tmpnfs/cxy/mmocr/tools/data_augment_zww/add_res.jpg'
imgh, imgw, _ = normal_img.shape
for i, obj in enumerate(objs):
type_str = obj[4]
box_str = get_obj_str(obj)
img_cropped = np.ones((imgh, imgw, 3))
img_cropped = normal_img2[obj[1]:obj[3],obj[0]:obj[2]]
normal_img[obj[1]:obj[3],obj[0]:obj[2]] = img_cropped
cv.imwrite(new_img_dir, normal_img)
心得:
roi区域一定先预定一个和要贴到的图像一样大小的图像
img_cropped = np.ones((imgh, imgw, 3))
img_cropped = normal_img2[obj[1]:obj[3],obj[0]:obj[2]]
normal_img[obj[1]:obj[3],obj[0]:obj[2]] = img_cropped
obj[1]:obj[3],obj[0]:obj[2] 为roi区域左上点和右下角点的坐标值。