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()
def create_detector_dataset(object_names, labels): class_distribution = defaultdict(int) rboxes = defaultdict(list) rboxes_set = defaultdict(set) frames = {} class_names = {k: i for i, k in enumerate(labels.union(object_names))} i_class_names = {i: k for k, i in class_names.items()} for r in Region.objects.all().filter(object_name__in=object_names): frames[r.frame_id] = r.frame if r.pk not in rboxes_set[r.frame_id]: rboxes[r.frame_id].append( (class_names[r.object_name], r.x, r.y, r.x + r.w, r.y + r.h)) rboxes_set[r.frame_id].add(r.pk) class_distribution[r.object_name] += 1 for dl in Label.objects.filter(name__in=labels): lname = dl.name for l in RegionLabel.all().objects.filter(label=dl): frames[l.frame_id] = l.frame if l.region: r = l.region if r.pk not in rboxes_set[r.frame_id]: rboxes[l.frame_id].append( (class_names[lname], r.x, r.y, r.x + r.w, r.y + r.h)) rboxes_set[r.frame_id].add(r.pk) class_distribution[lname] += 1 return class_distribution, class_names, rboxes, rboxes_set, frames, i_class_names
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
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)
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