#!/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
张小明
前端开发工程师
Home Assistant Android应用连接问题终极解决方案:快速修复无法连接服务器
Home Assistant Android应用连接问题终极解决方案:快速修复无法连接服务器 【免费下载链接】android :iphone: Home Assistant Companion for Android 项目地址: https://gitcode.com/gh_mirrors/android5/android 你是否遇到过这样的情况:升级Ho…
Retrieval-Augmented Generation with Conflicting Evidence
突破RAG冲突困境:MADAM-RAG多智能体辩论框架与RAMDocs数据集解析在AI搜索、智能问答等实际场景中,检索增强生成(RAG)技术早已成为提升大模型事实性的核心方案——它让模型能调用外部知识,避免幻觉和知识过时问题。但现…
5步掌握Ultralytics YOLO终极灰度检测方案
5步掌握Ultralytics YOLO终极灰度检测方案 【免费下载链接】ultralytics ultralytics - 提供 YOLOv8 模型,用于目标检测、图像分割、姿态估计和图像分类,适合机器学习和计算机视觉领域的开发者。 项目地址: https://gitcode.com/GitHub_Trending/ul/ul…
终极指南:ML Visuals科研绘图神器全解析
终极指南:ML Visuals科研绘图神器全解析 【免费下载链接】ml-visuals 🎨 ML Visuals contains figures and templates which you can reuse and customize to improve your scientific writing. 项目地址: https://gitcode.com/gh_mirrors/ml/ml-visua…
揭秘低轨卫星Agent自适应抗干扰机制:如何在强干扰下保持99.9%通信可用性
第一章:低轨卫星Agent抗干扰机制概述在低轨卫星通信系统中,Agent作为分布式智能节点,承担着自主决策、链路优化与环境感知等关键任务。由于低轨卫星运行高度较低(通常为500–2000公里),其通信链路易受大气衰…
Mitsuba-Blender渲染插件:解决专业渲染痛点的完整方案
还在为Blender内置渲染器的物理精度不足而烦恼吗?Mitsuba-Blender插件为你提供了完美的解决方案。这款强大的集成工具将学术界公认的物理渲染器Mitsuba无缝融入Blender环境,让艺术家和研究人员都能享受到最前沿的渲染技术。🎯 【免费下载链接…