class AnnotationViewWebApp(FlaskView):
    route_base = '/'

    def __init__(self):
        self.dataset_controller = DatasetController()

    @route('/annotation', methods=['GET'])
    def annotation(self):
        # get the datasets to see which one the user wants to annotate
        datasets = self.dataset_controller.get_datasets()
        dataset_names = []
        for i, d in enumerate(datasets):
            dataset_names.append(d["name"])
            print("i", datasets[i])
            datasets[i][
                "category_colors"] = ColorUtils.distiguishable_colors_hex(
                    len(d["image_categories"]))
            datasets[i]["last_modified"] = ""  # it is not serialiable
            print("i", datasets[i])
            print("=========")

        print("datasets", datasets)

        # the user can choose to annotate a specif image/dataset
        sel_dataset = ""
        sel_image = ""
        if request.args.get("dataset") is not None:
            sel_dataset = request.args.get("dataset")
        if request.args.get("image") is not None:
            sel_image = request.args.get("image")

        return render_template('annotation.html',
                               dataset_names=dataset_names,
                               datasets=json.dumps(datasets),
                               sel_dataset=sel_dataset,
                               sel_image=sel_image)

    @route('/label-annotation', methods=['GET'])
    def label_annotation(self):
        # get the datasets to see which one the user wants to annotate
        datasets = self.dataset_controller.get_datasets()
        dataset_names = []
        for i, d in enumerate(datasets):
            dataset_names.append(d["name"])
            datasets[i][
                "category_colors"] = ColorUtils.distiguishable_colors_hex(
                    len(d["image_categories"]))
            datasets[i]["last_modified"] = ""  # it is not serialiable

        # the user can choose to annotate a specif dataset
        sel_dataset = ""
        if request.args.get("dataset") is not None:
            sel_dataset = request.args.get("dataset")
        app.logger.info("sel_dataset: " + sel_dataset)

        return render_template('annotation_label.html',
                               dataset_names=dataset_names,
                               datasets=json.dumps(datasets),
                               sel_dataset=sel_dataset)
Beispiel #2
0
class UploadViewWebApp(FlaskView):
    route_base = '/'

    def __init__(self):
        self.dataset_controller = DatasetController()
        self.image_controller = ImageController()

    @route('/add/images', methods=['GET'])
    def datasets(self):
        datasets = self.dataset_controller.get_datasets()
        return render_template('upload_images.html', datasets=datasets)

    def allowed_file(self, filename):
        ALLOWED_EXTENSIONS = set(['bmp', 'png', 'jpg', 'jpeg', 'gif'])
        return '.' in filename and \
               filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

    def get_frontend_image(self, request):
        file = request.files['file']
        filename = secure_filename(file.filename)
        file.save(os.path.join('/tmp/', filename))

        # load from file and read image
        try:
            image = cv2.imread(os.path.join('/tmp/', filename))
            return image
        except:
            raise error_views.InternalError(
                'Unable to read image from local folder.')

    @route('/image/front-upload', methods=['POST'])
    def create_image(self):
        file = request.files['file']
        if file and self.allowed_file(file.filename):
            if 'dataset' not in request.form:
                raise error_views.InternalError('No dataset provided!')
            dataset_name = request.form['dataset']

            category = 'default'
            if 'category' in request.form:
                category = request.form['category']

            image = self.get_frontend_image(request)
            (ok, error,
             image_id) = self.image_controller.create_image(dataset_name,
                                                            image,
                                                            name=file.filename,
                                                            category=category)
            if not ok:
                raise error_views.InvalidParametersError(error)

            return '', 200
        else:
            raise error_views.InternalError(
                'The provided file is not an accepted extension.')
class PluginsViewWebApp(FlaskView):
    route_base = '/'

    def __init__(self):
        self.dataset_controller = DatasetController()

    @route('/plugins', methods=['GET'])
    def index(self):
        # get the datasets to see which one the user wants to annotate
        datasets = self.dataset_controller.get_datasets()
        return render_template('plugins.html', datasets=datasets)
Beispiel #4
0
class DatasetViewWebApp(FlaskView):
    route_base = '/'
    print('DatasetViewWebApp static')

    def __init__(self):
        self.dataset_controller = DatasetController()
        print('DatasetViewWebApp constructor')

    @route('/', methods=['GET'])
    def datasets_get(self):
        datasets = self.dataset_controller.get_datasets()
        for d in datasets:
            if len(d['annotation_labels']) > 6:
                d['annotation_labels'] = d['annotation_labels'][:6]
        return render_template('dataset.html', datasets=datasets)
Beispiel #5
0
 def __init__(self):
     self.dataset_controller = DatasetController()
     self.image_controller = ImageController()
Beispiel #6
0
class UploadVideoViewWebApp(FlaskView):
    route_base = '/'

    def __init__(self):
        self.dataset_controller = DatasetController()
        self.image_controller = ImageController()

    @route('/add/video', methods=['GET'])
    def datasets(self):
        datasets = self.dataset_controller.get_datasets()
        return render_template('upload_video.html', datasets=datasets)

    def allowed_file(self, filename):
        ALLOWED_EXTENSIONS = set(['mov', 'mpeg', 'mp4', 'mpg', 'avi'])
        return '.' in filename and \
               filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

    def extract_frontend_frames(self, request, step_frame):
        file = request.files['file']
        filename = secure_filename(file.filename)
        file.save(os.path.join('/tmp/', filename))

        vid = cv2.VideoCapture(os.path.join('/tmp/', filename))
        if not vid.isOpened():
            raise error_views.InternalError("Not possible to open video file.")
        else:
            frames = []
            ret, frame = vid.read()
            i = 0
            while frame is not None:
                if i % step_frame == 0:
                    # save it as base64 image
                    img_bytes = cv2.imencode('.jpg', frame)[1]
                    img_encoded = base64.b64encode(img_bytes).decode()
                    frames.append(img_encoded)

                i = i + 1
                ret, frame = vid.read()
        vid.release()

        return frames

    @route('/video/front-upload', methods=['POST'])
    def create_video(self):
        file = request.files['file']
        if file and self.allowed_file(file.filename):
            if 'step_frame' in request.form:
                step_frame = int(request.form['step_frame'])
            else:
                app.logger("Step frame not provided, assuming 10.")
                step_frame = 10

            frames = self.extract_frontend_frames(request, step_frame)
            if not frames:
                raise error_views.InternalError(
                    "Unable to extract frames from the video file.")

            return flask.jsonify({"frames_b64": frames})
        else:
            raise error_views.InternalError(
                'The provided file is not an accepted extension.')
 def __init__(self):
     self.dataset_controller = DatasetController()
Beispiel #8
0
 def __init__(self):
     self.dataset_controller = DatasetController()
     print('DatasetViewWebApp constructor')
class DatasetView(FlaskView):
    route_base = '/'

    def __init__(self):
        self.controller = DatasetController()
        self.image_controller = ImageController()

    @route('/dataset/all', methods=['GET'])
    def get_all_datasets(self):
        obj = self.controller.get_datasets()
        return flask.jsonify({"datasets": obj})

    @route('/dataset/<dataset>', methods=['GET'])
    def get_dataset(self, dataset):
        # obj = self.image_controller.all_images(dataset)

        d = self.controller.get_dataset(dataset)
        if d == None:
            return "Dataset not found", 404
        else:
            return flask.jsonify({"dataset": d})

    @route('/dataset/<dataset>/size', methods=['GET'])
    def get_dataset_size(self, dataset):
        obj = self.image_controller.all_images(dataset)
        print("obj", obj)
        # return flask.jsonify({"dataset_size": len(obj)})
        print('returning', flask.jsonify({"dataset_size": len(obj)}))
        return flask.jsonify({"dataset_size": len(obj)})

    @route('/dataset/<dataset>', methods=['DELETE'])
    def delete_dataset(self, dataset):
        ok, error = self.controller.purge_dataset(dataset)
        if not ok:
            raise error_views.InvalidParametersError(error)
        else:
            return '', 200

    @route('/dataset/<dataset>/partition', methods=['POST'])
    def create_partition(self, dataset):
        (ok, error, partitions) = view_tools.get_param_from_request(
            request, "partitions")
        if not ok:
            raise error_views.InvalidParametersError(error)

        (ok, error, percentages) = view_tools.get_param_from_request(
            request, "percentages")
        if not ok:
            raise error_views.InvalidParametersError(error)

        (ok, error) = self.controller.set_partition(dataset, partitions,
                                                    percentages)
        if not ok:
            raise error_views.InvalidParametersError(error)

        return '', 200

    @route('/dataset/create', methods=['POST'])
    def create_datasets(self):
        (ok, error, name) = view_tools.get_param_from_request(request, "name")
        if not ok:
            raise error_views.InvalidParametersError(error)

        (ok, error, tags) = view_tools.get_param_from_request(request, "tags")
        if not ok:
            raise error_views.InvalidParametersError(error)

        self.controller.create_dataset(name, tags)
        return flask.jsonify({"ok": "to-do"})
Beispiel #10
0
class PluginsView(FlaskView):
    route_base = '/'

    def __init__(self):
        self.controller = PluginsController()
        self.image_controller = ImageController()
        self.dataset_controller = DatasetController()

    @route('/plugins/process/<dataset>/<imageid>', methods=['GET'])
    def get_plugin_process_image(self, dataset, imageid):
        self.get_plugin_from_request(request)

        self.controller.init_plugin(
            self.dataset_controller.get_dataset(dataset))

        (img_m, img_o) = self.get_image_matrix_and_dict(dataset, imageid)
        (img_m, img_o) = self.controller.process(img_m, img_o)
        plugin_res = self.controller.end_plugin()

        fileid = "img"  # uuid.uuid4().hex
        full_filename = 'annotator_supreme/static/' + fileid + '.jpg'
        cv2.imwrite(full_filename, img_m)
        filename = 'static/' + fileid + '.jpg'

        # return flask.send_file(filename, mimetype='image/jpeg')
        img_bytes = cv2.imencode('.jpg', img_m)[1].tostring()
        img_encoded = base64.b64encode(img_bytes).decode()

        return flask.jsonify({
            'image': img_encoded,
            'plugin_response': plugin_res
        })

    @route('/plugins/process/<dataset>', methods=['GET'])
    def get_plugin_process(self, dataset):
        self.get_plugin_from_request(request)

        self.controller.init_plugin(
            self.dataset_controller.get_dataset(dataset))

        all_imgs = ImageController.all_images(dataset)
        for im_obj in all_imgs:
            (img_m,
             img_o) = self.get_image_matrix_and_dict(dataset, im_obj['phash'])
            (img_m, img_o) = self.controller.process(img_m, img_o)
            bbs_vec = []
            for bb in img_o['anno']:
                bbox_o = BBox(bb['top'], bb['left'], bb['bottom'], bb['right'],
                              bb['labels'], bb['ignore'])
                bbs_vec.append(bbox_o)
            self.image_controller.change_annotations(dataset, img_o['phash'],
                                                     bbs_vec)

        plugin_res = self.controller.end_plugin()
        return flask.jsonify({"plugin_response": plugin_res})

    @route('/plugins/process/partition/<dataset>/<partition>', methods=['GET'])
    def get_plugin_process_partition(self, dataset, partition):
        self.get_plugin_from_request(request)

        if partition == "" or (partition != "training"
                               and partition != "testing"):
            raise error_views.InvalidParametersError(
                "Partition not informed or invalid.")

        self.controller.init_plugin(
            self.dataset_controller.get_dataset(dataset), partition)

        all_imgs = ImageController.all_images(dataset)
        for im_obj in all_imgs:
            (img_m,
             img_o) = self.get_image_matrix_and_dict(dataset, im_obj['phash'])
            (img_m, img_o) = self.controller.process(img_m, img_o)
            bbs_vec = []
            for bb in img_o['anno']:
                bbox_o = BBox(bb['top'], bb['left'], bb['bottom'], bb['right'],
                              bb['labels'], bb['ignore'])
                bbs_vec.append(bbox_o)
            self.image_controller.change_annotations(dataset, img_o['phash'],
                                                     bbs_vec)

        plugin_res = self.controller.end_plugin()
        return flask.jsonify({"plugin_response": plugin_res})

    @route('/plugins/all', methods=['GET'])
    def get_plugin_all(self):
        plugins = self.controller.get_all_plugins()
        return flask.jsonify({"plugins": sorted(plugins)})

    def get_plugin_from_request(self, request):
        (ok, error,
         plugin_name) = view_tools.get_param_from_request(request, 'plugin')
        if not ok:
            raise error_views.InvalidParametersError(error)

        ok = self.controller.load_plugin(plugin_name)
        if not ok:
            print('An error occurred while loading plugin: ' + plugin_name)
            raise error_views.InternalError(
                'An error occurred while loading plugin: ' + plugin_name)

    def get_image_matrix_and_dict(self, dataset, imageid):
        img = self.image_controller.get_image(dataset, imageid)
        img_o = self.image_controller.get_image_object(dataset, imageid)
        img_d = view_tools.image_to_dict(img_o)

        return (img, img_d)