import os
import sys
sys.path.append('../')
# Import necessary packages
from modules import json_parser
from modules import Client
# Initialise Client object
client = Client(api_key='996c5ec910a35bc0813ba5f12f4ba667ba10af41ecd4c9dd0ff9aa5cbfc0439a',
project_id='5cd04ee51afd94639a492b8e', user_ID='5ca1c6991afd941dd93a4052',
project_type='app', source_file_path='handler.py',
silent=True)
# Make run/train/predict command alias for further use
run = client.run
train = client.train
predict = client.predict
# Make controller alias for further use
controller = client.controller
import os
GPUID='0' ##调用GPU序号
os.environ["CUDA_VISIBLE_DEVICES"] = GPUID
import torch
from apphelper.image import xy_rotate_box,box_rotate,solve
import model
import time
from PIL import Image
import cv2
import numpy as np
def plot_box(img,boxes):
blue = (0, 0, 0) #18
tmp = np.copy(img)
for box in boxes:
cv2.rectangle(tmp, (int(box[0]),int(box[1])), (int(box[2]), int(box[3])), blue, 1) #19
return Image.fromarray(tmp)
def plot_boxes(img,angle, result,color=(0,0,0)):
tmp = np.array(img)
c = color
h,w = img.shape[:2]
thick = int((h + w) / 300)
i = 0
if angle in [90,270]:
imgW,imgH = img.shape[:2]
else:
imgH,imgW= img.shape[:2]
for line in result:
cx =line['cx']
cy = line['cy']
degree =line['degree']
w = line['w']
h = line['h']
x1,y1,x2,y2,x3,y3,x4,y4 = xy_rotate_box(cx, cy, w, h, degree/180*np.pi)
x1,y1,x2,y2,x3,y3,x4,y4 = box_rotate([x1,y1,x2,y2,x3,y3,x4,y4],angle=(360-angle)%360,imgH=imgH,imgW=imgW)
cx =np.mean([x1,x2,x3,x4])
cy = np.mean([y1,y2,y3,y4])
cv2.line(tmp,(int(x1),int(y1)),(int(x2),int(y2)),c,1)
cv2.line(tmp,(int(x2),int(y2)),(int(x3),int(y3)),c,1)
cv2.line(tmp,(int(x3),int(y3)),(int(x4),int(y4)),c,1)
cv2.line(tmp,(int(x4),int(y4)),(int(x1),int(y1)),c,1)
mess=str(i)
cv2.putText(tmp, mess, (int(cx), int(cy)),0, 1e-3 * h, c, thick // 2)
i+=1
return Image.fromarray(tmp).convert('RGB')
import re
import base64
from io import BytesIO
from keras.preprocessing import image
def image_to_base64(img):
buffered = BytesIO()
resize_img = image.array_to_img(img)
resize_img.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue())
return img_str.decode("utf-8")
# def base64_to_image(base64_str, grayscale):
# base64_data = re.sub('^data:image/.+;base64,', '', base64_str)
# byte_data = base64.b64decode(base64_data)
# image_data = BytesIO(byte_data)
# img = image.load_img(image_data, grayscale)
# # img.save('hahhaha.jpg')
# return image.img_to_array(img)
def base64_to_image(base64_code):
base64_code = re.sub('^data:image/.+;base64,', '', base64_code)
# base64解码
img_data = base64.b64decode(base64_code)
# 转换为np数组
img_array = np.fromstring(img_data, np.uint8)
# 转换成opencv可用格式
img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)
return img
def handle(conf):
"""
该方法是部署之后,其他人调用你的服务时候的处理方法。
请按规范填写参数结构,这样我们就能替你自动生成配置文件,方便其他人的调用。
范例:
params['key'] = value # value_type: str # description: some description
参数请放到params字典中,我们会自动解析该变量。
"""
import time
from PIL import Image
base64_str = conf['img'] # value_type: str # description: some description
img = base64_to_image(base64_str)
h,w = img.shape[:2]
timeTake = time.time()
_,result,angle= model.model(img,
detectAngle=True,##是否进行文字方向检测
config=dict(MAX_HORIZONTAL_GAP=50,##字符之间的最大间隔,用于文本行的合并
MIN_V_OVERLAPS=0.6,
MIN_SIZE_SIM=0.6,
TEXT_PROPOSALS_MIN_SCORE=0.1,
TEXT_PROPOSALS_NMS_THRESH=0.3,
TEXT_LINE_NMS_THRESH = 0.7,##文本行之间测iou值
),
leftAdjust=True, ##对检测的文本行进行向左延伸
rightAdjust=True, ##对检测的文本行进行向右延伸
alph=0.01, ##对检测的文本行进行向右、左延伸的倍数
)
timeTake = time.time()-timeTake
# print('It take:{}s'.format(timeTake))
take_time = '耗时:'+ str(timeTake) + 's'
output = ''
for line in result:
# print(line['text'])
output += line['text']
return {'take_time': take_time, 'output': output}
# conf={}
# handle(conf)