votecount-ml-be / main.py
napatswift
Fix put text bug
e7d3991
raw
history blame
1.24 kB
from mmocr.ocr import MMOCR
import gradio as gr
import cv2
model_dir = 'model'
ocr = MMOCR(det_config=f'{model_dir}/config.py',
det_ckpt=f'{model_dir}/epoch_40.pth', device='cpu')
def get_rec(points):
xs = []
ys = []
for ix, iv in enumerate(points):
if ix % 2:
ys.append(iv)
else:
xs.append(iv)
return (min(xs), min(ys)), (max(xs), max(ys))
def predict(image_input, score_threshold):
draw_img = image_input.copy()
print(image_input.shape)
output = ocr.readtext(image_input)
polygons = output['det_polygons']
scores = output['det_scores']
for polygon, score in zip(polygons, scores):
if score < score_threshold:
continue
p0, p1 = get_rec([int(i) for i in polygon])
draw_img = cv2.rectangle(draw_img, p0, p1, (255,0,0), 2)
draw_img = cv2.putText(draw_img, '%.3f' % score, p0, cv2.FONT_HERSHEY_PLAIN, 2)
return draw_img
def run():
demo = gr.Interface(
fn=predict,
inputs=[gr.components.Image(), gr.Slider(0, 1, 0.8)],
outputs=gr.components.Image(),
)
demo.launch(server_name="0.0.0.0", server_port=7860)
if __name__ == "__main__":
run()