首页 > 解决方案 > Python pytesseract 问题在 Mac 中打开“[Errno 13] Permission denied:”

问题描述

我在 python 中遇到了 pytesseract 的问题。当我在代码中调用文件时,它说权限被拒绝。我在 Mac 上工作,所以我不知道该怎么做,但我希望你能帮助我。

#from every single image-based cell/box the strings are extracted via pytesseract and stored in a list
outer=[]
for i in range(len(finalboxes)):
    for j in range(len(finalboxes[i])):
        inner=''
        if(len(finalboxes[i][j])==0):
            outer.append(' ')
        else:
            for k in range(len(finalboxes[i][j])):
                y,x,w,h = finalboxes[i][j][k][0],finalboxes[i][j][k][1], finalboxes[i][j][k][2],finalboxes[i][j][k][3]
                finalimg = bitnot[x:x+h, y:y+w]
                kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1))
                border = cv2.copyMakeBorder(finalimg,2,2,2,2,   cv2.BORDER_CONSTANT,value=[255,255])
                resizing = cv2.resize(border, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
                dilation = cv2.dilate(resizing, kernel,iterations=1)
                erosion = cv2.erode(dilation, kernel,iterations=1)

                
                out = pytesseract.image_to_string(erosion) # This line is the one that gives the error
                if(len(out)==0):
                    out = pytesseract.image_to_string(erosion, config='--psm 3')
                inner = inner +" "+ out
            outer.append(inner)

谢谢

标签: pythonmacospermissions

解决方案


您是否正在使用 pytesseract 的 tesseract 可执行文件的分配?由于我没有在您的代码中看到它,因此我假设您没有。我还将假设您使用 Homebrew 来安装 tesseract OCR。如果您使用 macports 或在其他操作系统上使用,请在此处查看官方安装文档。您的整个脚本至少应包含以下内容:

  1. pytesseract 模块的导入。

import pytesseract

  1. 对 tesseract 可执行文件的 pytesseract 模块实例的赋值。这必须引用路径和 tesseract 可执行二进制文件才能充分发挥作用:

pytesseract.pytesseract.tesseract_cmd = r'/usr/local/Cellar/tesseract/[version]/bin/tesseract'

  1. 您现在可以调用 pytesseract 方法的任何方法,例如:

print(pytesseract.image_to_string(r'/Path/to/image/you/want/to/process/Picture1.png'))


推荐阅读