import os
import json
import shutil
img_dir = 'data\sodaa\datasets'
train_ann_dir = 'data\sodaa\Annotations\\train'
val_ann_dir = 'data\sodaa\Annotations\\val'
test_ann_dir = 'data\sodaa\Annotations\\test'
output_dir = 'data\sodaa\datasets'
os. makedirs( os. path. join( output_dir, 'train' ) , exist_ok= True )
os. makedirs( os. path. join( output_dir, 'val' ) , exist_ok= True )
os. makedirs( os. path. join( output_dir, 'test' ) , exist_ok= True ) def copy_images_from_annotations ( ann_dir, output_subdir) : ann_files = os. listdir( ann_dir) for ann_file in ann_files: with open ( os. path. join( ann_dir, ann_file) , 'r' ) as f: ann_data = json. load( f) img_file = ann_data[ 'images' ] shutil. copy( os. path. join( img_dir, img_file[ 'file_name' ] ) , os. path. join( output_dir, output_subdir, img_file[ 'file_name' ] ) ) copy_images_from_annotations( train_ann_dir, 'train' )
copy_images_from_annotations( val_ann_dir, 'val' )
copy_images_from_annotations( test_ann_dir, 'test' )