MS COCOから犬と猫の画像だけを選んで jpg ファイルを得る手順 ChatGPT Microsoft Researchの「MS COCO」データセットから犬と猫の画像を取得する手順を説明します。 ダウンロード: MS COCOの公式ウェブサイト(https://cocodataset.org/)にアクセスする。 ダウンロードセクション: ページ上部のメニューから「Download」を選択する。 イメージセクション: ダウンロードページ内で「2017 Train images [118K/18GB]」を見つけ、リンクをクリックしてダウンロードする。これにより、訓練用のカラー画像データセットがダウンロードされる。 アノテーションファイルの取得: ページ上部のメニューから「Annotations」を選択し、訓練用のアノテーションデータをダウンロードする。 Pythonスクリプトの作成: ダウンロードしたアノテーションファイルを使用して、犬と猫の画像のファイル名を取得し、それに基づいて該当する画像を選択する。 python Copy code import json import os import shutil # アノテーションファイルのパス annotation_file = "C:/Users/user/coco2017/coco/annotations/instances_val2017.json" # 画像ファイルのディレクトリパス image_directory = "C:/Users/user/coco2017/coco/val2017" # 犬と猫のカテゴリID dog_category_id = 18 cat_category_id = 17 # アノテーションファイルの読み込み with open(annotation_file, "r") as f: annotations = json.load(f) dogs = [] for annotation in annotations["annotations"]: if annotation["category_id"] in [dog_category_id]: image_id = annotation["image_id"] print(image_id) dogs.append(image_id) print(dogs) for annotation in annotations["images"]: if annotation["id"] in dogs: print(annotation["id"]) image_file_name = annotation["file_name"] print(image_file_name) image_file_path = os.path.join(image_directory, image_file_name) output_file_path = os.path.join("C:/Users/user/dog", image_file_name) shutil.copy(image_file_path, output_file_path) cats = [] for annotation in annotations["annotations"]: if annotation["category_id"] in [cat_category_id]: image_id = annotation["image_id"] print(image_id) cats.append(image_id) print(cats) for annotation in annotations["images"]: if annotation["id"] in cats: print(annotation["id"]) image_file_name = annotation["file_name"] print(image_file_name) image_file_path = os.path.join(image_directory, image_file_name) output_file_path = os.path.join("C:/Users/user/cat", image_file_name) shutil.copy(image_file_path, output_file_path) 上記のスクリプトでは、アノテーションファイルと画像ディレクトリのパスを実際の環境に合わせて置き換える必要がある。 スクリプトを実行すると、指定した出力ディレクトリに犬と猫のカラー画像が保存される。