-1

Note I am a beginner. I made a script that analyzes a picture and places a box around any faces found in the image, that part works, What i need it to do is change the "faces" in "if faces = True" to something to the effect of If faces found = true, though I don't know what that would be, faces does nothing.

import cv2
import sys
import time

imagePath = sys.argv[1]
cascPath = sys.argv[2]

faceCascade = cv2.CascadeClassifier(cascPath)

image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(30, 30))#,flags = cv2.cv.CV_HAAR_IMAGE_SCALE)

for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

if faces = True:
    cv2.imshow("(1) Pamela Found" ,image)
else:
    cv2.imshow("(0) Pamela's Found" ,image)

cv2.waitKey(0)&0xFF

the code already works it just that the:

if faces = True:
        cv2.imshow("(1) Pamela Found" ,image)
    else:
        cv2.imshow("(0) Pamela's Found" ,image)

doesn't work. Help would be appreciated - Thanks!

Edit: now I have changed the code to look like this:

import cv2
import sys
import time

imagePath = sys.argv[1]
cascPath = sys.argv[2]

faceCascade = cv2.CascadeClassifier(cascPath)

image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(30, 30))#,flags = cv2.cv.CV_HAAR_IMAGE_SCALE)

for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

if faces == True:
    cv2.imshow("(1) Pamela(s) Found" ,image)
    cv2.waitKey(0)&0xFF
else:
    cv2.imshow("(0) Pamela(s) Found" ,image)
    cv2.waitKey(0)&0xFF

When I run this, the XML file and the image without the face, it works, and says, "(0) Pamela(s) Found" as it should, but when I run this, the XML file, and the image with the face the window doesn't pop up, I believe this has to do with the waitkey under the if statement not functioning, help would be appreciated - thanks!

tcwissemann
  • 1
  • 1
  • 4
  • based on `for (x, y, w, h) in faces:`, I'm guessing `faces` is a [list](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists). What you probably want is something like `if len(faces) > 0` – Hamms Aug 15 '16 at 20:33

2 Answers2

0

Use faces as the condition:

if faces: # python types can be coerced to boolean
    cv2.imshow("(1) Pamela Found" ,image)
else:
    cv2.imshow("(0) Pamela's Found" ,image)

An empty list (or container) has a falsy value, while if a face was detected (i.e. faces is not empty), the iterable, faces will have a truthy value.


P.S. if faces = True will raise a Syntax error and if you intended if faces == True, that also reduces to, and is better written as if faces.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
0

According to the documentation I found in OpenCV the faceCascade.detectMultiScale return a collection of objects.

To test is a collection (list, set, tuple, dict, etc.) is non-empty, just try:

if faces:
    cv2.imshow("(1) Pamela Found", image)
else:
    cv2.imshow("(0) Pamela's Found", image)

May be a duplicate of Best way to check if a list is empty

Community
  • 1
  • 1
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103