When detecting a face using openCV, ** "1. Which trained model (cascade file) should I use?" And "2. What should I do with the parameters of the detectMultiScale method?" ** I think it's far from confusing. I will. The above two are indispensable to improve the detection accuracy, and it is inefficient to rewrite and execute the program one by one. This is an introduction of how to specify parameters (without changing the program) and execute them to quickly improve the detection accuracy. The following are other reference articles about openCV.
-Install OpenCV 3.3 and Python 3.6 on Windows 10 with Anaconda -[Explanation for beginners] openCV face detection mechanism and practice (detectMultiScale) -Face detection from multiple image files with openCV and cut out and saved -Tips for efficiently detecting faces with large numbers of images with openCV
The environmental summary is as follows.
argparse is part of the Python standard library that helps you use program arguments. For more information, see Official Help and [Official Tutorial](https://docs.python.jp/3/howto/argparse. See html #).
It's a simple way to use argparse. Write this code and save it as "argparse_test01.py" ([Tutorial "Introduction of Positioning Arguments"](https://docs.python.jp/3/howto/argparse.html#introducing- Quoted from positional-arguments)).
import argparse
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square",
help="display a square of a given number",
type=int)
args = parser.parse_args()
print(args.square**2)
First, get help on the command line.
python argparse_test01.py --h
Then it will return a description of the defined parameters.
usage: argparse_test01.py [-h] square
positional arguments:
square display a square of a given number
optional arguments:
-h, --help show this help message and exit
Now let's run the program on the command line.
python argparse_test01.py 4
Then, the value "16" which is the square of "4" specified as a parameter is returned.
Put the parameters in an array as used in tensorflow tutorial It was.
argparse_test02.py
import argparse
#Basic model
FLAGS = None
def main():
print(FLAGS.parameter1)
print(FLAGS.parameter2)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--parameter1',
type=float,
default=0.01,
help='Parameter1.'
)
parser.add_argument(
'--parameter2',
type=int,
default=2000,
help='Parameter2.'
)
FLAGS, unparsed = parser.parse_known_args()
main()
If you execute it with the following command, the parameter value will be output as it is.
python argparse_test02.py --parameter1 3.23 --parameter2 112
By the way, you can also define abbreviations by specifying two. In this case, you can also specify the parameter with "-p".
parser.add_argument(
'-p',
'--parameter',
type=int,
default=2000,
help='Parameter2.'
)
As I wrote at the beginning, "1. Trained model (cascade file)" and "2. Parameters of detectMultiScale method", and then parameterize the image file name. The code looks like this.
openCVWithParameter01.py
import cv2
import argparse
#Basic model parameters
FLAGS = None
#Types of trained models
cascade = ["default","alt","alt2","tree","profile","nose"]
#Pass if run directly(Imported and does not pass at runtime)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--cascade",
type=str,
default="default",
choices=cascade,
help="cascade file."
)
parser.add_argument(
"--image_file",
type=str,
default="cut_source0.jpg ",
help="image file."
)
parser.add_argument(
"--scale",
type=float,
default=1.3,
help="scaleFactor value of detectMultiScale."
)
parser.add_argument(
"--neighbors",
type=int,
default=2,
help="minNeighbors value of detectMultiScale."
)
parser.add_argument(
"--min",
type=int,
default=30,
help="minSize value of detectMultiScale."
)
#Parameter acquisition and execution
FLAGS, unparsed = parser.parse_known_args()
#Classifier directory(Obtained from)
# https://github.com/opencv/opencv/blob/master/data/haarcascades/
# https://github.com/opencv/opencv_contrib/blob/master/modules/face/data/cascades/
if FLAGS.cascade == cascade[0]:#"default":
cascade_path = "./models/haarcascade_frontalface_default.xml"
elif FLAGS.cascade == cascade[1]:#"alt":
cascade_path = "./models/haarcascade_frontalface_alt.xml"
elif FLAGS.cascade == cascade[2]:#"alt2":
cascade_path = "./models/haarcascade_frontalface_alt2.xml"
elif FLAGS.cascade == cascade[3]:#"tree":
cascade_path = "./models/haarcascade_frontalface_alt_tree.xml"
elif FLAGS.cascade == cascade[4]:#"profile":
cascade_path = "./models/haarcascade_profileface.xml"
elif FLAGS.cascade == cascade[5]:#"nose":
cascade_path = "./models/haarcascade_mcs_nose.xml"
#Files used and I / O directories
image_path = "./inputs/" + FLAGS.image_file
output_path = "./outputs/" + FLAGS.image_file
#For directory confirmation(For when things go wrong)
#import os
#print(os.path.exists(image_path))
#File reading
image = cv2.imread(image_path)
#Grayscale conversion
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#Acquire the features of the cascade classifier
cascade = cv2.CascadeClassifier(cascade_path)
#Execution of object recognition (face recognition)
#image - CV_8U type matrix. Objects are detected in the images stored here
#objects -A vector whose elements are rectangles. Each rectangle contains the detected object
#scaleFactor -Represents the amount of reduction at each image scale
#minNeighbors -The rectangles that are candidate objects must contain at least this number of neighboring rectangles.
#flags -This parameter is not used in the new cascade. For older cascades, it has the same meaning as for the cvHaarDetectObjects function.
#minSize -The minimum size that an object can take. Objects smaller than this are ignored
facerect = cascade.detectMultiScale(image_gray, scaleFactor=FLAGS.scale, minNeighbors=FLAGS.neighbors, minSize=(FLAGS.min, FLAGS.min))
#print(facerect)
color = (255, 255, 255) #White
#When detected
if len(facerect) > 0:
#Create a rectangle that surrounds the detected face
for rect in facerect:
cv2.rectangle(image, tuple(rect[0:2]),tuple(rect[0:2]+rect[2:4]), color, thickness=2)
#Saving recognition results
cv2.imwrite(output_path, image)
All you have to do is run it. This will reduce the time and effort of executing the program while rewriting it, and should improve efficiency! ** **
python openCVWithParameter01.py --scale 1.3 --neighbors 1 --image_file "cut_source1.jpg " --cascade "nose" --min 50
Of course, you can execute the default value without specifying any parameters.
python openCVWithParameter01.py
Recommended Posts