一、ddddocr介绍
- ddddocr 新的目标检测识别 1.3功能
- 安装
pip install ddddocr
- star哲哥免费开源的识别项目https://github.com/sml2h3/ddddocr
![汉字目标点选识别-ddddocr_第1张图片](http://img.e-com-net.com/image/info8/79372b6145b44b59b2e9553f960a855a.jpg)
二、识别效果
![在这里插入图片描述](http://img.e-com-net.com/image/info8/25b2f0128e6d4337bddceb14d35e02e2.png)
![汉字目标点选识别-ddddocr_第2张图片](http://img.e-com-net.com/image/info8/2e072d005c174238af128dd77d7cca77.jpg)
三、代码
- 图片demo
![汉字目标点选识别-ddddocr_第3张图片](http://img.e-com-net.com/image/info8/ace1f3098e164ed28be107824487d5f4.jpg)
- 代码 ,更多详细介绍看ddddocr 新的目标检测识别 1.3功能
"""
@author:十一姐
@desc: ddddocr目标识别
@time: 2022/01/09
"""
from io import BytesIO
import ddddocr
from PIL import Image, ImageDraw, ImageFont
class Ddddocr:
def __init__(self):
self.ocr = ddddocr.DdddOcr(show_ad=False)
self.xy_ocr = ddddocr.DdddOcr(det=True, show_ad=False)
def ddddocr_identify(self, captcha_bytes):
return self.ocr.classification(captcha_bytes)
def draw_img(self, content, xy_list):
"""画出图片"""
font_type = "./msyhl.ttc"
font_size = 20
font = ImageFont.truetype(font_type, font_size)
img = Image.open(BytesIO(content))
draw = ImageDraw.Draw(img)
words = []
for row in xy_list:
x1, y1, x2, y2 = row
draw.line(([(x1, y1), (x1, y2), (x2, y2), (x2, y1), (x1, y1)]), width=1, fill="red")
corp = img.crop(row)
img_byte = BytesIO()
corp.save(img_byte, 'png')
word = self.ocr.classification(img_byte.getvalue())
words.append(word)
y = y1 - 30 if y2 > 300 else y2
draw.text((int((x1 + x2)/2), y), word, font=font, fill="red")
img.show()
return words
def ddddocr_clcik_identify(self, content, crop_size=None):
"""目标检测识别"""
img = Image.open(BytesIO(content))
if crop_size:
img = img.crop(crop_size)
img_byte = BytesIO()
img.save(img_byte, 'png')
content = img_byte.getvalue()
xy_list = self.xy_ocr.detection(content)
words = self.draw_img(content, xy_list)
return dict(zip(words, xy_list))
def case_demo(self, con):
"""点选识别结果"""
click_identify_result = self.ddddocr_clcik_identify(con, (0, 0, 344, 344))
img = Image.open(BytesIO(con))
img = img.crop((0, 344, 344, 384))
img_byte = BytesIO()
img.save(img_byte, 'png')
img_xy = {}
for key, xy in click_identify_result.items():
img_xy[key] = (int((xy[0] + xy[2]) / 2), int((xy[1] + xy[3]) / 2))
print(img_xy)
with open(r'./8320423853e84b499be0b81d40c7f259.jpg', 'rb') as f:
con1 = f.read()
Ddddocr().case_demo(con1)