Load Dataset

To clear all record and load all images to the /dataset. svg_w=960, svg_h=540


In [7]:
import django
from django.conf import settings
django.setup()
file_type = 'PNG'

In [8]:
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)))


[range(0, 5), range(5, 10)]

In [9]:
#FOR DEBUG ONLY !!!!
# Clear all batches and move images from /dataset to /raw
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), ('*.'+file_type))
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)


DELETE ALL RECORDS!!
moving to: /Users/naii/Desktop/project/visionmarker/static/raw/41d1fe5c-f9c6-4bc0-80e8-dea24b5b1b17.PNG

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), ('*.'+file_type))
for chunk in getbatchlist(raw_files):
    b=Batch()
    b.save()
    for i in chunk:        
        j=str(uuid4())+('.'+file_type)
        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)


batch: BID000007,src: 41d1fe5c-f9c6-4bc0-80e8-dea24b5b1b17.PNG, dst: 2aac31f9-a848-47db-8882-886ef1283b91.PNG

In [ ]: