python - 麻烦与代码-> Python识别报纸上的面孔

标签 python opencv python-imaging-library tesseract

我的任务是遍历包含一些图像的zip文件,每个图像都是报纸的一页,目标是在页面中搜索单词并显示该页面中识别出的所有面孔。

我将数据存储在字典中,其中的键是图像文件的名称,值是,首先是pytesseract生成的文本,其次是该图像的ZipInfo对象。

生成字典的函数工作正常,因为它生成了我想要的东西,但是问题出在其他两个函数中,wordCheck()和detectFaces()是因为返回了一个空列表

这是代码:

def getDict(zippedFile):
    '''
    This function gets a .zip file containing images and returns a dictionary whose keys are the image names
    and the values are the images text content.
    '''
    j = 0
    i = 0
    dic = {}
    contents = []
    imageObject = []
    with zipfile.ZipFile(zippedFile) as file:
        for single_info in file.infolist():
            imageObject.append(single_info)
            with file.open(single_info) as imageInfo:
                img = Image.open(imageInfo)
                text = pytesseract.image_to_string(img)
                contents.append(text)
    for name in file.namelist():
        dic[name] = [contents[j], imageObject[j]]
        j += 1

    return dic

def detectFaces(imageName, dic):
    '''
    This function gets and image name, that is in a .zip file and returns a list containing bounding boxes for
    faces detected on the given image.
   '''
    boundingBoxes = []
    with zipfile.ZipFile('readonly/small_img.zip') as file:
        imageInfo = dic[imageName][1]
        PILImage = Image.open(imageInfo)
        display(PILImage)
        img = cv.imread(PILImage)
        gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        print(gray)
        faceBoxes = face_cascade.detectMultiScale(gray)

    for item in faceBoxes:
        print(item)
        boundingBoxes.append(item[0])

    return boundingBoxes

def checkWord(word, dic):
    '''

    '''
    bBoxes = []
    for key in dic:
        if word in dic[key]:
            print('Results found in {}'.format(key))
            bBoxes.append(detectFaces(key, dic))
    return bBoxes 


dictera = getDict('readonly/small_img.zip')
result = checkWord('Senator', dictera)
print(result)

我是编程方面的新手,所以如果我犯了一个愚蠢的错误,请原谅我!
我不知道下一步该怎么做,你们对发生的事情有头绪吗?

最佳答案

还有其他一些问题,并且已经解决了,但弄清楚我要返回的列表从未提及。杜尔!

关于python - 麻烦与代码-> Python识别报纸上的面孔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61207231/

相关文章:

linux - cmake 错误 : opencv2/opencv. hpp:opencv2/opencv.hpp:No such file or directory

numpy - 将恒星图像缩小为具有半径的恒星坐标(Numpy/OpenCV)

python - 为什么 print 不能与 python 2/3 中的 for 一起使用在同一行?

python - 如何在 Google App Engine 中对来自 webapp WSGI 应用程序的响应进行单元测试?

python - 我怎样才能让代码检查所有情况?

c++ - OpenCV cvtColor 图像被截断

c++ - OpenCV - 使用 findHomography 的 RobustMatcher

python - 设置图像的 alpha 值(通过 PIL/Pillow)

python - 为什么使用 PIL 调整 JPEG 图像的大小会增加文件大小?

python - 如何为分类值列表的列创建嵌入