pytesseract 无法按预期识别文本?

2024-04-24

我正在尝试通过 opencv 和 pytesseract 运行一个简单的车牌图像来获取文本,但我无法从中获取任何内容。按照此处的教程进行操作:

https:// Circuitdigest.com/microcontroller-projects/license-plate-recognition-using-raspberry-pi-and-opencv https://circuitdigest.com/microcontroller-projects/license-plate-recognition-using-raspberry-pi-and-opencv

我在 macbook 上运行,所有东西都安装在 anaconda 中,据我所知,没有错误,但是当我运行代码时,我得到了裁剪后的图像,但没有检测到数字:

(computer_vision) mac@x86_64-apple-darwin13 lpr % python explore.py
Detected Number is: 

代码如下:

import cv2
import numpy as np
import imutils
import pytesseract

img = cv2.imread('plate1.jpg')
img = cv2.resize(img, (620,480))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grey scale
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200) #Perform Edge detection

cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None

# loop over our contours
for c in cnts:
                # approximate the contour
                peri = cv2.arcLength(c, True)
                approx = cv2.approxPolyDP(c, 0.018 * peri, True)
                # if our approximated contour has four points, then
                # we can assume that we have found our screen
                if len(approx) == 4:
                      screenCnt = approx
                      break

# Masking the part other than the number plate
mask = np.zeros(gray.shape,np.uint8)
new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
new_image = cv2.bitwise_and(img,img,mask=mask)

# Now crop
(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx+1, topy:bottomy+1]

#Read the number plate
text = pytesseract.image_to_string(Cropped, config='--psm 11')
print("Detected Number is:",text)


cv2.imshow('image',Cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()

Base image is here:enter image description here


你可以尝试不同的psm配置:

text = pytesseract.image_to_string(Cropped, config='--psm 3')

输出是:Detected Number is: PHR. 26.BR 9044;.

超立方体手册页: https://github.com/tesseract-ocr/tesseract/blob/master/doc/tesseract.1.asc

0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR. (not implemented)
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.
11 = Sparse text. Find as much text as possible in no particular order.
12 = Sparse text with OSD.
13 = Raw line. Treat the image as a single text line,
     bypassing hacks that are Tesseract-specific.
I don't know why `psm` `11` is not giving any output...  

看起来 psm 11 尚未发布(或者太新):

tesseract-ocr https://github.com/tesseract-ocr/tesseract/blob/8d6dbb133b41/api/tesseractmain.cpp#L115:

void PrintHelpForPSM() {
  const char* msg =
      "Page segmentation modes:\n"
        "  0    Orientation and script detection (OSD) only.\n"
        "  1    Automatic page segmentation with OSD.\n"
        "  2    Automatic page segmentation, but no OSD, or OCR.\n"
        "  3    Fully automatic page segmentation, but no OSD. (Default)\n"
        "  4    Assume a single column of text of variable sizes.\n"
        "  5    Assume a single uniform block of vertically aligned text.\n"
        "  6    Assume a single uniform block of text.\n"
        "  7    Treat the image as a single text line.\n"
        "  8    Treat the image as a single word.\n"
        "  9    Treat the image as a single word in a circle.\n"
        " 10    Treat the image as a single character.\n"

        //TODO: Consider publishing these modes.
        #if 0
        " 11    Sparse text. Find as much text as possible in no"
          " particular order.\n"
        " 12    Sparse text with OSD.\n"
        " 13    Raw line. Treat the image as a single text line,\n"
          "\t\t\tbypassing hacks that are Tesseract-specific.\n"
        #endif
        ;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pytesseract 无法按预期识别文本? 的相关文章

随机推荐

  • 是否使用 PHP 框架? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我已经开始用 PHP 编写一些应用程序 并且我对这门语言越来越熟悉 有人告诉我有关 CakePHP 和 CodeIgniter 的信息 我想更好
  • 将 pandas 数据框中的数字和字母字符串转换为 int/float

    我觉得必须有一个快速的解决方案来解决我的问题 我使用多个列表理解破解了一个实施不佳的解决方案 这无论如何都不理想 也许有人可以在这里帮忙 我有一组字符串值 例如 3 2B 1 5M 1 1T 其中最后一个字符自然表示百万 十亿 万亿 该集合
  • Ecto - 验证关联模型的存在

    如何验证 Ecto 中是否存在关联模型 schema foo do has many bar Bar timestamps end required fields w bar invalid 有办法这样做吗 并验证这些字段的最小 最大数量
  • Pandas:无法导入名称邻接

    来自韦斯 def side by side objs kwds from pandas core common import adjoin space kwds get space 4 reprs repr obj split n for
  • 通过引用传递变量并构造新对象

    你好 我有像下面这样的代码 但我不知道为什么它不起作用 class Clazz2 class Clazz public void smth Clazz2 c void smth2 const Clazz2 c class Clazz2 in
  • 升级到 webpack 4 后,Angular 应用程序中的 InjectionToken 配置没有

    我最近从 Webpack 2 升级到 4 Webpack 可以编译 并且大多数应用程序都可以正常工作 看来应用程序的一部分已损坏 我收到错误 NullInjectorError No provider for InjectionToken
  • 为什么 getView 在分离列表适配器上返回错误的 ConvertView 对象?

    我根据自己的需要改编了 Jeff Sharkey 的分离列表适配器 SeparatedListAdapter 得到了如下结果 public class SeparatedListAdapter
  • 如何在CSS中使div背景颜色透明

    我没有使用CSS3 所以我不能使用opacity or filter属性 如果不使用这些属性 我怎样才能使background color透明的一个div 它应该是这样的文本框示例link http www w3schools com cs
  • 在 Django 中创建员工用户

    我正在尝试在 Django 中创建一个员工用户 UserModel objects create user username A email email protected cdn cgi l email protection passwo
  • Android appcompat-v7:21.0.0 更改材质复选框颜色

    我已经更新了我的项目以使用最新的 appcompat 支持库 新版本使用材料设计复选框和单选按钮 我的应用程序是深色主题 复选框是黑色的 很难看到 我正在尝试根据以下内容更改它们的颜色保持兼容性 https developer androi
  • 存储过程中的条件 WHERE 子句

    这个问题可能归结为更简单的问题 但我仍然很好奇 SQL Server TSQL 能有多接近条件WHERE条款 以及为什么它们不存在背后的推理也很有趣 我有一个存储过程 对于一些参数 它接受一个枚举数组 它已相应地转换为用户定义的表类型 它本
  • 如果值包含逗号字符,如何使用 QSetting 读取值[重复]

    这个问题在这里已经有答案了 在我的 QT 项目中 我使用 QSettings 从 ini 文件读取值 如果该值包含逗号字符 QSettings 无法读取它 我应该如何读取这些值 逗号字符被视为列表分隔符QSettings 带逗号的 INI
  • 将关系 R 分解为 1NF 后最少存在多少张表?

    考虑具有以下属性类型的关系 R A B C D E F G 键总数 1 A 一组简单 或 原子 或 单值属性 B C 多值属性集 D E 复合属性集 F G 将关系 R 分解为 1NF 后 存在的表的最小数量是多少 A 3 B 2 C 4
  • 使用新文件名保存文件:附加到现有文件名

    有没有一种简单的方法 在 VIM 中 使用当前名称加上附加短语保存当前打开的文件 即 来自 home affert type vim data folder file1 txt 然后将文件另存为 data folder file1 txt
  • 在 Perl 中如何接受多个 TCP 连接?

    我对 Linux 的 Perl 脚本有疑问 它的主要目的是成为 3 个应用程序之间的中间人 它应该做什么 它应该能够等待 UDP 文本 不带空格 udp port 当它收到 UDP 文本时 它应该将其转发到连接的 TCP 客户端 问题是我的
  • 反序列化通用列表返回 null

    我正在反 序列化一个对象 如下所示 public class myClass ISerializable public List
  • 跨浏览器高度 100%(变换比例<1)

    我似乎无法找到一种方法使子容器的高度为父容器的 100 并且只能看到滚动条 相反 我们看到高度的空白量增加了一倍 这个问题与Javascript 放大 缩小到鼠标 x y 坐标 https stackoverflow com questio
  • 获取函数/类构造函数的参数类型

    我正在尝试做一些我不确定在 TypeScript 中是否可行的事情 从函数推断参数类型 返回类型 例如 function foo a string b number return a b type typeA
  • Visual Studio 2013 自动套用格式 (CTRL K D) cshtml 小写问题

    我最近才更新到 VS 2013 当使用 MVC 3 应用程序时 我遇到了自动格式化问题 即使用 CTRL K D 快捷键 或突出显示全部 格式选择 例如 model IEnumerable
  • pytesseract 无法按预期识别文本?

    我正在尝试通过 opencv 和 pytesseract 运行一个简单的车牌图像来获取文本 但我无法从中获取任何内容 按照此处的教程进行操作 https Circuitdigest com microcontroller projects