Ejemplo n.º 1
0
def similarity_search_query(request):
    if request.is_ajax() and request.method == "POST" and \
            all(x in request.POST for x in ['mode', 'url', 'select', 'max']):
        try:
            max_results = int(request.POST['max'])
        except ValueError:
            return HttpResponseBadRequest()
        mode = request.POST['mode']
        if mode == "upload":
            if "file" not in request.FILES:
                return HttpResponseBadRequest("No file specified")
            similarity_search_result = search(SearchMode.UPLOAD,
                                              request.FILES['file'],
                                              max_results)
        elif mode == "url":
            similarity_search_result = search(SearchMode.URL,
                                              request.POST['url'], max_results)
        elif mode == "select":
            similarity_search_result = search(SearchMode.DB_IMAGE,
                                              request.POST['select'],
                                              max_results)
        else:
            return HttpResponseBadRequest()
        if similarity_search_result['error']:
            return HttpResponseBadRequest(similarity_search_result['error'])
        serialized_images = serialize_images(
            similarity_search_result['images'], request, return_dict=True)
        serialized_images['time'] = similarity_search_result['time']
        return HttpResponse(json.dumps(serialized_images))
    return HttpResponseBadRequest()
Ejemplo n.º 2
0
 def retrieval_query_data(request: WSGIRequest) -> HttpResponse:
     class_type_name = Classtype.objects.get(id=class_type_id).name.lower()
     if not request.is_ajax() or request.method != "POST" or \
             not all(x in request.POST for x in [class_type_name, 'asc', 'annotations']):
         return HttpResponseBadRequest(
             "Wrong request method or missing parameter!")
     try:
         class_id = int(request.POST[class_type_name])
     except ValueError:
         return HttpResponseBadRequest("Class ID needs to be an integer!")
     s_class = Class.objects.get(id=class_id)
     s_ascending = request.POST['asc'] == "1"
     s_annotations = request.POST['annotations'] == "1"
     evaluation_classtypeid = Evaluation.objects.filter(
         classid__classtypeid_id=class_type_id).values_list('id', flat=True)
     latest_model = Model.objects.filter(
         evaluation__in=evaluation_classtypeid,
         inference_stored=True).latest('date')
     predictions = Imageprediction.objects \
         .filter(classid=s_class, modelid=latest_model.id) \
         .order_by('score' if s_ascending else '-score')
     if not s_annotations:
         # the image can be annotated for another class - so there might exist an image annotation but without a
         # matching class id
         predictions = predictions.filter(
             ~Q(imageid__imageannotation__classid=s_class)
             | Q(imageid__imageannotation__isnull=True))
     return HttpResponse(
         serialize_images(
             predictions, request, lambda x: x.imageid,
             lambda image_dict, image: add_prediction_score(
                 image_dict, image, latest_model.id, s_class.id)))
Ejemplo n.º 3
0
def start_inference(request):
    """
    Starts the face processing for all available videoframes in the database
    :param request: the request
    :return:
    """
    if request.method == 'POST':
        reset_counters()
        # Inference is conducted on video keyframes only, excluding web or other kind of images
        videoframes = Videoframe.objects.all().values_list("id", flat=True)
        all_images = list(Image.objects.filter(videoframe__in=videoframes))

        all_images_dict = serialize_images(
            all_images,
            request,
            return_dict=True,
            additional_value_function=add_absolute_image_path)

        all_images_dict['elements'] = update_face_list(
            all_images_dict['elements'], is_inference=True, user=request.user)

        all_images_dict['count'] = len(all_images_dict['elements'])

        create_image_encodings(all_images_dict['elements'], training=False)
        return HttpResponse()
    return HttpResponseBadRequest()
Ejemplo n.º 4
0
def review_query(request: WSGIRequest):
    """Delivers a HTTP response that contains only images wherefore it exists a specified annotation
    (groundtruth) for the current selected class (stored in session). Conditions were specified by
    HTTP Post method parameters.

    :param request: the request
    :return: serialized images (JSON) that match the conditions
    """
    if request.is_ajax() and request.method == "POST" and all(
            x in request.POST for x in ['groundtruth']):

        bbox_objects = Bboxannotation.objects.filter(
            classid_id=request.session[
                SessionConfig.SELECTED_PERSON_SESSION_KEY]).order_by(
                    '-imageid')
        serialized_images_dict = serialize_images(
            bbox_objects,
            request,
            lambda x: x.imageid,
            return_dict=True,
            additional_value_function=add_absolute_image_path)

        serialized_images_dict = update_groundtruth_annotation(
            serialized_images_dict,
            request.session[SessionConfig.SELECTED_PERSON_SESSION_KEY],
            mode="image")

        if 'face' in request.COOKIES.keys():
            if request.COOKIES['face'] != 'photo':
                serialized_images_dict['elements'] = update_face_list_from_db(
                    serialized_images_dict['elements'], bbox_objects)

        return HttpResponse(json.dumps(serialized_images_dict))
    return HttpResponseBadRequest()
Ejemplo n.º 5
0
def similarity_search_query(request):
    simsearch_dict = similarity_search_handle_request(request)
    serialized_images_dict = serialize_images(
        simsearch_dict['images'],
        request,
        return_dict=True,
        additional_value_function=add_absolute_image_path)

    serialized_images_dict = update_groundtruth_annotation(
        serialized_images_dict,
        request.session[SessionConfig.SELECTED_PERSON_SESSION_KEY],
        mode="bbox",
        bbox_lst=simsearch_dict["bbox_check_list"])
    serialized_images_dict['time'] = simsearch_dict['time']

    return HttpResponse(json.dumps(serialized_images_dict))
Ejemplo n.º 6
0
    def import_query_data(request: WSGIRequest) -> HttpResponse:
        grid_elements = []
        file = ''
        try:
            file = request.FILES["upload"]
        except MultiValueDictKeyError:
            pass

        if file and allowed_file(file.name):
            data, allowed_columns = read_csv(file)
            if allowed_columns:
                for el in data:
                    video_name, frame_start, frame_end = el
                    try:
                        video = Video.objects.get(path=video_name)
                        frame_set = Videoframe.objects.filter(
                            videoid=video.id).filter(
                                number__range=[frame_start, frame_end
                                               ]).order_by('number')

                    except ObjectDoesNotExist:
                        continue
                    for frame in frame_set:
                        try:
                            img = Image.objects.get(id=frame.imageid.id)
                            grid_elements.append(img)
                        except ObjectDoesNotExist:
                            pass

        serialized_images_dict = serialize_images(
            grid_elements,
            request,
            return_dict=True,
            additional_value_function=add_absolute_image_path)

        if class_type_id == FaceRecognitionConfig.class_type_id:
            serialized_images_dict['elements'] = update_face_list(
                serialized_images_dict['elements'])
            serialized_images_dict = update_groundtruth_annotation(
                serialized_images_dict,
                request.session[SessionConfig.SELECTED_PERSON_SESSION_KEY],
                mode="bbox")

        return HttpResponse(json.dumps(serialized_images_dict))
Ejemplo n.º 7
0
def start_training(request):
    if request.method == 'POST':
        reset_counters()
        images = []
        class_lst = []
        bbox_lst = []

        all_classes = Class.objects.filter(
            classtypeid=FaceRecognitionConfig.class_type_id)
        for class_object in all_classes:
            bbox_annotations = Bboxannotation.objects.filter(
                classid=class_object)
            if PersonTrainInferConfig.MIN_ANNOTATIONS <= len(bbox_annotations):
                for bbox_anno in bbox_annotations:
                    bbox_lst.append(bbox_anno)
                    class_lst.append(class_object.id)
                    images.append(Image.objects.get(id=bbox_anno.imageid.id))

        images_dict = serialize_images(
            images,
            request,
            return_dict=True,
            additional_value_function=add_absolute_image_path)

        for idx, element in enumerate(images_dict['elements']):
            element.update({
                'classid': class_lst[idx],
                'bbox': {
                    "x": bbox_lst[idx].x,
                    "y": bbox_lst[idx].y,
                    "w": bbox_lst[idx].w,
                    "h": bbox_lst[idx].h
                }
            })
        images_dict['count'] = len(images_dict['elements'])

        create_image_encodings(images_dict['elements'], training=True)

        train_classifier()

        return HttpResponse()
    return HttpResponseBadRequest()
Ejemplo n.º 8
0
def review_query(request: WSGIRequest):
    """Delivers a HTTP response that contains only images wherefore it exists a specified annotation
    (difficult, groundtruth) for the current selected class (stored in session). Conditions were specified by
    HTTP Post method parameters.

    :param request: the request
    :return: serialized images (JSON) that match the conditions
    """
    if request.is_ajax() and request.method == "POST" and all(
            x in request.POST for x in ['difficult', 'groundtruth']):
        return HttpResponse(
            serialize_images(Imageannotation.objects.filter(
                groundtruth=request.POST['groundtruth'] == "1",
                difficult=request.POST['difficult'] == "1",
                classid_id=request.session[
                    SessionConfig.SELECTED_CONCEPT_SESSION_KEY]).order_by(
                        '-imageid'),
                             request,
                             image_extractor=lambda x: x.imageid))
    return HttpResponseBadRequest()