Search code examples
python-3.xopencv3.0face-api

'bool' object not iterable


I am working on python3, opencv 3.4 and using Microsoft Azure's FaceAPI function 'CF.face.detect()' As far as I know, 'for loop' needs iterable object to run on like list but simple boolean is not iterable. Though 'res1' is a list I get this error.

TypeError: 'bool' object not iterable

Please help, Thanks in advance

Here is the code: import unittest import cognitive_face as CF from PIL import Image, ImageFont, ImageDraw import time import cv2 from time import strftime

CF.Key.set('') 
#print(CF.Key.get()) 
CF.BaseUrl.set('https://southeastasia.api.cognitive.microsoft.com/face/v1.0/')
#print(CF.BaseUrl.get())
"""Setup Person and Person Group related data."""
person_group_id = '' #id from training terminal
"""Unittest for `face.detect`."""

cap = cv2.VideoCapture('1.mp4') 
while(cap.isOpened()): 
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    print("\n\n ##########.... LOOKING FOR FACES ....##########  \n\n")
    res1 = []
    print(type(res1))
    res1 = CF.face.detect(cap)

    print('\n This is the res1:  ', res1) 
    c = len(res1)
    print('\nTOTAL FACES FOUND:', c) 

    detect_id = [] ##error was here so put exception
    for i in range(c):
        print("\n\n ##########.... DETECTING FACES ....##########  \n\n")
        print('\n This is i in range c', i, c)
        detect_id.append(res1[i]['faceId'])
        #print('\n\n detected faces id ', detect_id[i])

        width  = res1[i]['faceRectangle']['width'] 
        height = res1[i]['faceRectangle']['height']
        x      = res1[i]['faceRectangle']['left']
        y      = res1[i]['faceRectangle']['top']
################## IF ENDS #########################################################################
cv2.imshow('image',img)
    k = cv2.waitKey(100) & 0xff
    if k == 27:
            break
################ WHILE ENDS ####################################
cap.release()
cv2.destroyAllWindows()

Solution

  • @Jonasz is right, you should be detecting faces on images, meaning, in frames from your mp4 file.

    The method CF.face.detect expects an URI, so in the following code we'll write it to disk before pass it onto CF.face.detect:

    cap = cv2.VideoCapture('1.mp4') 
    count = 0  # <--
    while(cap.isOpened()): 
        ret, img = cap.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
        filename = "frame%d.jpg" % count  # <--
        cv2.imwrite(filename, img)  # <--
        count+=1  # <--
    
        print("\n\n ##########.... LOOKING FOR FACES ....##########  \n\n")
        res1 = []
        print(type(res1))
        res1 = CF.face.detect(filename)  # <--