Skip to content
Snippets Groups Projects
Select Git revision
  • 7d1101ed96acc0c6ff16c76736cb031df6faccbc
  • master default protected
  • 51-docker
  • 40-return-match-when-edit-salamander
  • 36-email-verification
  • 23-add-object-detection
  • 22-run-branch
7 results

findsalamanderinfo.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    findsalamanderinfo.py 1.85 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
    import cv2
    import os
    from api import limiter
    
    ALLOWED_EXTENSIONS = ['jpeg', 'png', 'jpg']
    
    
    # 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.png", str_image)
                        return jsonify({"sex": "AI_sex", "species": "AI_species", "status": 200})
    
                    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()