forked from PensPencil/Haar_Cascades
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_headless.py
More file actions
31 lines (23 loc) · 947 Bytes
/
test_headless.py
File metadata and controls
31 lines (23 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import cv2
import os
def test_headless():
cascade_path = 'face_detection/haarcascade_frontalface_default.xml'
img_path = 'face_detection/sample.jpg'
output_path = 'face_detection/test_output.jpg'
if not os.path.exists(cascade_path):
print(f"Error: Cagecade file not found.")
return
if not os.path.exists(img_path):
print(f"Error: Sample image not found.")
return
face_cascade = cv2.CascadeClassifier(cascade_path)
image = cv2.imread(img_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
print(f"Detected {len(faces)} faces.")
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imwrite(output_path, image)
print(f"Result saved to {output_path}")
if __name__ == "__main__":
test_headless()