In [11]:
from app.models import Label,Image,Batch, 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, 4))
print getbatchlist(range(10))
In [14]:
#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), '*.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 [13]:
# 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), '*.jpg')
for chunk in getbatchlist(raw_files):
b=Batch()
b.save()
for i in chunk:
j=unicode(uuid4())+'.jpg'
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 [22]:
x=Batch()
x.save()
x.id
Out[22]:
In [17]:
x.id
In [18]:
x.save()
In [12]:
_labeller=MyUser.objects.get(user=User.objects.get(username='mylabeller'))
_reviwer=MyUser.objects.get(user=User.objects.get(username='admin'))
Batch(reviewer=_reviwer, labeller=_labeller).save()
In [13]:
User.objects.all()[1].username
Out[13]:
In [32]:
Batch.objects.filter(status='1').first().id
Out[32]:
In [4]:
import app.models as md
u=User.objects.get(username="admin")
mu=MyUser.objects.get(user=u)
b=Batch.objects.filter(status=md.TAGGING,labeller=mu).first()
In [10]:
if not b:
print "foo"
else:
print "bar"
In [9]:
type(b)
Out[9]:
In [ ]: