adding flask protobuf

This commit is contained in:
José Henrique 2023-11-04 00:41:12 -03:00
parent ac8bff31f2
commit e8ae8935f2
8 changed files with 49 additions and 59 deletions

View File

@ -2,7 +2,7 @@ FROM rust:latest
WORKDIR /app 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/iFuXSNhw/small-image.png && \
wget https://files.ivanch.me/api/public/dl/81Bkht5C/big-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 && \ wget https://files.ivanch.me/api/public/dl/nAndfAjK/video.mp4 && \

View File

@ -6,7 +6,12 @@ ENV PYTHONUNBUFFERED=1
WORKDIR /app 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 && \ 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 && \ wget https://files.ivanch.me/api/public/dl/81Bkht5C/big-image.png && \

View File

@ -5,20 +5,6 @@ from flask import request, Response, Blueprint, jsonify, send_file
image_blueprint = Blueprint('image_blueprint', __name__) image_blueprint = Blueprint('image_blueprint', __name__)
image_service = ImageService() 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']) @image_blueprint.route('/image/load-small-image', methods=['GET'])
def get_simple_image(): def get_simple_image():
return send_file(io.BytesIO(image_service.get_simple_image()), return send_file(io.BytesIO(image_service.get_simple_image()),

View File

@ -1,4 +1,5 @@
from flask import request, Blueprint, jsonify from flask import request, Blueprint, jsonify
import helloworld_pb2
simulation_blueprint = Blueprint('simulation_blueprint', __name__) simulation_blueprint = Blueprint('simulation_blueprint', __name__)
@ -28,3 +29,12 @@ def return_ok():
@simulation_blueprint.route('/simulation/json', methods=['GET']) @simulation_blueprint.route('/simulation/json', methods=['GET'])
def return_helloworld(): def return_helloworld():
return simulation_controller.return_helloworld(), 200 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

View File

@ -0,0 +1,5 @@
syntax = "proto3";
message MyObj {
string message = 1;
}

View File

@ -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)

View File

@ -1,3 +1,3 @@
Flask>=1.0 Flask>=1.0
gunicorn>=19.9.0 gunicorn>=19.9.0
Wand protobuf>=4.25.0

View File

@ -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: class ImageService:
def __init__(self): def __init__(self):
pass 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): def get_simple_image(self):
with open("./static/small-image.png", "rb") as file: with open("./static/small-image.png", "rb") as file:
return file.read() return file.read()