diff --git a/api/endpoints/salamander.py b/api/endpoints/salamander.py index 7579f9f2b8d022b22886cefc217eec32eb8c4709..576bc4302c5c9d38341875b5269f91d6007d4290 100644 --- a/api/endpoints/salamander.py +++ b/api/endpoints/salamander.py @@ -4,7 +4,10 @@ from flask_restful import Resource from api import db, limiter from api.models.dbmodels import Location, User, Salamander import os +import cv2 import glob +import cv2 +import base64 from image_encoder.image_encoder import * from path_constants import _ACCESS_DATABASE @@ -28,9 +31,30 @@ class SalamanderEndpoint(Resource): list_of_paths = glob.glob(os.path.join(path_to_salamander_images, '*.*')) for path in list_of_paths: if not path.__contains__("_str"): - encoded = encode(path)[2:-1] + + image = cv2.imread(path) + + #scaling to set size + height, width, _ = image.shape + + desired_long_side = 320 + scaling_factor = 1 + + if width > height: + scaling_factor = desired_long_side / width + else: + scaling_factor = desired_long_side / height + + new_dim = (int(width * scaling_factor), int(height * scaling_factor)) + + # resize image + image = cv2.resize(image, new_dim, interpolation=cv2.INTER_AREA) + + _, buffer = cv2.imencode('.jpg', image) + encoded = base64.b64encode(buffer) + image_id = os.path.basename(path)[0] - image_data = {"id": image_id, "url": "data:image/png;base64," + encoded} + image_data = {"id": image_id, "url": "data:image/png;base64," + encoded.decode()} salamander_images.append(image_data) salamander_images.sort(key=lambda x: x.get('id')) diff --git a/api/endpoints/salamanderimageid.py b/api/endpoints/salamanderimageid.py index 2cab2a61fb0774aa22c1e44a24d1fec66f6b07f8..6898d5383cdec7a4b04843247d1d88ff69049e5a 100644 --- a/api/endpoints/salamanderimageid.py +++ b/api/endpoints/salamanderimageid.py @@ -1,3 +1,5 @@ +import sys + from flask import jsonify from flask_jwt_extended import jwt_required, get_jwt_identity from flask_restful import Resource @@ -5,8 +7,12 @@ from api import db, limiter from api.models.dbmodels import Location, User, Salamander, SalamanderGrowth import os import glob +import cv2 +import sys from image_encoder.image_encoder import * from path_constants import _ACCESS_DATABASE +import cv2 + # get one specific image on a specific salamander based on s_id and i_id class SalamanderImageEndpoint(Resource): @@ -20,20 +26,49 @@ class SalamanderImageEndpoint(Resource): if user.admin: with _ACCESS_DATABASE: salamander = db.session.query(Salamander).filter_by(id=salamander_id).first() - salamander_growth = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander.id, image_id=image_id).first() + salamander_growth = db.session.query(SalamanderGrowth).filter_by(salamander_id=salamander.id, + image_id=image_id).first() if salamander and salamander_growth: location = db.session.query(Location).filter_by(id=salamander.location_id).first() salamander_images = [] - path_to_salamander_images = os.path.join("./images", location.name, salamander.species, salamander.sex, str(salamander.id)) + path_to_salamander_images = os.path.join("./images", location.name, salamander.species, + salamander.sex, str(salamander.id)) list_of_paths = glob.glob(os.path.join(path_to_salamander_images, '*.*')) for path in list_of_paths: basename = os.path.basename(path) if basename.__contains__(str(image_id)): - encoded = encode(path)[2:-1] - image_data = {"url": "data:image/png;base64," + encoded} + + image = cv2.imread(path) + if not basename.__contains__("str"): #Scale only original to set size + + height, width, _ = image.shape + print(sys.getsizeof(image)) + + desired_long_side = 320 + scaling_factor = 1 + + if width > height: + scaling_factor = desired_long_side/width + else: + scaling_factor = desired_long_side/height + + new_dim = (int(width*scaling_factor), int(height*scaling_factor)) + + # resize image + image = cv2.resize(image, new_dim, interpolation=cv2.INTER_AREA) + print(sys.getsizeof(image)) + + _, buffer = cv2.imencode('.jpg', image) + encoded = base64.b64encode(buffer) + + image_data = {"url": "data:image/png;base64," + encoded.decode()} + salamander_images.append(image_data) - return jsonify({"images": salamander_images, "length": salamander_growth.length, "weight": salamander_growth.weight, "imageId": image_id, "location": location.name, "salamanderId": salamander.id, "sex": salamander.sex, "species": salamander.species, 'status': 200}) + return jsonify({"images": salamander_images, "length": salamander_growth.length, + "weight": salamander_growth.weight, "imageId": image_id, "location": location.name, + "salamanderId": salamander.id, "sex": salamander.sex, "species": salamander.species, + 'status': 200}) else: return jsonify({"message": "no salamander with this id or no growth data", 'status': 400}) else: