Example #1
0
def label_upload(request, imageset_id):
    imageset = get_object_or_404(ImageSet, id=imageset_id)
    if not imageset.has_perm('annotate', request.user):
        messages.warning(
            request,
            _('You do not have permission to upload the annotations to this set.'
              ))
        return redirect(reverse('images:view_imageset', args=(imageset_id, )))

    images = Image.objects.filter(image_set=imageset)
    report_list = list()
    if request.method == 'POST':
        error_count = 0
        similar_count = 0
        verify = 'verify' in request.POST.keys()
        for line in request.FILES['file']:
            # filter empty lines
            print(line)
            if line in ('', "b'\n'"):
                continue
            dec_line = line.decode().replace('\n', '').replace(',}', '}')
            line_frags = dec_line.split('|')
            image = images.filter(name=line_frags[0])
            if image.exists():
                image = image[0]
                annotation_type = AnnotationType.objects.filter(
                    name=line_frags[1], active=True)
                if annotation_type.exists():
                    annotation_type = annotation_type[0]
                    vector = False
                    blurred = False
                    concealed = False
                    if len(line_frags) > 3:
                        flags = line_frags[3]
                        test_flags = flags.replace('b', '')
                        test_flags = test_flags.replace('c', '')
                        if len(test_flags) > 0:
                            report_list.append(
                                'unknown flags: \"{}\" for image: \"{}\"'.
                                format(test_flags, line_frags[0]))
                        blurred = 'b' in flags
                        concealed = 'c' in flags
                    if line_frags[2] == 'not in image' or line_frags[2] == '{}':
                        vector = None

                    else:
                        try:
                            vector = json.loads(line_frags[2])
                        except Exception as error:
                            report_list.append(
                                "In image \"{}\" the annotation:"
                                " \"{}\" was not accepted as valid JSON".
                                format(line_frags[0], line_frags[2]))

                    if annotation_type.validate_vector(vector):
                        if not Annotation.similar_annotations(
                                vector, image, annotation_type):
                            annotation = Annotation()
                            annotation.annotation_type = annotation_type
                            annotation.image = image
                            annotation.user = request.user
                            annotation.vector = vector
                            annotation._blurred = blurred
                            annotation._concealed = concealed
                            annotation.save()
                            if verify:
                                verification = Verification()
                                verification.user = request.user
                                verification.annotation = annotation
                                verification.verified = True
                                verification.save()
                        else:
                            similar_count += 1
                            report_list.append(
                                'For the image ' + line_frags[0] +
                                ' the annotation ' + line_frags[2] +
                                ' was too similar to an already existing one')
                    else:
                        error_count += 1
                        report_list.append(
                            'For the image ' + line_frags[0] +
                            ' the annotation ' + line_frags[2] +
                            ' was not a valid vector or '
                            'bounding box for the annotation type')
                else:
                    error_count += 1
                    report_list.append('For the image ' + line_frags[0] +
                                       ' the annotation type \"' +
                                       line_frags[1] +
                                       '\" does not exist in this ImageTagger')
            else:
                error_count += 1
                report_list.append('The image \"' + line_frags[0] +
                                   '\" does not exist in this imageset')

        for element in report_list[:20]:
            messages.error(request, element)
            if len(report_list) > 20:
                messages.warning(request,
                                 'Only the first 20 errors are displayed.')
        if error_count + similar_count > 0:
            messages.warning(
                request,
                _('The label upload ended with {} errors and {} similar existing labels.'
                  ).format(error_count, similar_count))
        else:
            messages.success(
                request,
                _('The label upload ended with {} errors and {} similar existing labels.'
                  ).format(error_count, similar_count))
    return redirect(reverse('images:view_imageset', args=(imageset_id, )))
Example #2
0
def label_upload(request, imageset_id):
    imageset = get_object_or_404(ImageSet, id=imageset_id)
    if not imageset.has_perm('annotate', request.user):
        messages.warning(
            request,
            _('You do not have permission to upload the annotations to this set.'
              ))
        return redirect(reverse('images:view_imageset', args=(imageset_id, )))

    images = Image.objects.filter(image_set=imageset)
    if request.method == 'POST':
        error_count = 0
        similar_count = 0
        verify = 'verify' in request.POST.keys()
        for line in request.FILES['file']:
            dec_line = line.decode().replace('\n', '')
            line_frags = dec_line.split('|')
            image = images.filter(name=line_frags[0])
            if image.exists():
                image = image[0]
                annotation_type = AnnotationType.objects.filter(
                    name=line_frags[1])
                if annotation_type.exists():
                    annotation_type = annotation_type[0]
                    vector = False
                    if line_frags[2] == 'not in image':
                        vector = None
                    elif int(line_frags[2]) and int(line_frags[3]) and int(
                            line_frags[4]) and int(line_frags[5]):
                        vector = {
                            'x1': int(line_frags[2]),
                            'y1': int(line_frags[3]),
                            'x2': int(line_frags[4]),
                            'y2': int(line_frags[5]),
                        }
                    if Annotation.validate_vector(
                            vector, Annotation.VECTOR_TYPE.BOUNDING_BOX):
                        if not Annotation.similar_annotations(
                                vector, image, annotation_type):
                            annotation = Annotation()
                            annotation.annotation_type = annotation_type
                            annotation.image = image
                            annotation.user = request.user
                            annotation.vector = vector
                            annotation.save()
                            if verify:
                                verification = Verification()
                                verification.user = request.user
                                verification.annotation = annotation
                                verification.verified = True
                                verification.save()
                        else:
                            similar_count += 1
                    else:
                        error_count += 1
                else:
                    error_count += 1

            else:
                error_count += 1
        messages.warning(
            request,
            _('The label upload ended with {} errors and {} similar existing labels.'
              ).format(error_count, similar_count))
    return redirect(reverse('images:view_imageset', args=(imageset_id, )))