some backend stuff (untested)
parent
d4d248ee17
commit
85de4ffd88
|
@ -1,24 +1,90 @@
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI, Request
|
||||||
from os import getenv
|
from os import getenv
|
||||||
import cv2 as cv
|
import cv2 as cv
|
||||||
from tools import math_transform
|
from tools import math_transform
|
||||||
|
import base64
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
openapi_url="/resources/openapi.json",
|
openapi_url="/resources/openapi.json",
|
||||||
docs_url="/resources/docs"
|
docs_url="/resources/docs"
|
||||||
) if getenv("ENV") == 'dev' else FastAPI()
|
) if getenv("ENV") == 'dev' else FastAPI()
|
||||||
|
|
||||||
|
# Image endpoints
|
||||||
|
|
||||||
@app.post("/image/math-transform")
|
@app.post("/image/math-transform")
|
||||||
def math_image_transform_grayscale(image_b64: str, latex: str):
|
async def math_image_transform_grayscale(
|
||||||
|
req: Request
|
||||||
|
):
|
||||||
|
|
||||||
|
image_b64: str = req.body['image_b64']
|
||||||
|
latex: str = req.body['latex']
|
||||||
|
autominmax: bool = req.body['autominmax']
|
||||||
|
heatmap_out: bool = req.body['heatmap_out']
|
||||||
|
|
||||||
img = cv.imdecode(image_b64)
|
img = cv.imdecode(image_b64)
|
||||||
|
|
||||||
transformed = img.copy()
|
transformed = img.copy()
|
||||||
func = math_transform.generate_func(latex)
|
|
||||||
|
func = math_transform.transform_func_from_latex(
|
||||||
|
latex,
|
||||||
|
["R", "G", "B", "A"]
|
||||||
|
)
|
||||||
|
|
||||||
|
alpha_on = len(img[0][0]) == 4
|
||||||
|
|
||||||
for i in range(len(img)):
|
for i in range(len(img)):
|
||||||
for j in range(len(img[i])):
|
for j in range(len(img[i])):
|
||||||
transformed[i][j] = func(inputs)
|
pix = img[i][j]
|
||||||
|
transformed[i][j] = func(
|
||||||
|
pix[2],
|
||||||
|
pix[1],
|
||||||
|
pix[0],
|
||||||
|
255 if alpha_on else pix[3]
|
||||||
|
)
|
||||||
|
|
||||||
|
transformed_b64 = base64.b64encode(
|
||||||
|
cv.imencode('.png', transformed)[1]
|
||||||
|
).decode(encoding='utf-8')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"transformed_b64": transformed
|
"img_b64": transformed_b64,
|
||||||
|
"legend": None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@app.post("/image/smooth-conv")
|
||||||
|
async def convolutional_smooth(
|
||||||
|
image_b64: str,
|
||||||
|
smoothing_radius: int
|
||||||
|
):
|
||||||
|
img = cv.imdecode(image_b64)
|
||||||
|
|
||||||
|
# build & apply smoothing conv kernel
|
||||||
|
k = []
|
||||||
|
for i in range(smoothing_radius):
|
||||||
|
k.append([1/(smoothing_radius**2) for i in range(smoothing_radius)])
|
||||||
|
kernel = np.array(k)
|
||||||
|
|
||||||
|
transformed = cv.filter2D(img, -1, kernel)
|
||||||
|
|
||||||
|
transformed_b64 = base64.b64encode(
|
||||||
|
cv.imencode('.png', transformed)[1]
|
||||||
|
).decode(encoding='utf-8')
|
||||||
|
|
||||||
|
return transformed_b64
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/image/heatmap")
|
||||||
|
async def intensity_heatmap(
|
||||||
|
image_b64: str,
|
||||||
|
smoothing_radius: int
|
||||||
|
):
|
||||||
|
img = cv.imdecode(image_b64)
|
||||||
|
|
||||||
|
transformed = cv.applyColorMap(img, cv.COLORMAP_JET)
|
||||||
|
|
||||||
|
transformed_b64 = base64.b64encode(
|
||||||
|
cv.imencode('.png', transformed)[1]
|
||||||
|
).decode(encoding='utf-8')
|
||||||
|
|
||||||
|
return transformed_b64
|
||||||
|
|
|
@ -1,14 +1,27 @@
|
||||||
|
from typing import List
|
||||||
from latex2sympy2 import latex2sympy
|
from latex2sympy2 import latex2sympy
|
||||||
|
|
||||||
def generate_func(latex_input: str) -> function:
|
def transform_func_from_latex(
|
||||||
|
latex_input: str,
|
||||||
|
arg_names: List[str]
|
||||||
|
):
|
||||||
latex_clean = latex_input.replace('\ ', '')
|
latex_clean = latex_input.replace('\ ', '')
|
||||||
res: str
|
|
||||||
try:
|
try:
|
||||||
res = latex2sympy(latex_clean)
|
expr = latex2sympy(latex_clean)
|
||||||
|
arg_list_string = ""
|
||||||
|
subst_dict_string = "{"
|
||||||
|
for i in range(len(arg_names)):
|
||||||
|
name = arg_names[i]
|
||||||
|
arg_list_string += f"{name}"
|
||||||
|
subst_dict_string += f'"{name}":{name}'
|
||||||
|
if i < len(arg_names) - 1:
|
||||||
|
arg_list_string += ","
|
||||||
|
subst_dict_string += ","
|
||||||
|
subst_dict_string += "}"
|
||||||
|
f: function = eval(f"""
|
||||||
|
lambda {arg_list_string}: expr.subs({subst_dict_string}).evalf()
|
||||||
|
""", {"expr": expr})
|
||||||
|
return f
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
def func(input: dict):
|
|
||||||
return eval(res)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue