From 04f28c9384e34ec9af5767e4f1bf13c89553a90f Mon Sep 17 00:00:00 2001 From: AndersLan <andeslan@stud.ntnu.no> Date: Wed, 9 Jun 2021 14:14:53 +0200 Subject: [PATCH] Added set size downscaling --- api/endpoints/salamander.py | 23 ++++++++++++++++++++++- api/endpoints/salamanderimageid.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/api/endpoints/salamander.py b/api/endpoints/salamander.py index fb2c3a2..2cf5930 100644 --- a/api/endpoints/salamander.py +++ b/api/endpoints/salamander.py @@ -6,6 +6,8 @@ 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 @@ -43,8 +45,27 @@ class SalamanderEndpoint(Resource): encoded = base64.b64encode(cv2.imencode(".jpg", resized)[1]).decode() + #scaling to set size + # height, width, _ = image.shape + # + # desired_long_side = 150 + # 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 her om bestemt størrelse skal brukes 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 8477ffc..97894fd 100644 --- a/api/endpoints/salamanderimageid.py +++ b/api/endpoints/salamanderimageid.py @@ -7,6 +7,8 @@ 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 @@ -50,6 +52,34 @@ class SalamanderImageEndpoint(Resource): encoded = base64.b64encode(cv2.imencode(".jpg", resized)[1]).decode() image_data = {"url": "data:image/png;base64," + encoded} + + #Scale only original to set size + # image = cv2.imread(path) + # if not basename.__contains__("str"): + # + # 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) + # + # #encoded = encode(path)[2:-1] + # image_data = {"url": "data:image/png;base64," + encoded.decode()} + salamander_images.append(image_data) return jsonify({"images": salamander_images, "length": salamander_growth.length, -- GitLab