Page 268 - MDP2020-3
P. 268
262
| 인천전자마이스터고등학교 ·············································································································
ledNums = [False, False, False, False, False]
cotSet = 0
face_dirs = 'faces/'
face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
def face_extractor(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces == ():
return None
for (x, y, w, h) in faces:
cropped_face = img[y:y + h, x:x + w]
# 리턴!
return cropped_face
def take_pictures(dName):
# 해당 이름의 폴더가 없다면 생성
if not isdir(face_dirs + dName):
makedirs(face_dirs + dName)
# 카메라 ON
cap = cv2.VideoCapture(0)
count = 0
while True:
# 카메라로 부터 사진 한장 읽어 오기
ret, faceFrame = cap.read()
# 사진에서 얼굴 검출 , 얼굴이 검출되었다면
if face_extractor(faceFrame) is not None:
count += 1
face = cv2.resize(face_extractor(faceFrame), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
file_name_path = face_dirs + name + '/user' + str(count) + '.jpg'
cv2.imwrite(file_name_path, face)
cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1,
(0, 255, 0), 2)