Opencv实用笔记(一): 获取并绘制JSON标注文件目标区域(可单独保存目标小图)

文章目录

背景

如果我们想要根据json标注文件,获取里面的指定目标的裁剪区域,那么我们可以根据以下代码来实现(也可以校验标注情况)。

代码

python 复制代码
from tqdm import tqdm
import os, json, cv2, copy
import numpy as np

def get_all_images(path, flags):
	result_list, filenames = [], []
	for root, dirs, files in os.walk(path):
		for file in files:
			filename, file_extension = os.path.splitext(file)
			if file_extension.lower() in flags:
				result_list.append(os.path.join(root, file))
				filenames.append(file)
	return result_list, filenames

def get_labelme_info(label_file, target):
    anno = json.load(open(label_file, "r", encoding="utf-8"))
    shapes = anno['shapes']
    image_path = os.path.basename(anno['imagePath'])
    labels = []
    boxes = []
    for s in shapes:
        pts = s['points']
        x1, y1 = pts[0]
        x2, y2 = pts[1]
        label = s['label']
        if label in target:
            labels.append(label)
            boxes.append([x1, y1, x2, y2])
    return image_path, boxes, labels

def plot_one_ori(image, boxs, label, color, mask_alpha=0.4):
    [x1, y1, x2, y2] = boxs
    mask_img = copy.deepcopy(image) # 1
    ori_img = copy.deepcopy(mask_img) # 1
    cropped_image = ori_img[y1:y2, x1:x2]
    img_height, img_width = image.shape[:2]
    size = min([img_height, img_width]) * 0.0006
    text_thickness = int(min([img_height, img_width]) * 0.001)
    cv2.rectangle(image, (x1, y1), (x2, y2), color, 3)
    caption = f'{"原标签-"}{label}'
    (tw, th), _ = cv2.getTextSize(text=caption, fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                                fontScale=size, thickness=text_thickness)
    th = int(th * 1.2)
    cv2.rectangle(image, (x1, y2),
                (x1 + tw, y2 + th), color, -1)
    cv2.rectangle(mask_img, (x1, y2),
                (x1 + tw, y2 + th), color, -1)
    cv2.putText(image, caption, (x1, y2 + th), cv2.FONT_HERSHEY_SIMPLEX, size, (255, 255, 255), text_thickness, cv2.LINE_AA)
    cv2.putText(mask_img, caption, (x1, y2 + th), cv2.FONT_HERSHEY_SIMPLEX, size, (255, 255, 255), text_thickness, cv2.LINE_AA)
    image = cv2.addWeighted(mask_img, mask_alpha, image, 1 - mask_alpha, 0)
    return image, cropped_image

def cv_imread(filePath):
    cv_img = cv2.imdecode(np.fromfile(filePath, dtype=np.uint8), flags=cv2.IMREAD_COLOR)
    return cv_img

if __name__ == "__main__":
    img_folder = r"\\DSJ_NAS_90*******" # 原图和JSON文件位置
    save_target_path = r"\\DSJ_NA************" # 保存位置
    target = ["red_face"] # 获取指定目标,可传多个
    os.makedirs(save_target_path, exist_ok=True)
    img_list, filenames = get_all_images(img_folder, flags=[".jpg", ".png", ".jpeg"])
    print(filenames)
    for filename in filenames:
        fn, file_extension = os.path.splitext(filename)
        image_path = os.path.join(img_folder, filename)  # 图片名
        json_path = os.path.join(img_folder, "{}.json".format(fn))  # 标签文件名
        save_path = os.path.join(save_target_path, "{}.jpg".format(fn))
        _, ori_boxes, ori_labels = get_labelme_info(json_path, target)
        for box, label in zip(ori_boxes, ori_labels):
            x1, y1, x2, y2 = list(map(int, box))
            image = cv_imread(image_path)
            image, crop_image = plot_one_ori(image, [x1, y1, x2, y2], label, color=(0,0,255))
            # cv2.imshow("1",crop_image)
            # cv2.waitKey(1)
            cv2.imencode('.jpg', crop_image)[1].tofile(save_path)
相关推荐
GOTXX35 分钟前
基于Opencv的图像处理软件
图像处理·人工智能·深度学习·opencv·卷积神经网络
嵌入式大圣1 小时前
单片机结合OpenCV
单片机·嵌入式硬件·opencv
熙曦Sakura1 小时前
完全竞争市场
笔记
dr李四维2 小时前
iOS构建版本以及Hbuilder打iOS的ipa包全流程
前端·笔记·ios·产品运营·产品经理·xcode
只怕自己不够好3 小时前
OpenCV 图像运算全解析:加法、位运算(与、异或)在图像处理中的奇妙应用
图像处理·人工智能·opencv
华清远见IT开放实验室4 小时前
【每天学点AI】实战图像增强技术在人工智能图像处理中的应用
图像处理·人工智能·python·opencv·计算机视觉
只怕自己不够好4 小时前
《OpenCV 图像缩放、翻转与变换全攻略:从基础操作到高级应用实战》
人工智能·opencv·计算机视觉
Komorebi.py6 小时前
【Linux】-学习笔记05
linux·笔记·学习
亦枫Leonlew6 小时前
微积分复习笔记 Calculus Volume 1 - 6.5 Physical Applications
笔记·数学·微积分
安静读书10 小时前
Python解析视频FPS(帧率)、分辨率信息
python·opencv·音视频