mirror of
https://github.com/sml2h3/ddddocr.git
synced 2025-05-04 16:50:25 +08:00
Merge pull request #33 from lededev/mtp
classification() add image type Image.Image and pathlib.Path
This commit is contained in:
commit
97f23ada2f
@ -5,6 +5,7 @@ warnings.filterwarnings('ignore')
|
|||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import base64
|
import base64
|
||||||
|
import pathlib
|
||||||
import onnxruntime
|
import onnxruntime
|
||||||
from PIL import Image, ImageChops
|
from PIL import Image, ImageChops
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -1580,13 +1581,20 @@ class DdddOcr(object):
|
|||||||
return []
|
return []
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def classification(self, img_bytes: bytes = None, img_base64: str = None):
|
def classification(self, img):
|
||||||
if self.det:
|
if self.det:
|
||||||
raise TypeError("当前识别类型为目标检测")
|
raise TypeError("当前识别类型为目标检测")
|
||||||
if img_bytes:
|
if not isinstance(img, (bytes, str, pathlib.PurePath, Image.Image)):
|
||||||
image = Image.open(io.BytesIO(img_bytes))
|
raise TypeError("未知图片类型")
|
||||||
|
if isinstance(img, bytes):
|
||||||
|
image = Image.open(io.BytesIO(img))
|
||||||
|
elif isinstance(img, Image.Image):
|
||||||
|
image = img.copy()
|
||||||
|
elif isinstance(img, str):
|
||||||
|
image = base64_to_image(img)
|
||||||
else:
|
else:
|
||||||
image = base64_to_image(img_base64)
|
assert isinstance(img, pathlib.PurePath)
|
||||||
|
image = Image.open(img)
|
||||||
image = image.resize((int(image.size[0] * (64 / image.size[1])), 64), Image.ANTIALIAS).convert('L')
|
image = image.resize((int(image.size[0] * (64 / image.size[1])), 64), Image.ANTIALIAS).convert('L')
|
||||||
image = np.array(image).astype(np.float32)
|
image = np.array(image).astype(np.float32)
|
||||||
image = np.expand_dims(image, axis=0) / 255.
|
image = np.expand_dims(image, axis=0) / 255.
|
||||||
|
Loading…
Reference in New Issue
Block a user