Ejemplo n.º 1
0
def create_annotation(form, object_name, labels, frame):
    annotation = Region()
    annotation.object_name = object_name
    if form.cleaned_data['high_level']:
        annotation.full_frame = True
        annotation.x = 0
        annotation.y = 0
        annotation.h = 0
        annotation.w = 0
    else:
        annotation.full_frame = False
        annotation.x = form.cleaned_data['x']
        annotation.y = form.cleaned_data['y']
        annotation.h = form.cleaned_data['h']
        annotation.w = form.cleaned_data['w']
    annotation.text = form.cleaned_data['text']
    annotation.metadata = form.cleaned_data['metadata']
    annotation.frame = frame
    annotation.video = frame.video
    annotation.region_type = Region.ANNOTATION
    annotation.save()
    for lname in labels:
        if lname.strip():
            dl, _ = Label.objects.get_or_create(name=lname, set="UI")
            rl = RegionLabel()
            rl.video = annotation.video
            rl.frame = annotation.frame
            rl.region = annotation
            rl.label = dl
            rl.save()
Ejemplo n.º 2
0
def annotate_entire_frame(request, frame_pk):
    frame = Frame.objects.get(pk=frame_pk)
    annotation = None
    if request.method == 'POST':
        if request.POST.get('text').strip() \
                or request.POST.get('metadata').strip() \
                or request.POST.get('object_name', None):
            annotation = Region()
            annotation.region_type = Region.ANNOTATION
            annotation.x = 0
            annotation.y = 0
            annotation.h = 0
            annotation.w = 0
            annotation.full_frame = True
            annotation.text = request.POST.get('text')
            annotation.metadata = request.POST.get('metadata')
            annotation.object_name = request.POST.get('object_name',
                                                      'frame_metadata')
            annotation.frame = frame
            annotation.video = frame.video
            annotation.save()
        for label_name in request.POST.get('tags').split(','):
            if label_name.strip():
                if annotation:
                    dl = RegionLabel()
                    dl.video = frame.video
                    dl.frame = frame
                    dl.label = Label.objects.get_or_create(name=label_name,
                                                           set="UI")[0]
                    dl.region = annotation
                    dl.save()
                else:
                    dl = FrameLabel()
                    dl.video = frame.video
                    dl.frame = frame
                    dl.label = Label.objects.get_or_create(name=label_name,
                                                           set="UI")[0]
                    dl.save()
    return redirect("frame_detail", pk=frame.pk)
Ejemplo n.º 3
0
def create_yolo_test_data():
    import shutil
    import numpy as np
    import os
    from PIL import Image
    setup_django()
    from dvaapp.view_shared import handle_uploaded_file
    from django.core.files.uploadedfile import SimpleUploadedFile
    from dvaapp.models import Region,TEvent,Frame,Label,RegionLabel
    from dvaapp.tasks import perform_dataset_extraction,perform_export
    try:
        shutil.rmtree('/Users/aub3/tests/yolo_test')
    except:
        pass
    try:
        os.mkdir('/Users/aub3/tests/yolo_test')
    except:
        pass
    data = np.load('shared/underwater_data.npz')
    json_test = {}
    json_test['anchors'] = [(0.57273, 0.677385), (1.87446, 2.06253), (3.33843, 5.47434), (7.88282, 3.52778), (9.77052, 9.16828)]
    id_2_boxes = {}
    class_names = {
        0:"red_buoy",
        1:"green_buoy",
        2:"yellow_buoy",
        3:"path_marker",
        4:"start_gate",
        5:"channel"
    }
    labels = {k: Label.objects.create(name=v, set="test") for k, v in class_names}
    for i,image in enumerate(data['images'][:500]):
        path = "/Users/aub3/tests/yolo_test/{}.jpg".format(i)
        Image.fromarray(image).save(path)
        id_2_boxes[path.split('/')[-1]] = data['boxes'][i].tolist()
    local('zip /Users/aub3/tests/yolo_test.zip -r /Users/aub3/tests/yolo_test/* ')
    fname = "/Users/aub3/tests/yolo_test.zip"
    name = "yolo_test"
    f = SimpleUploadedFile(fname, file(fname).read(), content_type="application/zip")
    dv = handle_uploaded_file(f, name)
    perform_dataset_extraction(TEvent.objects.create(video=dv).pk)
    for df in Frame.objects.filter(video=dv):
        for box in id_2_boxes[df.name]:
            r = Region()
            r.video = dv
            r.frame = df
            c , top_x, top_y, bottom_x, bottom_y = box
            r.object_name = class_names[c]
            r.region_type = Region.ANNOTATION
            r.x = top_x
            r.y = top_y
            r.w = bottom_x - top_x
            r.h = bottom_y - top_y
            r.save()
            l = RegionLabel()
            l.frame = df
            l.video = dv
            l.label = labels[c]
            l.region = r
            l.save()
    perform_export(TEvent.objects.create(video=dv,arguments={'destination':'FILE'}).pk)
    try:
        shutil.rmtree('/Users/aub3/tests/yolo_test')
    except:
        pass
Ejemplo n.º 4
0
fname = "/Users/aub3/tests/yolo_test.zip"
name = "yolo_test"
f = SimpleUploadedFile(fname, file(fname).read(), content_type="application/zip")
dv = handle_uploaded_file(f, name)
perform_dataset_extraction(TEvent.objects.create(video=dv).pk)
for df in Frame.objects.filter(video=dv):
    for box in id_2_boxes[df.name]:
        r = Region()
        r.video = dv
        r.frame = df
        c, top_x, top_y, bottom_x, bottom_y = box
        r.object_name = class_names[c]
        r.region_type = Region.ANNOTATION
        r.x = top_x
        r.y = top_y
        r.w = bottom_x - top_x
        r.h = bottom_y - top_y
        r.save()
        l = RegionLabel()
        l.frame = df
        l.video = dv
        l.label = labels[c]
        l.region = r
        l.save()
perform_export(TEvent.objects.create(video=dv, arguments={'destination': 'FILE'}).pk)
try:
    shutil.rmtree('/Users/aub3/tests/yolo_test')
except:
    pass