2023-07-06 07:32:59 +04:00
|
|
|
|
import base64
|
|
|
|
|
import cv2 as cv
|
|
|
|
|
from flask import Flask, redirect, request
|
|
|
|
|
import numpy
|
|
|
|
|
from imageWorking import get_image_buf_as_array
|
|
|
|
|
from main import analyze_base
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__, static_url_path = "/")
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
|
def main():
|
|
|
|
|
return redirect('index.html')
|
|
|
|
|
|
|
|
|
|
@app.route("/analyze", methods=["POST"])
|
|
|
|
|
def analyze():
|
2023-07-07 22:10:13 +04:00
|
|
|
|
# Первоначальные проверки.
|
2023-07-06 07:32:59 +04:00
|
|
|
|
if 'image' not in request.files or request.files['image'].filename == '':
|
|
|
|
|
return {
|
|
|
|
|
'success': False,
|
|
|
|
|
'error': 'Укажите изображение',
|
|
|
|
|
}
|
|
|
|
|
if 'ontology' in request.files and request.files['ontology'].filename != '':
|
|
|
|
|
return {
|
|
|
|
|
'success': False,
|
|
|
|
|
'error': 'Загрузка онтологии ещё не реализована',
|
|
|
|
|
}
|
2023-07-07 22:10:13 +04:00
|
|
|
|
|
|
|
|
|
# Подготовка исходного изображения.
|
|
|
|
|
image_source = request.files['image'].read();
|
|
|
|
|
image_source = numpy.fromstring(image_source, numpy.uint8)
|
|
|
|
|
image_source = get_image_buf_as_array(image_source)
|
|
|
|
|
|
|
|
|
|
# Подготовка прочих данных и выполнение запроса.
|
2023-07-06 07:32:59 +04:00
|
|
|
|
queries = [ 'QueryGetNotEmpty', 'QueryGetCheck', 'QueryGetEmpty' ]
|
2023-07-07 22:10:13 +04:00
|
|
|
|
results, response = analyze_base('5cc5570b-6ed9-3b33-9db4-bdb8ecb9f890', image_source, queries)
|
|
|
|
|
|
|
|
|
|
# Подготовка изображения с ответом.
|
|
|
|
|
image_result = results[0].plot()
|
|
|
|
|
image_result = cv.cvtColor(image_result, cv.COLOR_BGR2RGB)
|
|
|
|
|
image_result = cv.imencode(".jpg", image_result)[1]
|
|
|
|
|
image_result = base64.b64encode(image_result).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
# Вывод ответа.
|
2023-07-06 07:32:59 +04:00
|
|
|
|
return {
|
|
|
|
|
'success': True,
|
|
|
|
|
'data': response,
|
2023-07-07 22:10:13 +04:00
|
|
|
|
'image': image_result,
|
2023-07-06 07:32:59 +04:00
|
|
|
|
}
|