For Haar cascade face detection, precision and recall are the most important metrics. Precision tells us how many detected faces are actually faces (few false alarms). Recall tells us how many real faces the detector finds (few misses). Since missing a face or wrongly detecting a face both matter, we want a good balance. The F1 score combines precision and recall to give one number showing overall detection quality.
Haar cascade face detection in Computer Vision - Model Metrics & Evaluation
| Predicted Face | Predicted No Face |
|----------------|-------------------|
| True Positive | False Positive |
| False Negative | True Negative |
Example:
TP = 80 (faces correctly detected)
FP = 20 (non-faces wrongly detected as faces)
FN = 15 (faces missed)
TN = 885 (non-faces correctly ignored)
Total samples = 80 + 20 + 15 + 885 = 1000
If the detector is set to be very sensitive, it finds almost all faces (high recall) but also detects many false faces (low precision). This means many false alarms, which can annoy users.
If the detector is strict, it only detects very clear faces (high precision) but misses some faces (low recall). This means some faces go undetected, which can be bad for security or photo tagging.
For example, in a photo app, missing a face (low recall) is worse because users want all faces found. So recall is more important. In a security camera, false alarms (low precision) can cause wasted attention, so precision matters more.
Good: Precision and recall both above 0.85 means most faces are found and few false alarms happen.
Bad: Precision below 0.5 means many false detections, recall below 0.5 means many faces missed.
For example, precision=0.9 and recall=0.9 is excellent. Precision=0.4 and recall=0.7 is poor because many false faces are detected.
- Accuracy paradox: If most images have no faces, accuracy can be high by always predicting no face, but the detector is useless.
- Data leakage: Testing on images used in training inflates metrics falsely.
- Overfitting: Detector works well on training images but poorly on new images, showing high training metrics but low real-world performance.
- Ignoring class imbalance: Faces are rare compared to non-faces, so metrics like accuracy can be misleading.
Your Haar cascade face detector has 98% accuracy but only 12% recall on faces. Is it good for production? Why or why not?
Answer: No, it is not good. The high accuracy is misleading because most images have no faces, so the detector guesses "no face" most times. The very low recall means it misses almost all faces, which defeats the purpose of face detection.
Practice
Solution
Step 1: Understand Haar cascade function
Haar cascade uses simple patterns to detect faces quickly in images.Step 2: Compare options with purpose
Only To quickly find faces in images using simple patterns matches the purpose of fast face detection using patterns.Final Answer:
To quickly find faces in images using simple patterns -> Option DQuick Check:
Haar cascade = Fast face detection [OK]
- Confusing Haar cascade with 3D modeling
- Thinking it changes image colors
- Assuming it compresses images
Solution
Step 1: Recall OpenCV syntax for Haar cascade loading
The correct method is cv2.CascadeClassifier() with the XML file path as argument.Step 2: Check each option's method name
Only face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') uses the exact method name and syntax from OpenCV documentation.Final Answer:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') -> Option AQuick Check:
Load Haar cascade = cv2.CascadeClassifier() [OK]
- Using incorrect method names like loadCascade or loadClassifier
- Missing parentheses after method name
- Passing wrong file path format
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
print(faces)
Solution
Step 1: Understand detectMultiScale output
detectMultiScale returns a list of rectangles for detected faces or an empty list if none found.Step 2: Check behavior when no faces detected
If no faces are found, it returns an empty list, not None or error.Final Answer:
An empty list [] -> Option BQuick Check:
No faces detected = empty list [] [OK]
- Expecting None instead of empty list
- Assuming it throws an error
- Thinking it returns a null value
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0))Solution
Step 1: Check input image type for detectMultiScale
detectMultiScale expects a grayscale image, but code uses 'image' which is likely color.Step 2: Review other parts for errors
cv2.rectangle can omit thickness (defaults to 1), variable names are consistent.Final Answer:
detectMultiScale should be called on gray image, not color image -> Option CQuick Check:
detectMultiScale needs grayscale input [OK]
- Passing color image directly to detectMultiScale
- Assuming rectangle needs thickness argument
- Using wrong variable names
detectMultiScale can help reduce false positives without missing faces?Solution
Step 1: Understand minNeighbors effect
Increasing minNeighbors requires more neighbor rectangles to confirm detection, reducing false positives.Step 2: Evaluate other options
Decreasing scaleFactor increases detection time and may increase false positives; smaller image reduces detail; minSize (0,0) allows all sizes, not reducing false positives.Final Answer:
IncreaseminNeighborsvalue -> Option AQuick Check:
Higher minNeighbors = fewer false positives [OK]
- Lowering scaleFactor to fix false positives
- Shrinking image size expecting better detection
- Setting minSize to zero ignoring face size
Ready to prove it?
Pass the test and this pattern moves unknown
