未验证 提交 283be69c 编写于 作者: jm_12138's avatar jm_12138 提交者: GitHub

update ch_pp-ocrv3 (#2174)

上级 4382eee6
......@@ -163,6 +163,9 @@
print(r.json()["results"])
```
- ### Gradio App 支持
从 PaddleHub 2.3.1 开始支持使用链接 http://127.0.0.1:8866/gradio/ch_pp-ocrv3 在浏览器中访问 ch_pp-ocrv3 的 Gradio App。
## 五、更新历史
* 1.0.0
......@@ -173,6 +176,10 @@
移除 Fluid API
* 1.2.0
添加 Gradio APP 支持
- ```shell
$ hub install ch_pp-ocrv3==1.1.0
$ hub install ch_pp-ocrv3==1.2.0
```
......@@ -31,21 +31,22 @@ from .utils import base64_to_cv2
from .utils import draw_ocr
from .utils import get_image_ext
from .utils import sorted_boxes
from paddlehub.utils.utils import logger
from paddlehub.module.module import moduleinfo
from paddlehub.module.module import runnable
from paddlehub.module.module import serving
from paddlehub.utils.utils import logger
@moduleinfo(
name="ch_pp-ocrv3",
version="1.1.0",
version="1.2.0",
summary="The module can recognize the chinese texts in an image. Firstly, it will detect the text box positions \
based on the differentiable_binarization_chn module. Then it classifies the text angle and recognizes the chinese texts. ",
author="paddle-dev",
author_email="paddle-dev@baidu.com",
type="cv/text_recognition")
class ChPPOCRv3:
def __init__(self, text_detector_module=None, enable_mkldnn=False):
"""
initialize with the necessary elements
......@@ -477,3 +478,25 @@ class ChPPOCRv3:
Add the command input options
"""
self.arg_input_group.add_argument('--input_path', type=str, default=None, help="diretory to image")
def create_gradio_app(self):
import gradio as gr
def inference(image, use_gpu=False, box_thresh=0.5, text_thresh=0.5, angle_classification_thresh=0.9):
return self.recognize_text(paths=[image],
use_gpu=use_gpu,
output_dir=None,
visualization=False,
box_thresh=box_thresh,
text_thresh=text_thresh,
angle_classification_thresh=angle_classification_thresh)
return gr.Interface(inference, [
gr.Image(type='filepath'),
gr.Checkbox(),
gr.Slider(0, 1.0, 0.5, step=0.01),
gr.Slider(0, 1.0, 0.5, step=0.01),
gr.Slider(0, 1.0, 0.5, step=0.01)
], [gr.JSON(label='results')],
title='ch_pp-ocrv3',
allow_flagging=False)
......@@ -4,13 +4,14 @@ import unittest
import cv2
import requests
import paddlehub as hub
import paddlehub as hub
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
class TestHubModule(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
img_url = 'https://unsplash.com/photos/KTzZVDjUsXw/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MzM3fHx0ZXh0fGVufDB8fHx8MTY2MzUxMTExMQ&force=true&w=640'
......@@ -34,13 +35,13 @@ class TestHubModule(unittest.TestCase):
use_gpu=False,
visualization=False,
)
self.assertEqual(results[0]['data'], [
{
'text': 'GIVE.', 'confidence': 0.9509806632995605,
self.assertEqual(results[0]['data'], [{
'text': 'GIVE.',
'confidence': 0.9509768486022949,
'text_box_position': [[283, 162], [352, 162], [352, 202], [283, 202]]
},
{
'text': 'THANKS', 'confidence': 0.9943129420280457,
}, {
'text': 'THANKS',
'confidence': 0.9943074584007263,
'text_box_position': [[261, 202], [376, 202], [376, 239], [261, 239]]
}])
......@@ -50,13 +51,13 @@ class TestHubModule(unittest.TestCase):
use_gpu=False,
visualization=False,
)
self.assertEqual(results[0]['data'], [
{
'text': 'GIVE.', 'confidence': 0.9509806632995605,
self.assertEqual(results[0]['data'], [{
'text': 'GIVE.',
'confidence': 0.9509768486022949,
'text_box_position': [[283, 162], [352, 162], [352, 202], [283, 202]]
},
{
'text': 'THANKS', 'confidence': 0.9943129420280457,
}, {
'text': 'THANKS',
'confidence': 0.9943074584007263,
'text_box_position': [[261, 202], [376, 202], [376, 239], [261, 239]]
}])
......@@ -66,13 +67,13 @@ class TestHubModule(unittest.TestCase):
use_gpu=True,
visualization=False,
)
self.assertEqual(results[0]['data'], [
{
'text': 'GIVE.', 'confidence': 0.9509806632995605,
self.assertEqual(results[0]['data'], [{
'text': 'GIVE.',
'confidence': 0.9509768486022949,
'text_box_position': [[283, 162], [352, 162], [352, 202], [283, 202]]
},
{
'text': 'THANKS', 'confidence': 0.9943129420280457,
}, {
'text': 'THANKS',
'confidence': 0.9943074584007263,
'text_box_position': [[261, 202], [376, 202], [376, 239], [261, 239]]
}])
......@@ -82,29 +83,21 @@ class TestHubModule(unittest.TestCase):
use_gpu=False,
visualization=True,
)
self.assertEqual(results[0]['data'], [
{
'text': 'GIVE.', 'confidence': 0.9509806632995605,
self.assertEqual(results[0]['data'], [{
'text': 'GIVE.',
'confidence': 0.9509768486022949,
'text_box_position': [[283, 162], [352, 162], [352, 202], [283, 202]]
},
{
'text': 'THANKS', 'confidence': 0.9943129420280457,
}, {
'text': 'THANKS',
'confidence': 0.9943074584007263,
'text_box_position': [[261, 202], [376, 202], [376, 239], [261, 239]]
}])
def test_recognize_text5(self):
self.assertRaises(
AttributeError,
self.module.recognize_text,
images=['tests/test.jpg']
)
self.assertRaises(AttributeError, self.module.recognize_text, images=['tests/test.jpg'])
def test_recognize_text6(self):
self.assertRaises(
AssertionError,
self.module.recognize_text,
paths=['no.jpg']
)
self.assertRaises(AssertionError, self.module.recognize_text, paths=['no.jpg'])
def test_save_inference_model(self):
self.module.save_inference_model('./inference/model')
......
......@@ -18,6 +18,7 @@ from __future__ import print_function
import base64
import math
from io import BytesIO
import cv2
import numpy as np
......@@ -189,4 +190,17 @@ def base64_to_cv2(b64str):
data = base64.b64decode(b64str.encode('utf8'))
data = np.fromstring(data, np.uint8)
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
if data is None:
buf = BytesIO()
image_decode = base64.b64decode(b64str.encode('utf8'))
image = BytesIO(image_decode)
im = Image.open(image)
rgb = im.convert('RGB')
rgb.save(buf, 'jpeg')
buf.seek(0)
image_bytes = buf.read()
data_base64 = str(base64.b64encode(image_bytes), encoding="utf-8")
image_decode = base64.b64decode(data_base64)
img_array = np.frombuffer(image_decode, np.uint8)
data = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
return data
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册