def get_thumbnail(cls, image_id, image_base_path):
     thumbnail_path = cls._get_thumbnail_path(image_id)
     if not os.path.isfile(thumbnail_path):
         image = ImageDataHandler.get_image_by_id(image_id)
         cls._create_resized_image(
             os.path.join(image_base_path, image.file), thumbnail_path, 290)
     return thumbnail_path
示例#2
0
            def put(self, label_assignment_id):
                """ Update an existing label assignment """

                # FIXME: This isn't called from the main thread which may result in SQL Alchemy
                #        session problems.
                #     ... but somehow it also works like this most of the time.

                data = self.api.payload or {}
                label_data = data.get('label')
                if label_data and label_data.get('name'):
                    assignment = ImageDataHandler.update_label_assignment_label_name(
                        label_assignment_id, label_data['name']) or abort(404)
                else:
                    assignment = ImageDataHandler.get_label_assignment_by_id(
                        label_assignment_id) or abort(404)
                return assignment
示例#3
0
 def all_images():
     return {
         'images': [{
             'path': f'images/{filename}',
             'uid': filename,
         } for filename in ImageDataHandler.filelist()]
     }
示例#4
0
def test_bounding_box(client):
    ImageDataHandler.add_label_assignment('file1.jpg', 'label_with_bb',
                                          'bb_tester', 0.9,
                                          dict(l=0.2, r=0.3, t=0.1, b=0.5))

    assignments = ImageDataHandler.get_assignments_from_origin(
        'file1.jpg', 'bb_tester')
    assert assignments[0].box == {
        'bottom': 50.0,
        'left': 20.0,
        'right': 70.0,
        'top': 10.0
    }
    assert assignments[0].confidence == 0.9

    # no box
    assignments = ImageDataHandler.get_assignments_from_origin(
        'file1.jpg', 'nnet')
    assert assignments[0].box is None
示例#5
0
def test_filtered_images(client):
    ImageDataHandler.add_label_assignment('file4.jpg', 'l_ax', 'user')
    ImageDataHandler.add_label_assignment('file2.jpg', 'xl_cx', 'user')

    images = ImageDataHandler.filtered_images('l_a')
    assert sorted(image.file for image in images) == [
        'file has spaces.jpg', 'file1.jpg', 'file2.jpg', 'file4.jpg'
    ]

    images = ImageDataHandler.filtered_images('l*a')
    assert sorted(image.file for image in images) == [
        'file has spaces.jpg', 'file1.jpg', 'file2.jpg', 'file4.jpg'
    ]

    images = ImageDataHandler.filtered_images('*c*')
    assert sorted(image.file for image in images) == ['file2.jpg', 'file3.jpg']

    images = ImageDataHandler.filtered_images('*')
    assert sorted(image.file for image in images) == [
        'file has spaces.jpg', 'file1.jpg', 'file2.jpg', 'file3.jpg',
        'file4.jpg'
    ]
示例#6
0
 def get(self, image_id):
     return ImageDataHandler.get_image_by_id(image_id)
示例#7
0
 def get(self):
     all_images = ImageDataHandler.all_images()
     return all_images
示例#8
0
 def get(self, label_assignment_id):
     return ImageDataHandler.get_label_assignment_by_id(label_assignment_id) \
            or abort(404)
示例#9
0
 def get(self, label_id):
     return ImageDataHandler.get_label_by_id(label_id) or abort(404)
示例#10
0
 def get(self):
     return ImageDataHandler.all_labels()
示例#11
0
 def get(self, q):
     images = ImageDataHandler.filtered_images(q)
     return images
示例#12
0
def run_classifiers(config: ConfigParser):

    # Load ResNet model
    res_net = ResNet(config.model('resnet'), config.labels('resnet'))
    res_net.load()

    # Load YoloV4 model
    yolo_v4 = YoloV4(config.model('yolov4'), config.labels('yolov4'))
    yolo_v4.load()

    for row, image_path in enumerate(tqdm(config.image_files())):

        file_ending = os.path.splitext(image_path)[1].lower()
        if file_ending == 'arw':
            with rawpy.imread(image_path) as raw:
                image = raw.post_process()
        else:  # if file_ending in ['jpg', 'jpeg']:
            image = cv2.imread(image_path)  # pylint: disable=no-member

        # get relative path to image
        rel_path = os.path.relpath(image_path, config.image_folder())

        origin = 'ResNet:ImageNet'
        if not ImageDataHandler.get_assignments_from_origin(rel_path, origin):
            # classify image with resnet
            labels, confidences = res_net.classify(np.copy(image))

            # create new entry in database for image with relative path and top-5 labels
            for (label, confidence) in zip(labels, confidences):
                ImageDataHandler.add_label_assignment(rel_path, label_name=label, origin_name=origin,
                                                      confidence=confidence)

        origin = 'YoloV4:COCO'
        if not ImageDataHandler.get_assignments_from_origin(rel_path, origin):
            # classify image with yolov4
            labels, confidences, bounding_boxes = yolo_v4.classify(np.copy(image))

            for (label, confidence, bounding_box) in zip(labels, confidences, bounding_boxes):
                bounding_box = create_bounding_box(*[c for edge in bounding_box for c in edge])
                ImageDataHandler.add_label_assignment(rel_path, label_name=label, origin_name=origin,
                                                      confidence=confidence, bounding_boxes=bounding_box)

        origin = 'dlib_face_rec'
        if not ImageDataHandler.get_assignments_from_origin(rel_path, origin):
            scale_percent = 25  # percent of original size
            width = int(image.shape[1] * scale_percent / 100)
            height = int(image.shape[0] * scale_percent / 100)
            dim = (width, height)

            # resize image
            image_resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)

            # classify image with face_recognition
            bboxes = face_recognition.face_locations(image_resized, model='hog')  # order: top, right, bottom, left
            encodings = face_recognition.face_encodings(image_resized)

            for (bbox, encoding) in zip(bboxes, encodings):
                coor_orig = np.array([bbox[3], bbox[0], bbox[1], bbox[2]])  # left, top, right, bottom
                coor = normalize_coordinates(coor_orig, image_resized)
                bounding_box = create_bounding_box(*coor)
                ImageDataHandler.add_label_assignment(rel_path, label_name='unknown_face',
                                                      origin_name=origin, bounding_boxes=bounding_box,
                                                      encoding=json.dumps(list(encoding)), editable=True)
示例#13
0
 def index():
     return render_template('index.html',
                            images=ImageDataHandler.all_images())