当前位置: 首页 > news >正文

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

文章目录

  • 背景
  • 代码

背景

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

代码

from tqdm import tqdm
import os, json, cv2, copy
import numpy as npdef 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, filenamesdef 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, labelsdef plot_one_ori(image, boxs, label, color, mask_alpha=0.4):[x1, y1, x2, y2] = boxsmask_img = copy.deepcopy(image) # 1ori_img = copy.deepcopy(mask_img) # 1cropped_image = ori_img[y1:y2, x1:x2]img_height, img_width = image.shape[:2]size = min([img_height, img_width]) * 0.0006text_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_imagedef cv_imread(filePath):cv_img = cv2.imdecode(np.fromfile(filePath, dtype=np.uint8), flags=cv2.IMREAD_COLOR)return cv_imgif __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)
http://www.lryc.cn/news/362299.html

相关文章:

  • LabVIEW在高校电力电子实验中的应用
  • rtsp python实现
  • RHCE (Linux进阶) Ubuntu 操作系统安装教程
  • vue-router 源码分析——1. 路由匹配
  • 百度云下载不限速方式集合
  • 2024年6月1日 (周六) 叶子游戏新闻
  • MathorCup挑战赛获奖名单公示,第九届研讨会及颁奖典礼即将举行
  • vulnhub靶机xptosystem
  • Spring Boot详解:深入了解与实践
  • FreeRtos进阶——中断的内部逻辑
  • Centos7对比Ubuntu一些常用操作差异点
  • 24、matlab二维和三维网格(meshgrid函数)以及散点数据插值 griddata()函数
  • Codeforces Round 950 (Div. 3)
  • 弘君资本炒股开户:如何看待股价波动?
  • 操作系统实验六 文件系统验证实验
  • python中的循环控制语句break与continue
  • C语言笔记23 •文件操作•
  • 新项目来了,JDK 17和JDK 21 该如何选择?
  • 友顺科技(UTC)分立器件与集成IC产品选型和应用
  • 并发与多线程:创建多个线程,数据共享
  • 【机器学习数据挖掘】基于ARIMA 自回归积分滑动平均模型的销售价格库存分析报告 附完整python代码
  • LightGBM 进行回归建模的流程
  • 【Linux】多进程基础--信号
  • 若依项目部署(Linux2.0)
  • python小练习03
  • 【机器学习】机器学习在深度学习领域中的作用:半监督学习的视角
  • C#WPF数字大屏项目实战01--开发环境与项目创建
  • python中字符串的各类方法
  • DataGrip 数据库连接客户端
  • JS片段:生成 UUID