Python中用于从图像中提取文本的8大OCR库

在Python中,有许多库可以用于从图像中提取文本,这些库通常被称为光学字符识别(OCR)工具。以下是其中的8个最常见的OCR库:

  1. Tesseract
  2. Google Vision
  3. Cuneiform
  4. Gnostic
  5. Pyocr
  6. Pytesseract
  7. Tesseract.js
  8. Tesseract OCR for iOS

解决方案和实例代码:

  1. Tesseract

    Tesseract是一个开源的OCR库,它可以通过多种语言进行训练,以识别不同语言的文本。它可以处理各种图像文件格式,包括jpg、png、gif、pdf和epdf。




from PIL import Image
import pytesseract
 
def ocr_image(image_path):
    img = Image.open(image_path)
    text = pytesseract.image_to_string(img)
    return text
 
print(ocr_image('path_to_your_image.jpg'))
  1. Google Vision

    Google Vision API是一种云服务,可以分析图像,以识别其内容,包括人脸和地标。它还可以识别文本,并以编程方式提供对识别的文本的访问。




from google.cloud import vision
import io
 
def detect_text(path):
    client = vision.ImageAnnotatorClient()
 
    with io.open(path, 'rb') as image_file:
        content = image_file.read()
 
    image = vision.types.Image(content=content)
 
    response = client.text_detection(image=image)
    texts = response.text_annotations
    print('\nTexts:')
 
    for text in texts:
        print('\n"{}"'.format(text.description.strip()))
 
        vertices = (['({},{})'.format(vertex.x, vertex.y) for vertex in text.bounding_poly.vertices])
 
        print('bounds: {}'.format(','.join(vertices)))
 
detect_text('path_to_your_image.jpg')
  1. Cuneiform

    Cuneiform是一个开源OCR库,可以识别80多种语言的文本。它可以处理各种图像文件格式,包括jpg、png、gif、pdf和epdf。




from cuneiform import Cuneiform
 
def ocr_image(image_path):
    cform = Cuneiform()
    with open(image_path, 'rb') as image_file:
        data = image_file.read()
    return cform.convert_file(data)
 
print(ocr_image('path_to_your_image.jpg'))
  1. Gnostic

    Gnostic是一个用于处理图像和文本数据的Python库。它可以帮助开发者更容易地处理和分析从图像中提取的文本。




from gnostic import Image
 
def ocr_image(image_path):
    with Image.open(image_path) as img:
        text = img.text(clean=True, x_tolerance=10)
    return text
 
print(ocr_image('path_to_your_image.jpg'))
  1. Pyocr

    Pyocr是Python的OCR工具。它可以用于在Python中读取扫描件。




import sys
import pyocr
import pyocr.builders
 
def ocr_image(image_path):
    tools = pyocr.get_available_tools()
    if len(tools) == 0:
        print("No OCR tool found")
        sys.exit(1)
 
    tool = tools[0]
    txt = tool.image_to_string(
        pyocr.open_image(image_path),
        lang='eng',
        builder=pyocr.builders.TextBuilder()
    )
    r
最后修改于:2024年08月24日 21:51

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日