Skip to content

Instantly share code, notes, and snippets.

@XUJiahua
Created May 1, 2018 14:51
Show Gist options
  • Save XUJiahua/e46085cd3224efb0ea38d4818b50f5a8 to your computer and use it in GitHub Desktop.
Save XUJiahua/e46085cd3224efb0ea38d4818b50f5a8 to your computer and use it in GitHub Desktop.
有的图像数据集,图片以{class_name}_{#no}.jpg这种格式命名,目录结构扁平化。而keras训练数据结构,需要class_name作为文件夹名称。
import sys
import os
from subprocess import call
pfxs = set()
for f in os.listdir('.'):
if f.endswith('.jpg'):
pfx = f[:f.rindex('_')]
pfxs.add(pfx)
pfxs = list(pfxs)
print(pfxs)
#if len(sys.argv) != 2:
# sys.exit(2)
#pfx = sys.argv[1]
def handle_pfx(pfx):
print('[INFO] processing {}'.format(pfx))
call(["mkdir", "-p", pfx])
for f in os.listdir('.'):
if f.startswith(pfx) and f.endswith('.jpg'):
print('[INFO] mv file {} to {}'.format(f, pfx))
call(["mv", f, pfx])
for pfx in pfxs:
handle_pfx(pfx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment