#!/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
张小明
前端开发工程师
n8n自动化测试深度解析:5个关键策略构建企业级测试体系
n8n自动化测试深度解析:5个关键策略构建企业级测试体系 【免费下载链接】n8n n8n 是一个工作流自动化平台,它结合了代码的灵活性和无代码的高效性。支持 400 集成、原生 AI 功能以及公平开源许可,n8n 能让你在完全掌控数据和部署的前提下&…
Rename It终极指南:高效设计文件管理完整教程
Rename It终极指南:高效设计文件管理完整教程 【免费下载链接】RenameIt Keep your Sketch files organized, batch rename layers and artboards. 项目地址: https://gitcode.com/gh_mirrors/re/RenameIt 在当今快节奏的设计工作中,高效的设计文…
SVNAdmin2:革命性图形化SVN权限管理解决方案
SVNAdmin2:革命性图形化SVN权限管理解决方案 【免费下载链接】SvnAdminV2.0 基于web的SVN管理系统,支持HTTP协议、SVN协议、支持LDAP认证、Docker部署 项目地址: https://gitcode.com/gh_mirrors/sv/SvnAdminV2.0 传统SVN权限管理正面临着严峻的效…
MCP AI-102模型测试的秘密武器:自动化测试平台搭建与应用案例分享
第一章:MCP AI-102模型测试的核心挑战在对MCP AI-102模型进行测试时,面临多重技术与工程层面的挑战。这些挑战不仅影响测试结果的准确性,也对部署效率和系统稳定性构成潜在威胁。数据质量与多样性不足 高质量、多样化的测试数据是验证模型泛化…
为什么你的AI总是“呆若木鸡”?:重塑游戏Agent实时决策能力的7个关键步骤
第一章:游戏 AI Agent 的行为决策在现代电子游戏中,AI Agent 的行为决策机制是决定其智能程度与交互真实感的核心。一个优秀的游戏 AI 不仅需要感知环境变化,还需基于当前状态选择最优动作,从而实现类人甚至超越人类玩家的策略表现…
Windows平台安卓应用安装新纪元:APK Installer打造移动办公新体验
Windows平台安卓应用安装新纪元:APK Installer打造移动办公新体验 【免费下载链接】APK-Installer An Android Application Installer for Windows 项目地址: https://gitcode.com/GitHub_Trending/ap/APK-Installer 在数字化办公日益普及的今天,…