#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 一键生成“纯背景”COCO格式json(annotations为空) """ import os import json import glob from PIL import Image from datetime import datetime def generate_empty_coco_json(image_folder, output_json_path, start_image_id=500000): """ 将文件夹内所有图片写成COCO格式(无标注) :param image_folder: 纯背景图片目录 :param output_json_path: 输出json路径 :param start_image_id: 起始image_id,避免冲突 """ exts = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.tif", "*.tiff"] image_files = [] for ext in exts: image_files.extend(glob.glob(os.path.join(image_folder, ext))) image_files.extend(glob.glob(os.path.join(image_folder, ext.upper()))) image_files = sorted(list(set(image_files))) if not image_files: print(f"× 在{image_folder}下未找到图片,请检查路径。") return print(f"√ 找到{len(image_files)}张背景图片,开始处理...") coco_output = { "info": { "description": "Empty Background Images for Negative Mining", "url": "", "version": "1.0", "year": datetime.now().year, "contributor": "User", "date_created": datetime.now().strftime("%Y-%m-%d %H:%M:%S") }, "licenses": [], "images": [], "annotations": [], # 关键:空列表 "categories": [] # 可留空,合并时以主文件为准 } for idx, img_path in enumerate(image_files): try: with Image.open(img_path) as img: width, height = img.size file_name = os.path.basename(img_path) image_info = { "id": start_image_id + idx, "file_name": file_name, "width": width, "height": height, "date_captured": datetime.now().strftime("%Y-%m-%d %H:%M:%S") } coco_output["images"].append(image_info) if (idx + 1) % 100 == 0: print(f"√ 已处理{idx + 1}张图片...") except Exception as e: print(f"× 无法读取图片{img_path}:{e}") os.makedirs(os.path.dirname(output_json_path), exist_ok=True) with open(output_json_path, 'w', encoding='utf-8') as f: json.dump(coco_output, f, indent=4, ensure_ascii=False) print(f"成功生成JSON文件:{output_json_path}") print(f"共包含{len(coco_output['images'])}张图片,0个标注。") if __name__ == "__main__": # ===== 按需改这两行 ===== IMAGE_DIR = r"C:\Users\11726\Desktop\pla\ao-90" # 纯背景图文件夹 OUTPUT_JSON = r"C:\Users\11726\Desktop\pla/empty_samples.json" START_ID = 500000 # 起始image_id # ======================= generate_empty_coco_json(IMAGE_DIR, OUTPUT_JSON, START_ID)空白数据生成coco
张小明
前端开发工程师
如何快速掌握vue-plugin-hiprint:Vue项目打印设计的终极解决方案
如何快速掌握vue-plugin-hiprint:Vue项目打印设计的终极解决方案 【免费下载链接】vue-plugin-hiprint hiprint for Vue2/Vue3 ⚡打印、打印设计、可视化设计器、报表设计、元素编辑、可视化打印编辑 项目地址: https://gitcode.com/gh_mirrors/vu/vue-plugin-hip…
ReadCat开源小说阅读器:打造沉浸式数字阅读新体验
还在为商业阅读软件的广告困扰吗?想要一款真正纯净、功能强大的本地小说阅读器吗?ReadCat开源小说阅读器正是为你量身打造的完美解决方案!这款基于Vue3和Electron技术构建的跨平台工具,为技术爱好者和阅读用户带来了前所未有的阅读…
物流智能调度进阶之路(量子Agent赋能路径优化实战)
第一章:物流智能调度进阶之路(量子Agent赋能路径优化实战)在现代物流系统中,路径优化长期面临组合爆炸难题,传统算法在大规模节点场景下计算效率受限。随着量子计算与多智能体系统(Multi-Agent System, MAS…
【医疗多模态Agent权重优化】:揭秘高效模型融合背后的黑科技
第一章:医疗多模态Agent权重优化的背景与意义在现代智慧医疗系统中,多模态Agent通过整合医学影像、电子病历、基因组数据和实时生理信号等多种异构信息源,显著提升了疾病诊断、治疗建议与患者管理的智能化水平。然而,不同模态数据…
SD Maid SE安卓版(安卓系统清理器)
SD Maid是一款Android设备的系统清理工具,它能帮助用户清理手机中的不需要的文件和数据,提升设备的性能并节省存储空间。 软件功能 垃圾清理:SD Maid能够扫描并删除设备中的垃圾文件、临时文件和无用的应用程序数据,包括缓存文件…
Zoner Photo Studio X(照片编辑管理)
Zoner Photo Studio X是一款功能丰富的照片编辑和管理软件。它提供了许多强大的工具和功能,可以帮助用户轻松地编辑、组织和共享照片。 软件功能 图片编辑:提供了多种编辑工具,包括剪裁、调整色彩、修复红眼、消除瑕疵等,使用户能…