ai_platform_cv/run.py

186 lines
6.0 KiB
Python
Raw Permalink Normal View History

2022-08-03 10:16:48 +08:00
# -*-coding:utf-8-*-
import os
import sys
from logzero import logger
2022-12-07 10:46:43 +08:00
2022-12-08 14:39:23 +08:00
2022-12-07 10:46:43 +08:00
# current_path = os.path.dirname(__file__) # for local
2022-12-08 14:39:23 +08:00
current_path = "/app" # for docker
2022-12-07 10:46:43 +08:00
logger.info(f"{current_path}")
2022-08-03 10:16:48 +08:00
sys.path.append(f"{current_path}/text2image/")
sys.path.append(f"{current_path}/text2image/BigGAN_utils/")
2022-12-07 10:46:43 +08:00
2022-08-03 10:16:48 +08:00
import json
import base64
2022-12-07 10:46:43 +08:00
from PIL import Image
2022-08-03 10:16:48 +08:00
from flask import Flask, request, make_response
import cv2
2022-12-07 10:46:43 +08:00
from io import BytesIO
import torch
2022-08-03 10:16:48 +08:00
from mmpose.apis import init_pose_model
2022-12-07 10:46:43 +08:00
from text2image.run_text2img import text2image
from detection.detection import detector
from segmentation.segment_pred import run_seg
from ocr.ocr import run_tr
2022-08-03 10:16:48 +08:00
from backbone.backbone_infer import run_backbone_infer
2022-12-07 10:46:43 +08:00
from mnist.mnist_torch import run_mnist_infer, CNN
2022-08-03 10:16:48 +08:00
2022-12-08 14:39:23 +08:00
2022-08-03 10:16:48 +08:00
DEVICE = 'cpu'
2022-12-08 14:39:23 +08:00
TEXT = "text"
BASE64_IMG = "base64_img"
2022-08-03 10:16:48 +08:00
2022-12-08 14:39:23 +08:00
model_5x = torch.hub.load(f'{current_path}/detection/yolov5/', 'custom',
path=f'{current_path}/detection/models/yolov5x.pt', source='local')
model_5s = torch.hub.load(f'{current_path}/detection/yolov5/', 'custom',
path=f'{current_path}/detection/models/yolov5s.pt', source='local')
2022-12-07 10:46:43 +08:00
model_seg = torch.load(f'{current_path}/segmentation/models/best_model_pvgc.pth', map_location=DEVICE)
2022-08-03 10:16:48 +08:00
pose_config_file = f'{current_path}/backbone/associative_embedding_hrnet_w32_coco_512x512.py'
pose_ckpt_file = f'{current_path}/backbone/models/hrnet_w32_coco_512x512-bcb8c247_20200816.pth'
2022-12-07 10:46:43 +08:00
pose_model = init_pose_model(pose_config_file, pose_ckpt_file, device=DEVICE)
mnist_model = torch.load(f'{current_path}/mnist/models/MNIST_torch.pth', map_location=DEVICE)
2022-08-03 10:16:48 +08:00
2022-12-08 14:39:23 +08:00
app = Flask(__name__)
2022-08-03 10:16:48 +08:00
2022-12-08 14:39:23 +08:00
@app.route('/text2image/', methods=["POST"])
2022-12-07 10:46:43 +08:00
def run_text2img():
2022-12-08 14:39:23 +08:00
resp_info = dict()
2022-12-07 10:46:43 +08:00
if request.method == "POST":
text = request.form.get('text')
logger.info(f"{text}")
img = text2image(text)
output_buffer = BytesIO()
img.save(output_buffer, format='png')
byte_data = output_buffer.getvalue()
b64_code = base64.b64encode(byte_data).decode('utf-8')
2022-12-08 14:39:23 +08:00
resp_info["code"] = 200
resp_info["data"] = b64_code
resp_info["dtype"] = BASE64_IMG
2022-12-08 14:55:10 +08:00
resp = make_response(json.dumps(resp_info))
2022-12-08 14:39:23 +08:00
resp.status_code = 200
return resp
2022-08-03 10:16:48 +08:00
2022-12-07 10:46:43 +08:00
@app.route('/detection/', methods=["POST"])
def run_detection():
2022-12-08 14:39:23 +08:00
resp_info = dict()
2022-12-07 10:46:43 +08:00
if request.method == "POST":
img = request.files.get('image')
model_type = request.form.get('model_type')
try:
img = cv2.imread(img)
2022-12-08 14:39:23 +08:00
except Exception as e:
resp_info["msg"] = e
resp_info["code"] = 406
2022-12-07 10:46:43 +08:00
else:
2022-12-08 14:39:23 +08:00
if model_type.lower().strip() == 'yolov5x':
rst, _ = detector(img, model_5x)
else:
rst, _ = detector(img, model_5s)
logger.info(rst.shape)
img_str = cv2.imencode('.png', rst)[1].tobytes()
b64_code = base64.b64encode(img_str).decode('utf-8')
resp_info["code"] = 200
resp_info["data"] = b64_code
resp_info["dtype"] = BASE64_IMG
2022-12-08 14:55:10 +08:00
resp = make_response(json.dumps(resp_info))
2022-12-08 14:39:23 +08:00
resp.status_code = 200
return resp
2022-08-03 10:16:48 +08:00
2022-12-07 10:46:43 +08:00
@app.route('/ocr/', methods=["POST"])
def run_ocr():
2022-12-08 14:39:23 +08:00
resp_info = dict()
2022-12-07 10:46:43 +08:00
if request.method == "POST":
img = request.files.get('image')
try:
img = cv2.imread(img)
2022-12-08 14:39:23 +08:00
except Exception as e:
resp_info["msg"] = e
resp_info["code"] = 406
else:
text = run_tr(img)
resp_info["code"] = 200
resp_info["data"] = text
resp_info["dtype"] = TEXT
2022-12-08 14:55:10 +08:00
resp = make_response(json.dumps(resp_info))
2022-12-08 14:39:23 +08:00
resp.status_code = 200
return resp
2022-08-03 10:16:48 +08:00
2022-12-07 10:46:43 +08:00
@app.route('/segmentation/', methods=["POST"])
def run_segmentation():
2022-12-08 14:39:23 +08:00
resp_info = dict()
2022-12-07 10:46:43 +08:00
if request.method == "POST":
img_upload = request.files.get('image')
try:
img = cv2.imread(img_upload)
2022-12-08 14:39:23 +08:00
except Exception as e:
resp_info["msg"] = e
resp_info["code"] = 406
else:
result = run_seg(img, model_seg)
img_str = cv2.imencode('.png', result)[1].tobytes()
b64_code = base64.b64encode(img_str).decode('utf-8')
resp_info["code"] = 200
resp_info["data"] = b64_code
resp_info["dtype"] = BASE64_IMG
2022-12-08 14:55:10 +08:00
resp = make_response(json.dumps(resp_info))
2022-12-08 14:39:23 +08:00
resp.status_code = 200
return resp
2022-08-03 10:16:48 +08:00
@app.route('/backbone/', methods=["POST"])
def run_backbone():
2022-12-08 14:39:23 +08:00
resp_info = dict()
2022-08-03 10:16:48 +08:00
if request.method == "POST":
img_upload = request.files.get('image')
try:
img = cv2.imread(img_upload)
2022-12-08 14:39:23 +08:00
except Exception as e:
resp_info["msg"] = e
resp_info["code"] = 406
else:
_, result = run_backbone_infer(img, pose_model)
img_str = cv2.imencode('.png', result)[1].tobytes()
b64_code = base64.b64encode(img_str).decode('utf-8')
resp_info["code"] = 200
resp_info["data"] = b64_code
resp_info["dtype"] = BASE64_IMG
2022-12-08 14:55:10 +08:00
resp = make_response(json.dumps(resp_info))
2022-12-08 14:39:23 +08:00
resp.status_code = 200
return resp
2022-08-03 10:16:48 +08:00
2022-12-07 10:46:43 +08:00
@app.route('/mnist/', methods=["POST"])
def run_mnist():
2022-12-08 14:39:23 +08:00
resp_info = dict()
2022-12-07 10:46:43 +08:00
if request.method == "POST":
img_upload = request.files.get('image')
try:
img = cv2.imread(img_upload, 1)
# 使用全局阈值,降噪
2022-12-08 14:39:23 +08:00
ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
2022-12-07 10:46:43 +08:00
# 把opencv图像转化为PIL图像
2022-12-08 14:39:23 +08:00
im = Image.fromarray(cv2.cvtColor(th1, cv2.COLOR_BGR2RGB))
2022-12-07 10:46:43 +08:00
# 灰度化
im = im.convert('L')
Im = im.resize((28, 28), Image.ANTIALIAS)
2022-12-08 14:39:23 +08:00
except Exception as e:
resp_info["msg"] = e
resp_info["code"] = 406
else:
result = run_mnist_infer(Im, mnist_model)
resp_info["code"] = 200
resp_info["data"] = str(result)
resp_info["dtype"] = TEXT
2022-12-08 14:55:10 +08:00
resp = make_response(json.dumps(resp_info))
2022-12-08 14:39:23 +08:00
resp.status_code = 200
return resp
2022-12-07 10:46:43 +08:00
2022-08-03 10:16:48 +08:00
if __name__ == '__main__':
2022-12-08 14:39:23 +08:00
app.run(host='0.0.0.0', port='8902')