Page 374 - MDP2020-3
P. 374

368
                      |    인천전자마이스터고등학교  ·············································································································

                    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:

                            #  카메라로  부터  사진  한장  읽기
                            ret,  frame  =  cap.read()
                            #  얼굴  검출  시도
                            image,  face  =  face_detector(frame)
                            try:

                                    min_score  =  999    #  가장  낮은  점수로  예측된  사람의  점수
                                    min_score_name  =  ""    #  가장  높은  점수로  예측된  사람의  이름


                                    #  검출된  사진을  흑백으로  변환
                                    face  =  cv2.cvtColor(face,  cv2.COLOR_BGR2GRAY)



                                    #  위에서  학습한  모델로  예측시도
                                    for  key,  model  in  models.items():
                                            result  =  model.predict(face)
                                            if  min_score  >  result[1]:

                                                    min_score  =  result[1]
                                                    min_score_name  =  key


                                    #  min_score  신뢰도이고  0에  가까울수록  자신과  같다는  뜻이다.
                                    if  min_score  <  500:

                                            confidence  =  int(100  *  (1  -  min_score  /  300))
                                    if  confidence  >  75:    #  해당  사람일  때
                                            print(min_score_name)
                                            faceCnt  +=  1
   369   370   371   372   373   374   375   376   377   378   379