#!/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
张小明
前端开发工程师
2025常见平板定向天线模组技术方案与应用指南解析
平板定向天线凭借其高增益、窄波束宽度、抗干扰性强等优势,在无线通信、物联网(IoT)、智能交通、工业自动化和5G网络中得到了广泛应用。它在远距离通信和高密度信号应用场景中尤为重要。近年来,随着5G网络覆盖扩展、工业物联网需求…
英语单词发音MP3下载终极指南:119,376个单词发音一键获取
英语单词发音MP3下载终极指南:119,376个单词发音一键获取 【免费下载链接】English-words-pronunciation-mp3-audio-download Download the pronunciation mp3 audio for 119,376 unique English words/terms 项目地址: https://gitcode.com/gh_mirrors/en/Englis…
STM32 CANopen协议栈实战指南:从零构建工业级嵌入式通信系统
STM32 CANopen协议栈实战指南:从零构建工业级嵌入式通信系统 【免费下载链接】CanOpenSTM32 CANopenNode on STM32 microcontrollers. 项目地址: https://gitcode.com/gh_mirrors/ca/CanOpenSTM32 你是不是正在为STM32项目寻找一个稳定可靠的CANopen通信解决…
【MCP AI-102文档避坑指南】:资深架构师亲授10年经验总结
第一章:MCP AI-102认证概述MCP AI-102认证是微软推出的一项专业级人工智能技术认证,全称为“Designing and Implementing a Microsoft Azure AI Solution”。该认证面向希望在Azure平台上设计、部署和管理AI解决方案的开发人员与架构师,验证其…
Navigation的三种方式之动态路由(自定义路由表动态import)
参考链接 Navigationhvigor插件:https://gitee.com/zhongrui_developer/AppRouterProject/tree/master#%E7%AE%80%E4%BB%8B https://gitcode.com/HarmonyOS-Cases/cases/tree/master/test/performance/dynamicRouter/ 系统路由表实现页面跳转: RouterMap注册转为…
Syncthing-Android 文件同步神器:跨设备数据管理终极指南
Syncthing-Android 文件同步神器:跨设备数据管理终极指南 【免费下载链接】syncthing-android Wrapper of syncthing for Android. 项目地址: https://gitcode.com/gh_mirrors/sy/syncthing-android Syncthing-Android 是一款专为 Android 设备设计的开源文件…