Spaces:
Sleeping
Sleeping
Commit
·
c534a01
1
Parent(s):
372a14a
Upload 6 files
Browse files- ocr/__init__.py +5 -0
- ocr/__pycache__/__init__.cpython-38.pyc +0 -0
- ocr/__pycache__/ocr.cpython-38.pyc +0 -0
- ocr/__pycache__/utils.cpython-38.pyc +0 -0
- ocr/ocr.py +45 -0
- ocr/utils.py +40 -0
ocr/__init__.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# time: 2022/10/17 13:03
|
| 3 |
+
# file: __init__.py.py
|
| 4 |
+
# author: fengchenggang
|
| 5 |
+
# email: chenggang.feng@ashermed.com
|
ocr/__pycache__/__init__.cpython-38.pyc
ADDED
|
Binary file (158 Bytes). View file
|
|
|
ocr/__pycache__/ocr.cpython-38.pyc
ADDED
|
Binary file (1.48 kB). View file
|
|
|
ocr/__pycache__/utils.cpython-38.pyc
ADDED
|
Binary file (1.15 kB). View file
|
|
|
ocr/ocr.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# time: 2022/10/17 13:04
|
| 3 |
+
# file: ocr.py
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
from paddleocr import PaddleOCR
|
| 7 |
+
from ocr_utils import draw_ocr, draw_ocr_box_txt
|
| 8 |
+
|
| 9 |
+
ocr = PaddleOCR(lang='ch',use_angle_cls=True)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def detect(image):
|
| 13 |
+
"""
|
| 14 |
+
文本检测
|
| 15 |
+
args:
|
| 16 |
+
image(array): numpy格式的图片 'RGB'
|
| 17 |
+
return(array):
|
| 18 |
+
检测后的图片 numpy格式 'RGB'
|
| 19 |
+
"""
|
| 20 |
+
result = ocr.ocr(image, rec=False)
|
| 21 |
+
im_show = draw_ocr(image, result)
|
| 22 |
+
|
| 23 |
+
return im_show
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def recognize(image, output_mode=0):
|
| 27 |
+
"""
|
| 28 |
+
文本识别
|
| 29 |
+
args:
|
| 30 |
+
image(array): numpy格式的图片 'RGB'
|
| 31 |
+
output_mode(int): 图片输出模式 [0|1]
|
| 32 |
+
return(array):
|
| 33 |
+
识别后的图片 numpy格式 'RGB'
|
| 34 |
+
"""
|
| 35 |
+
result = ocr.ocr(image)
|
| 36 |
+
boxes = [line[0] for line in result]
|
| 37 |
+
txts = [line[1][0] for line in result]
|
| 38 |
+
scores = [line[1][1] for line in result]
|
| 39 |
+
if output_mode == 0:
|
| 40 |
+
im_show = draw_ocr_box_txt(image, boxes, txts, scores)
|
| 41 |
+
elif output_mode == 1:
|
| 42 |
+
im_show = draw_ocr(image, boxes, txts, scores)
|
| 43 |
+
|
| 44 |
+
return im_show
|
| 45 |
+
|
ocr/utils.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# time: 2022/10/17 13:04
|
| 3 |
+
# file: utils.py
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def bytes_to_numpy(image_bytes, channels='BGR'):
|
| 11 |
+
"""
|
| 12 |
+
图片格式转换 bytes -> numpy
|
| 13 |
+
args:
|
| 14 |
+
image_bytes(str): 图片的字节流
|
| 15 |
+
channels(str): 图片的格式 ['BGR'|'RGB']
|
| 16 |
+
return(array):
|
| 17 |
+
转换后的图片
|
| 18 |
+
"""
|
| 19 |
+
_image_np = np.frombuffer(image_bytes, dtype=np.uint8)
|
| 20 |
+
image_np = cv2.imdecode(_image_np, cv2.IMREAD_COLOR)
|
| 21 |
+
if channels == 'BGR':
|
| 22 |
+
return image_np
|
| 23 |
+
elif channels == 'RGB':
|
| 24 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
|
| 25 |
+
return image_np
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def numpy_to_pic(image_path, image_np, channels='BGR'):
|
| 29 |
+
"""
|
| 30 |
+
保存图片
|
| 31 |
+
args:
|
| 32 |
+
image_path(str): 图片路径
|
| 33 |
+
image_np(array): numpy格式的图片数据
|
| 34 |
+
channels(str): 图片的格式 ['BGR'|'RGB']
|
| 35 |
+
"""
|
| 36 |
+
if channels == 'BGR':
|
| 37 |
+
cv2.imwrite(image_path, image_np)
|
| 38 |
+
elif channels == 'RGB':
|
| 39 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 40 |
+
cv2.imwrite(image_path, image_np)
|