Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion core/management/commands/importrois.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ def add_arguments(self, parser):
parser.add_argument('directory', type=str, help='directory containing images')
parser.add_argument('-c','--collection', type=str, help='image collection to create or add images to')
parser.add_argument('-u','--user', type=str, help='username for any created annotations (user must exist)')
parser.add_argument('-f', '--flag', type=str, help='indicate whether annotation is manual or auto. Defaulted to manual, if not specified')
parser.add_argument('-cl', '--classifier', type=str, help='if auto annotated, indicate associated classifier')

def handle(self, *args, **options):
# handle arguments
directory = options['directory']
collection_name = options.get('collection')
username = options.get('user')
flag = options.get('flag')
classifier = options.get('classifier') or ''
# validate arguments
if not os.path.exists(directory):
raise CommandError('specified directory does not exist')
Expand All @@ -31,6 +35,19 @@ def handle(self, *args, **options):
if collection_name is not None:
collection, created = ImageCollection.objects.get_or_create(
name=collection_name)

auto = False
# If flag not specified, auto defaulted to False
if flag:
if not(flag == "auto" or flag == "manual"):
raise CommandError('values of flag should either be auto or manual')
if flag == "auto":
auto = True
if not classifier:
raise CommandError('classifier must be specified if auto annotated')
# if flag == manual, auto already set to False


# scan directory and one level of subdirectories
def scan(dir):
result = []
Expand Down Expand Up @@ -61,7 +78,7 @@ def scan(dir):
for roi_filename in rois:
roi_path = os.path.join(directory, label_name, roi_filename)
roi = ROI.objects.create_or_update_roi(roi_path, collection=collection)
Annotation.objects.create_or_verify(roi, label, user)
Annotation.objects.create_or_verify(roi, label, user, auto, classifier)



23 changes: 23 additions & 0 deletions core/migrations/0009_annotation_auto_annotation_classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.0.5 on 2022-06-22 17:03

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0008_roi_winning_annotation'),
]

operations = [
migrations.AddField(
model_name='annotation',
name='auto',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='annotation',
name='classifier',
field=models.TextField(blank=True),
),
]
12 changes: 7 additions & 5 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,18 @@ class AnnotationManager(models.Manager):
def get_queryset(self):
return AnnotationQuerySet(self.model, using=self._db)

def create_annotation(self, roi, label, user):
return self.create(roi=roi, label=label, user=user, user_power=user.annotator.power)
def create_annotation(self, roi, label, user, auto, classifier):
return self.create(roi=roi, label=label, user=user, user_power=user.annotator.power, auto=auto, classifier=classifier)

def create_or_verify(self, roi, label, user):
def create_or_verify(self, roi, label, user, auto, classifier):
with transaction.atomic():
try:
annotation = self.get(roi=roi, label=label, user=user)
annotation = self.get(roi=roi, label=label, user=user, auto=auto, classifier=classifier)
annotation.verify()
annotation.save()
created = False
except Annotation.DoesNotExist:
annotation = self.create_annotation(roi=roi, label=label, user=user)
annotation = self.create_annotation(roi=roi, label=label, user=user, auto=auto, classifier=classifier)
created = True
roi.cache_winning_annotation()
return annotation, created
Expand All @@ -195,6 +195,8 @@ class Annotation(models.Model):
user_power = models.IntegerField(default=1)
timestamp = models.DateTimeField(auto_now_add=True)
verifications = models.IntegerField(default=0)
auto = models.BooleanField(default = False)
classifier = models.TextField(blank=True)

objects = AnnotationManager()

Expand Down