Ejemplo n.º 1
0
    def delete(cls, student_id: int):
        student = StudentModel.find_by_id(student_id)
        if student:
            student.delete_from_db()
            # delete the folder containing images of student in the dataset
            id_path = os.path.join(DATASET_PATH, str(student_id))
            if os.path.exists(id_path):
                shutil.rmtree(id_path)
            # remove student training data
            ids = []
            encodings = []
            try:
                print("[INFO] loading encodings...")
                data = pickle.loads(open(ENCODINGS_FILE, "rb").read())
                # initialize the list of known encodings and known names
                known_encodings = data["encodings"]
                known_ids = data["ids"]
                for index in range(len(known_ids)):
                    if known_ids[index] != student_id:
                        ids.append(known_ids[index])
                        encodings.append(known_encodings[index])
                data = {"encodings": encodings, "ids": ids}
                f = open(ENCODINGS_FILE, "wb")
                f.write(pickle.dumps(data))
                f.close()
            except FileNotFoundError:
                pass
            return {"message": gettext('student_deleted').format(student.name, student.id)}, 200

        return {"message": gettext('student_not_found')}, 404
Ejemplo n.º 2
0
    def delete(cls, teacher_id: int):
        teacher = TeacherModel.find_by_id(teacher_id)
        if not teacher:
            return {"message": gettext("teacher_not_found")}, 404

        teacher.delete_from_db()
        return {"message": gettext("teacher_deleted")}, 200
Ejemplo n.º 3
0
    def delete(cls, feed_id: str):
        video_feed = VideoFeedModel.find_by_id(feed_id)
        if video_feed:
            video_feed.delete_from_db()
            return {
                "message": gettext('video_feed_deleted').format(video_feed.id)
            }, 200

        return {"message": gettext('video_feed_not_found')}, 404
Ejemplo n.º 4
0
    def post(cls):
        teacher_json = request.get_json()
        teacher = teacher_schema.load(teacher_json, session=Session)

        if TeacherModel.find_by_username(teacher.username):
            return {"message": gettext("teacher_username_exists")}, 400

        teacher.save_to_db()

        return {"message": gettext("teacher_registered")}, 201
Ejemplo n.º 5
0
    def get(cls, feed_id: str):
        video_feed = VideoFeedModel.find_by_id(feed_id)
        if video_feed:
            RecognitionCamera.stop(feed_id)
            try:
                video_feed.is_active = False
                video_feed.save_to_db()
            except:
                return {"message": gettext('internal_server_error')}, 500
            return {"message": gettext('video_feed_stopped')}, 200

        return {"message": gettext('video_feed_not_found')}, 404
Ejemplo n.º 6
0
    def get(cls, feed_id: str):
        """Video streaming route "/video_feed". Put this in the src attribute of an img tag."""
        video_feed = VideoFeedModel.find_by_id(feed_id)

        if video_feed:
            return video_feed_schema.dump(video_feed), 200

        return {"message": gettext('video_feed_not_found')}, 404
Ejemplo n.º 7
0
    def post(cls):
        video_feed_json = request.get_json()

        video_feed = video_feed_schema.load(video_feed_json, session=Session)

        try:
            video_feed.save_to_db()
        except:
            return {"message": gettext('error_inserting')}, 500

        return video_feed_schema.dump(video_feed), 201
Ejemplo n.º 8
0
    def post(cls, student_id: int):
        """
        Used to upload an images of student to the server.
        """
        data = image_schema.load(request.files)  # {"image": FileStorage}
        folder = os.path.join("dataset", str(student_id))  # static/images/dataset/1
        filename = str(len(os.listdir(os.path.join(DATASET_PATH, str(student_id)))) + 1)
        # image_path = image_helper.get_path(filename, folder)
        try:
            extension = image_helper.get_extension(data["image"].filename)
            save_as_filename = filename + extension
            image_path = image_helper.save_image(data["image"], folder=folder, name=save_as_filename)
            basename = image_helper.get_basename(image_path)
        except UploadNotAllowed:
            extension = image_helper.get_extension(data["image"])
            return {"message": gettext("image_illegal_extension").format(extension)}, 400

        # train images when submitted successfully
        TrainClassifier.train()
        return {"message": gettext("image_uploaded").format(basename)}, 201
Ejemplo n.º 9
0
    def post(cls):
        teacher_json = request.get_json()
        teacher_data = teacher_schema.load(teacher_json, session=Session)

        teacher = TeacherModel.find_by_username(teacher_data.username)

        if teacher and safe_str_cmp(teacher.password, teacher_data.password):
            access_token = create_access_token(identity=teacher.id, fresh=True)
            refresh_token = create_refresh_token(teacher.id)
            return {
                "access_token": access_token,
                "refresh_token": refresh_token
            }, 200

        return {"message": gettext("teacher_invalid_credentials")}, 401
Ejemplo n.º 10
0
    def post(cls):
        student_json = request.get_json()

        student = student_schema.load(student_json, session=Session)

        try:
            student.save_to_db()
        except:
            return {"message": gettext('error_inserting')}, 500

        # create a directory for <id> of the student
        id_path = os.path.join(DATASET_PATH, str(student.id))
        if not os.path.exists(id_path):
            os.makedirs(id_path)
        # todo: now automatically train the images
        return student_schema.dump(student), 201
Ejemplo n.º 11
0
    def get(cls, feed_id: str):
        """Video streaming route. Put this route in the src attribute of an img tag."""
        video_feed = VideoFeedModel.find_by_id(feed_id)
        feed_url = video_feed.url
        camera_stream = RecognitionCamera
        # Camera Device Selection
        if feed_url == "0":
            feed_url = 0
        elif feed_url == "1":
            feed_url = 1
        elif feed_url == "2":
            feed_url = 2
        elif feed_url == "3":
            feed_url = 3
        elif feed_url == "4":
            feed_url = 4
        camera_stream.set_video_source(feed_url)
        if video_feed:
            resp = Response(
                cls.gen_frame(camera_stream(unique_id=feed_id)),
                mimetype='multipart/x-mixed-replace; boundary=frame')
            return resp

        return {"message": gettext('video_feed_not_found')}, 404
Ejemplo n.º 12
0
 def get(cls):
     user_id = get_jwt_identity()
     if user_id:
         return {'message': gettext('you_are_logged_in')}, 200
     return {'message': gettext('login_to_continue')}, 401
Ejemplo n.º 13
0
    def get(cls, teacher_id: int):
        teacher = TeacherModel.find_by_id(teacher_id)
        if not teacher:
            return {"message": gettext("teacher_not_found")}, 404

        return teacher_schema.dump(teacher), 200