moist_wrist3
moist_wrist3

Reputation: 31

Python face detection not working in terminal

I am trying to make a face detection python programme using opencv but when i run the following shell command I get the following error:

when i typed: python main2.py abba.png haarcascade_frontalface_default.xml

I am using opencv and I used the following documentationRealpython face Recognition:

(venv) C:\Users\User\PycharmProjects\main_1>python face_detect.py abba.png haarcascade_frontalface_default.xml
C:\Users\User\AppData\Local\Programs\Python\Python39\python.exe: can't open file 'C:\Users\User\PycharmProjects\main_1\face_detect.py': [Errno 2] No such file or directory

(venv) C:\Users\User\PycharmProjects\main_1>python main2.py abba.png haarcascade_frontalface_default.xml
Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\main_1\main2.py", line 13, in <module>
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-95hbg2jt\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src
.empty() in function 'cv::cvtColor'

Here is the Code I used:

import sys
import cv2

# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

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

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)

print("Found {0} faces!".format(len(faces)))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow("Faces found", image)
cv2.waitKey(0)

Upvotes: 0

Views: 1672

Answers (2)

sefiks
sefiks

Reputation: 1630

You can apply face detection within deepface. It wraps several face detectors. Here, mtcnn is the most robust but the slowest one. ssd is the fastest. The framework will download required dependencies in the background.

#!pip install deepface
from deepface import DeepFace
detectors = ['opencv', 'ssd', 'mtcnn', 'dlib']
img = DeepFace.detectFace("img.jpg", detector_backend = detectors[0])

Upvotes: 1

Gabriel Pellegrino
Gabriel Pellegrino

Reputation: 1122

This error indicates that the image file wasn't found correctly:

C:\Users\User\AppData\Local\Programs\Python\Python39\python.exe: can't open file 'C:\Users\User\PycharmProjects\main_1\face_detect.py': [Errno 2] No such file or directory

And thus it can't convert color using opencv of something (image) that's not a valid file.

File "C:\Users\User\PycharmProjects\main_1\main2.py", line 13, in <module>
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-95hbg2jt\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src
.empty() in function 'cv::cvtColor'

So, check if your folder contains the file you want to use, in your case: abba.png.

Use the command dir to list all the directories and files in a directory using windows cmd; ls is a unix command.

Upvotes: 1

Related Questions