From e8ae8935f249dce8d6c99dd3027b96a0285c7106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique?= Date: Sat, 4 Nov 2023 00:41:12 -0300 Subject: [PATCH] adding flask protobuf --- ActixAPI/Dockerfile | 2 +- FlaskAPI/Dockerfile | 7 ++++- FlaskAPI/controllers/image.py | 14 ---------- FlaskAPI/controllers/simulation.py | 10 +++++++ FlaskAPI/helloworld.proto | 5 ++++ FlaskAPI/helloworld_pb2.py | 26 ++++++++++++++++++ FlaskAPI/requirements.txt | 2 +- FlaskAPI/services/image.py | 42 ------------------------------ 8 files changed, 49 insertions(+), 59 deletions(-) create mode 100644 FlaskAPI/helloworld.proto create mode 100644 FlaskAPI/helloworld_pb2.py diff --git a/ActixAPI/Dockerfile b/ActixAPI/Dockerfile index 873964d..23dfd4f 100644 --- a/ActixAPI/Dockerfile +++ b/ActixAPI/Dockerfile @@ -2,7 +2,7 @@ FROM rust:latest WORKDIR /app -RUN RUN apt-get update && apt-get -y install wget && \ +RUN apt-get update && apt-get -y install wget && \ wget https://files.ivanch.me/api/public/dl/iFuXSNhw/small-image.png && \ wget https://files.ivanch.me/api/public/dl/81Bkht5C/big-image.png && \ wget https://files.ivanch.me/api/public/dl/nAndfAjK/video.mp4 && \ diff --git a/FlaskAPI/Dockerfile b/FlaskAPI/Dockerfile index d0caa71..436dd2e 100644 --- a/FlaskAPI/Dockerfile +++ b/FlaskAPI/Dockerfile @@ -6,7 +6,12 @@ ENV PYTHONUNBUFFERED=1 WORKDIR /app -RUN apt-get update && apt-get install -y imagemagick && apt-get install -y wget && ls +RUN apt-get update && \ + apt-get install -y wget && \ + wget https://github.com/protocolbuffers/protobuf/releases/download/v25.0/protoc-25.0-linux-x86_64.zip && \ + unzip protoc-25.0-linux-x86_64.zip && \ + mv bin/protoc /usr/local/bin/ && \ + rm -rf protoc-25.0-linux-x86_64.zip bin include readme.txt RUN wget https://files.ivanch.me/api/public/dl/iFuXSNhw/small-image.png && \ wget https://files.ivanch.me/api/public/dl/81Bkht5C/big-image.png && \ diff --git a/FlaskAPI/controllers/image.py b/FlaskAPI/controllers/image.py index 6fb10b9..f028550 100644 --- a/FlaskAPI/controllers/image.py +++ b/FlaskAPI/controllers/image.py @@ -5,20 +5,6 @@ from flask import request, Response, Blueprint, jsonify, send_file image_blueprint = Blueprint('image_blueprint', __name__) image_service = ImageService() -@image_blueprint.route('/image/blur', methods=['POST']) -def blur_image(): - radius = int(request.args.get('radius')) - image = request.get_data() - - if radius and image: - return send_file(io.BytesIO(image_service.box_blur_image(image, radius)), - mimetype='image/png', - as_attachment=True, - download_name='blurred_image.png') - - return "Bad request", 400 - - @image_blueprint.route('/image/load-small-image', methods=['GET']) def get_simple_image(): return send_file(io.BytesIO(image_service.get_simple_image()), diff --git a/FlaskAPI/controllers/simulation.py b/FlaskAPI/controllers/simulation.py index 9cde57f..239ec37 100644 --- a/FlaskAPI/controllers/simulation.py +++ b/FlaskAPI/controllers/simulation.py @@ -1,4 +1,5 @@ from flask import request, Blueprint, jsonify +import helloworld_pb2 simulation_blueprint = Blueprint('simulation_blueprint', __name__) @@ -28,3 +29,12 @@ def return_ok(): @simulation_blueprint.route('/simulation/json', methods=['GET']) def return_helloworld(): return simulation_controller.return_helloworld(), 200 + +@simulation_blueprint.route('/simulation/protobuf', methods=['POST']) +def return_protobuf(): + bytes_data = request.data + + helloworld = helloworld_pb2.MyObj() + helloworld.ParseFromString(bytes_data) + + return helloworld.SerializeToString(), 200 diff --git a/FlaskAPI/helloworld.proto b/FlaskAPI/helloworld.proto new file mode 100644 index 0000000..c35e7c8 --- /dev/null +++ b/FlaskAPI/helloworld.proto @@ -0,0 +1,5 @@ +syntax = "proto3"; + +message MyObj { + string message = 1; +} \ No newline at end of file diff --git a/FlaskAPI/helloworld_pb2.py b/FlaskAPI/helloworld_pb2.py new file mode 100644 index 0000000..4b917da --- /dev/null +++ b/FlaskAPI/helloworld_pb2.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: helloworld.proto +# Protobuf Python Version: 4.25.0 +"""Generated protocol buffer code.""" +from google.protobuf import descriptor as _descriptor +from google.protobuf import descriptor_pool as _descriptor_pool +from google.protobuf import symbol_database as _symbol_database +from google.protobuf.internal import builder as _builder +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + + + +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x10helloworld.proto\"\x18\n\x05MyObj\x12\x0f\n\x07message\x18\x01 \x01(\tb\x06proto3') + +_globals = globals() +_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) +_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'helloworld_pb2', _globals) +if _descriptor._USE_C_DESCRIPTORS == False: + DESCRIPTOR._options = None + _globals['_MYOBJ']._serialized_start=20 + _globals['_MYOBJ']._serialized_end=44 +# @@protoc_insertion_point(module_scope) diff --git a/FlaskAPI/requirements.txt b/FlaskAPI/requirements.txt index 20ce748..8be1675 100644 --- a/FlaskAPI/requirements.txt +++ b/FlaskAPI/requirements.txt @@ -1,3 +1,3 @@ Flask>=1.0 gunicorn>=19.9.0 -Wand \ No newline at end of file +protobuf>=4.25.0 \ No newline at end of file diff --git a/FlaskAPI/services/image.py b/FlaskAPI/services/image.py index 2ced7c5..4a981c1 100644 --- a/FlaskAPI/services/image.py +++ b/FlaskAPI/services/image.py @@ -1,49 +1,7 @@ -from wand.image import Image, Color - -def box_blur_image_separable(image, radius_x, radius_y): - width, height = image.width, image.height - kernel_x_size = 2 * radius_x + 1 - kernel_y_size = 2 * radius_y + 1 - kernel_area = kernel_x_size * kernel_y_size - - blurred_image = image.clone() - - for y in range(height): - for x in range(width): - r_total, g_total, b_total = 0, 0, 0 - - for offset_y in range(-radius_y, radius_y + 1): - for offset_x in range(-radius_x, radius_x + 1): - new_x = x + offset_x - new_y = y + offset_y - - if 0 <= new_x < width and 0 <= new_y < height: - pixel = image[new_x, new_y] - r_total += pixel.red_int8 - g_total += pixel.green_int8 - b_total += pixel.blue_int8 - - r_avg = int(r_total / kernel_area) - g_avg = int(g_total / kernel_area) - b_avg = int(b_total / kernel_area) - - blurred_image[x, y] = Color(f'rgb({r_avg},{g_avg},{b_avg})') - - return blurred_image - class ImageService: def __init__(self): pass - def box_blur_image(self, img, radius): - with Image(blob=img) as image: - image.gaussian_blur(radius, radius) - return image.make_blob() - - # blurred_image = box_blur_image_separable(image, radius, 0) - # blurred_image = box_blur_image_separable(blurred_image, 0, radius) - # return blurred_image.make_blob() - def get_simple_image(self): with open("./static/small-image.png", "rb") as file: return file.read()