Using OpenCV detectMultiScale to find my face

2024/9/20 13:54:08

I'm pretty sure I have the general theme correct, but I'm not finding any faces. My code reads from c=cv2.VideoCapture(0), i.e. the computer's videocamera. I then have the following set up to yield where the faces are. As you can see, I'm looping through different scaleFactors and minNeighbors but rects always comes back empty. I've also tried each of the four different haarcascade xml files included in the opencv/data/haarcascades package.

Any tips?

while(1):ret, frame = c.read()rects = find_face_from_img(frame)def detect(img, cascade):for scale in [float(i)/10 for i in range(11, 15)]:for neighbors in range(2,5):rects = cascade.detectMultiScale(img, scaleFactor=scale, minNeighbors=neighbors,minSize=(20, 20), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)print 'scale: %s, neighbors: %s, len rects: %d' % (scale, neighbors, len(rects))def find_face_from_img(img):gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)gray = cv2.equalizeHist(gray)rects = detect(gray, cascade)
Answer

I changed your code a little in order to make it run on my pc. When I run is at such I get results

import cv2
import cv2.cv as cv
import getopt, sysdef detect(img, cascade):for scale in [float(i)/10 for i in range(11, 15)]:for neighbors in range(2,5):rects = cascade.detectMultiScale(img, scaleFactor=scale, minNeighbors=neighbors,minSize=(20, 20), flags=cv2.cv.CV_HAAR_SCALE_IMAGE)print 'scale: %s, neighbors: %s, len rects: %d' % (scale, neighbors, len(rects))def find_face_from_img(img):gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)gray = cv2.equalizeHist(gray)rects = detect(gray, cascade)if __name__ == '__main__':args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])try: video_src = video_src[0]except: video_src = 0args = dict(args)cascade_fn = args.get('--cascade', "cascades/haarcascade_frontalface_alt.xml")cascade = cv2.CascadeClassifier(cascade_fn)c=cv2.VideoCapture(0)while(1):ret, frame = c.read()rects = find_face_from_img(frame)if 0xFF & cv2.waitKey(5) == 27:break

Output:

scale: 1.2, neighbors: 2, len rects: 1
scale: 1.2, neighbors: 3, len rects: 1
scale: 1.2, neighbors: 4, len rects: 1
scale: 1.3, neighbors: 2, len rects: 1
scale: 1.3, neighbors: 3, len rects: 1
scale: 1.3, neighbors: 4, len rects: 0
scale: 1.4, neighbors: 2, len rects: 1
scale: 1.4, neighbors: 3, len rects: 0
scale: 1.4, neighbors: 4, len rects: 0
scale: 1.1, neighbors: 2, len rects: 1
scale: 1.1, neighbors: 3, len rects: 1
scale: 1.1, neighbors: 4, len rects: 1
scale: 1.2, neighbors: 2, len rects: 1
scale: 1.2, neighbors: 3, len rects: 1
scale: 1.2, neighbors: 4, len rects: 1
scale: 1.3, neighbors: 2, len rects: 1

Some advice: Don't pick your minSize too low ... else every small item which resembles a face will be detected.

I assume you are running through all these parameters to find the ones that are the best. I found out the minNeighors shouldn't be too high, else it won't find any.

Make sure your cascade xml file is linked to correctly. If it doesn't find it, it won't give an error, it will just find no faces.

https://en.xdnf.cn/q/72342.html

Related Q&A

Get marginal effects for sklearn logistic regression

I want to get the marginal effects of a logistic regression from a sklearn modelI know you can get these for a statsmodel logistic regression using .get_margeff(). Is there nothing for sklearn? I want…

How to use win32com.client.constants with MS Word?

Whats wrong with this code? Why win32com.client.constants doesnt have attribute wdWindowStateMinimize?>>> import win32com.client >>> w=win32com.client.Dispatch("Word.Applicatio…

How to properly patch boto3 calls in unit test

Im new to Python unit testing, and I want to mock calls to the boto3 3rd party library. Heres my stripped down code:real_code.py:import boto3def temp_get_variable(var_name):return boto3.client(ssm).ge…

import a github into jupyter notebook directly?

Hey Im creating a jupyter notebook, would like to install: https://github.com/voice32/stock_market_indicators/blob/master/indicators.py which is a python program not sure how to do it directly so anybo…

Django : Call a method only once when the django starts up

I want to initialize some variables (from the database) when Django starts. I am able to get the data from the database but the problem is how should I call the initialize method . And this should be o…

Mocking instance attributes

Please help me understand why the following doesnt work. In particular - instance attributes of a tested class are not visible to Pythons unittest.Mock.In the example below bar instance attribute is no…

Are there any good 3rd party GUI products for Python? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

not able to get root window resize event

im trying to display the size(dimension) of the root window (top level window) on a label. whenever the user resize the window, new window dimensions should be displayed on the label. I tried to bind t…

Inverting large sparse matrices with scipy

I have to invert a large sparse matrix. I cannot escape from the matrix inversion, the only shortcut would be to just get an idea of the main diagonal elements, and ignore the off-diagonal elements (Id…

Error with tweepy OAuthHandler

Im new here and kind of unexperienced with python, so sorry if the question is trivial.I have this simple script, to fetch followers of a given twitter user:import time import tweepyconsumer_key="…