'Operators with Conditional Statements Numpy

I followed a facial detection algorithm linked here.

I found out that the program is pretty outdated, for example, using model.predict_classes instead of model.predict. After going through multiple StackOverflow and GitHub discussions, I can run the code without any difficulty, but then I encountered an error, The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() is the lines

if(lpred[0]==1):
    lbl='Open'
if(lpred[0]==0):
    lbl='Closed'

I changed this to

if lpred.all()==1:
    lbl='Open'
if lpred.all()==0:
    lbl='Closed'

I encountered the same problem in line

if (rpred[0]==0 and lpred[0]==0):

I changed it to

if np.logical_and((rpred.all()) == 0, (lpred.all()) == 0):

But now the code is not working. I thought this could be because I had initialized lpred and rpred as python list.

rpred=[99]
lpred=[99]

I changed this to this

rpred = np.array(99)
lpred = np.array(99)

But this issue remains the same.

if np.logical_and((rpred.all()) == 0, (lpred.all()) == 0):
    score=score+1
    cv2.putText(frame,"Closed",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)
    # print(score)
# if(rpred[0]==1 or lpred[0]==1):
else:
    score=score-1
    # print(score)
    cv2.putText(frame,"Open",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)

Some debugging helped in determining that the if condition is not running and only the else condition is executing as the score remains -1 in any case. I am unsure how to solve this.

The whole script can be found here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source