Skip to content
Snippets Groups Projects
Select Git revision
  • f6ca4a16c4a02534e6ac093e0e17256e41c74968
  • main default protected
  • Tormod
  • Alex
  • Martin
  • mqttTest
6 results

addProjectMap.js

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    findsalamanderinfo.py 2.10 KiB
    from flask import request, jsonify
    from flask_restful import Resource
    from flask_jwt_extended import get_jwt_identity, jwt_required
    from algorithm.train_src.new_straightening import straighten
    from algorithm.train_src.estimate_gender import estimate_image
    from path_constants import image_type
    import cv2
    import os
    from api import limiter
    from image_encoder.image_encoder import *
    
    ALLOWED_EXTENSIONS = [image_type]
    
    
    # findSalamanderInfo endpoint
    class FindSalamanderInfo(Resource):
        decorators = [limiter.limit("5/minute")]
    
        @staticmethod
        @jwt_required
        def post():
            if "image" not in request.files:
                return jsonify({"message": "no file given", "status": 400})
            image = request.files['image']
            user_id = get_jwt_identity()
            if image.filename != '':
                if image and allowed_image(image.filename):
                    image.filename = str(user_id) + "." + extension(image.filename)
                    image.save("temp_images/" + image.filename)
                    execution_path = os.path.abspath(os.getcwd())
                    img = cv2.imread(execution_path + "/temp_images/" + str(user_id) + "." + extension(image.filename), 1)
                    # run AI on image
                    str_image, _, _, _, _ = straighten(img)
                    if str_image is not None:
                        cv2.imwrite("temp_images/" + str(user_id) + "_str."+image_type, str_image)
                        encoded = encode("./temp_images/" + str(user_id) + "_str."+image_type)[2:-1]
                        return jsonify({"sex": "AI_sex", "species": "AI_species", "status": 200, "image": encoded})
    
                    else:
                        return jsonify({"message": "image could not be processed", "status": 400})
                else:
                    return jsonify({"message": "file extension not allowed", "status": 400})
            else:
                return jsonify({"message": "something wrong with image", "status": 400})
    
    
    def allowed_image(filename):
        return '.' in filename and \
               filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
    
    
    def extension(filename):
        return filename.rsplit('.', 1)[1].lower()