ПРОБЛЕМА С КОДОМ -> Python распознавание лиц в газете - PullRequest
1 голос
/ 14 апреля 2020

Мое задание - перебирать 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)

Я очень новичок ie в программировании, поэтому, если я сделал глупую ошибку, простите меня! Я понятия не имею, что попробовать дальше. Ребята, вы понимаете, что происходит?

1 Ответ

0 голосов
/ 14 апреля 2020

Имел некоторые другие проблемы и уже решил их, но понял, что я возвращаю список, который никогда не упоминается. Duur!

...