Page 372 - MDP2020-3
P. 372

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

                            #  카메라로  부터  사진  한장  읽어  오기
                            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)
                                    cv2.imshow('Face  Cropper',  face)
                            else:
                                    print("얼굴이  검출되지  않았습니다")

                                    pass
                            if  count  ==  100:
                                    break
                    cap.release()
                    cv2.destroyAllWindows()

                    print(str(count)  +  "장의  사진  수집을  완료했습니다")




            def  train(pName):
                    data_path  =  'faces/'  +  pName  +  '/'

                    #  파일만  리스트로  만듬
                    face_pics  =  [f  for  f  in  listdir(data_path)  if  isfile(join(data_path,  f))]


                    Training_Data,  Labels  =  [],  []



                    for  i,  files  in  enumerate(face_pics):
                            image_path  =  data_path  +  face_pics[i]
                            images  =  cv2.imread(image_path,  cv2.IMREAD_GRAYSCALE)
                            #  이미지가  아니면  패스
                            if  images  is  None:

                                    continue
                            Training_Data.append(np.asarray(images,  dtype=np.uint8))
                            Labels.append(i)
                    if  len(Labels)  ==  0:
   367   368   369   370   371   372   373   374   375   376   377