In [3]:
import django
from django.conf import settings
django.setup()
In [4]:
from app.models import Label,Image,Batch, Comment, STATUS_CHOICES
from django.contrib.auth.models import User
import os, fnmatch, uuid, shutil
from uuid import uuid4
def getbatchlist(filelist):
def chunks(li, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(li), n):
yield li[i:i + n]
return list(chunks(filelist, 5))
print(getbatchlist(range(10)))
print "DELETE ALL RECORDS!!" q=Batch.objects.all().delete() static_path = settings.STATICFILES_DIRS[0] raw_path = os.path.join(static_path,'raw') dataset_path = os.path.join(static_path,'dataset') raw_files = fnmatch.filter(os.listdir(dataset_path), '*.jpg') for i in raw_files: _dst=os.path.join(raw_path, i ) _src=os.path.join(dataset_path,i) print "moving to: %s"%(_dst) shutil.move(src=_src, dst=_dst)
In [11]:
# moving from /raw/i to /dataset/j
static_path = settings.STATICFILES_DIRS[0]
raw_path = os.path.join(static_path,'raw')
dataset_path = os.path.join(static_path,'dataset')
raw_files = fnmatch.filter(os.listdir(raw_path), '*.PNG')
for chunk in getbatchlist(raw_files):
b=Batch()
b.save()
for i in chunk:
j=str(uuid4())+'.PNG'
print(("batch: %s,src: %s, dst: %s")%(b,i,j))
Image(batch=b, src_path=j, raw_path=i).save()
_dst=os.path.join(dataset_path,j)
_src=os.path.join(raw_path,i)
shutil.move(src=_src, dst=_dst)
In [ ]: