Page 270 - MDP2020-3
P. 270
264
| 인천전자마이스터고등학교 ·············································································································
# 폴더만 색출
model_dirs = [f for f in listdir(data_path) if isdir(join(data_path, f))]
# 학습 모델 저장할 딕셔너리
models = {}
# 각 폴더에 있는 얼굴들 학습
for model in model_dirs:
print('학습 시작함 :' + model)
# 학습 시작
result = train(model)
# 학습이 안되었다면 패스!
if result is None:
print('학습 실패함 :' + model)
continue
# 학습되었으면 저장
print('학습 완료함 :' + model)
models[model] = result
# 학습된 모델 딕셔너리 리턴
return models
# 얼굴 검출
def face_detector(img, size=0.5):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces == ():
return img, []
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)
roi = img[y:y + h, x:x + w]
roi = cv2.resize(roi, (200, 200))
return img, roi # 검출된 좌표에 사각 박스 그리고(img), 검출된 부위를 잘라(roi) 전달
# 인식 시작
def run(models):
global faceCnt
# 카메라 열기
cap = cv2.VideoCapture(0)
while True:
# 카메라로 부터 사진 한장 읽기