Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ch_pp-ocrv3 #2174

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion modules/image/text_recognition/ch_pp-ocrv3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
27 changes: 25 additions & 2 deletions modules/image/text_recognition/ch_pp-ocrv3/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@
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="[email protected]",
type="cv/text_recognition")
class ChPPOCRv3:

def __init__(self, text_detector_module=None, enable_mkldnn=False):
"""
initialize with the necessary elements
Expand Down Expand Up @@ -477,3 +478,25 @@ def add_module_input_arg(self):
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)
2 changes: 0 additions & 2 deletions modules/image/text_recognition/ch_pp-ocrv3/requirements.txt

This file was deleted.

63 changes: 28 additions & 35 deletions modules/image/text_recognition/ch_pp-ocrv3/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

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'
Expand All @@ -34,13 +35,13 @@ def test_recognize_text1(self):
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]]
}])

Expand All @@ -50,13 +51,13 @@ def test_recognize_text2(self):
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]]
}])

Expand All @@ -66,13 +67,13 @@ def test_recognize_text3(self):
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]]
}])

Expand All @@ -82,29 +83,21 @@ def test_recognize_text4(self):
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')
Expand Down
14 changes: 14 additions & 0 deletions modules/image/text_recognition/ch_pp-ocrv3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import base64
import math
from io import BytesIO

import cv2
import numpy as np
Expand Down Expand Up @@ -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