Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从图像中获取文本_Python_Ocr - Fatal编程技术网

Python 如何从图像中获取文本

Python 如何从图像中获取文本,python,ocr,Python,Ocr,我想从图像中读取文本 我在Python中使用pytesseract 这是我的密码: 导入pytesseract 从PIL导入图像 pytesseract.pytesseract.tesseract_cmd=r“C:\Program Files\tesseract OCR\tesseract.exe” image=image.open(r'a.jpg') image.resize((150,50),image.antialas.save(“pic.jpg”) image=image.open(“p

我想从图像中读取文本

我在Python中使用
pytesseract

这是我的密码:

导入pytesseract
从PIL导入图像
pytesseract.pytesseract.tesseract_cmd=r“C:\Program Files\tesseract OCR\tesseract.exe”
image=image.open(r'a.jpg')
image.resize((150,50),image.antialas.save(“pic.jpg”)
image=image.open(“pic.jpg”)
captcha=pytesseract.image_to_字符串(image)。替换(“,”)。替换(“-”,”)。替换(“$”,”)

但是,它返回空字符串

正确的方法应该是什么


谢谢。

Tesseract用于对文本文档执行OCR。根据我的经验,这是很好的,但即使数据非常干净,也有点零碎

在这种情况下,您似乎正在尝试解决一个专门设计用于击败OCR软件的验证码您很可能无法使用Tesseract解决此问题,因为:

  • 它不是专门为这个设计的
  • 这种情况是对抗性的:
    • 该示例专门设计用于防止您尝试执行的操作
    • 如果你能让它工作,另一方可能会改变它,使它再次破裂
如果你想继续,我建议:

  • 在试图处理图像之前,先清理图像(你能得到一张可读性好的黑白图像吗?)
  • 使用大量示例训练您自己的识别网络

    • 我同意@Jon Betts的观点

    • tesseract在OCR中不是很强,只有在设置正确的二进制情况下才很好
    • 愚弄OCR的捕获 但是如果你真的需要这样做,你需要为它想出一个手动程序

      我专门为您提供的CAPTCHA类型创建了下面的代码(但它完全僵化,并没有针对所有情况进行通用/优化)

      psudo代码

      from PIL import Image
      import pytesseract
      import numpy as np
      import cv2
      
      
      img = cv2.imread('a.jpg')
      img = cv2.medianBlur(img, 3)
      # extract blue parts
      img2 = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8)
      cond = np.bitwise_and(img[:, :, 0] >= 100, img[:, :, 2] < 100)
      img2[np.where(cond)] = 255
      img = img2
      # delete the noise
      kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
      img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
      
      str1 = pytesseract.image_to_string(Image.fromarray(img),
                                             config='-c tessedit_char_whitelist=abcedfghijklmnopqrtuvwxyz0123456789 -oem 3 -psm 8')
      
      cv2.imwrite("frame.png", img)
      print(str1)
      
    • 应用中值模糊
    • 应用阈值仅获取蓝色(此阶段的二进制图像输出)
    • 应用开窗减少二值图像中的白色小像素
    • 使用以下选项将图像提供给tesseract:
      • 输出字符的有限白名单
      • OEM 3:tesseract+cube
      • PSM 8:每幅图像一个单词
    • 代码

      from PIL import Image
      import pytesseract
      import numpy as np
      import cv2
      
      
      img = cv2.imread('a.jpg')
      img = cv2.medianBlur(img, 3)
      # extract blue parts
      img2 = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8)
      cond = np.bitwise_and(img[:, :, 0] >= 100, img[:, :, 2] < 100)
      img2[np.where(cond)] = 255
      img = img2
      # delete the noise
      kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
      img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
      
      str1 = pytesseract.image_to_string(Image.fromarray(img),
                                             config='-c tessedit_char_whitelist=abcedfghijklmnopqrtuvwxyz0123456789 -oem 3 -psm 8')
      
      cv2.imwrite("frame.png", img)
      print(str1)
      


      要查看tesseract的完整选项,请键入以下命令
      tesseract--help extra
      或refere to

      我收到错误:
      pytesseract.pytesseract.TesseractError:(1,“错误,未知命令行参数'-psm')
      注释tesseract行并获取图像输出,使用命令行/终端尝试tesseract,如果仍然出现错误,则tesseract安装程序有问题