-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_cut_folder.py
More file actions
71 lines (51 loc) · 2.33 KB
/
face_cut_folder.py
File metadata and controls
71 lines (51 loc) · 2.33 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import cv2
import time
# Haar Cascade 파일 경로
cascade_path = './haarcascade_frontalface_default.xml'
# 입력 디렉토리 경로
input_path = './image'
# 출력 디렉토리 경로
output_path = './1.detected_face'
save_count = []
# 입력 폴더 순회
for folder in os.listdir(input_path):
input_dir = os.path.join(input_path, folder)
# 디렉토리인지 확인
if os.path.isdir(input_dir):
# 출력 디렉토리 경로 생성
output_dir = os.path.join(output_path, folder)
# 출력 디렉토리가 없으면 생성
if not os.path.exists(output_dir):
os.makedirs(output_dir)
total_cuted_image = 0
# 입력 디렉토리의 이미지 파일들을 순회하면서 얼굴 검출
for filename in os.listdir(input_dir):
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
image_path = os.path.join(input_dir, filename)
try:
# 이미지 읽기
image = cv2.imread(image_path)
# 그레이스케일 변환
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 얼굴 검출용 CascadeClassifier 객체 생성
face_cascade = cv2.CascadeClassifier(cascade_path)
# 얼굴 검출
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.4, minNeighbors=5, minSize=(30, 30))
print(image_path)
for (x, y, w, h) in faces:
detected_face = image[y:y+h, x:x+w]
output_file_path = os.path.join(output_dir, filename)
cv2.imwrite(output_file_path, detected_face)
total_cuted_image += 1
except Exception as e:
print(f"Error processing image {image_path}: {str(e)}")
continue
save_count.append({input_dir : total_cuted_image})
# 완료 메시지 출력
print("---------------------------------")
print("얼굴 검출 및 저장이 완료되었습니다.")
for item in save_count:
for key, value in item.items():
print(f"{key}: {value}")
print("---------------------------------")