TIL Day 66.

Jen Devver·2024년 5월 20일

내배캠 TIL

목록 보기
73/91

Django 최종 팀 프로젝트

DeepFace 내부 라이브러리 수정

  • stream() 함수를 수정하여 사용해보기로 함.
  • 함수 내부를 보니 perform_demography_analysis()analysis()가 우리 팀 프로젝트에 필요한 기능이라 이 함수들에 기반하여 새 함수 perform_age_analysis() 를 생성
  • perform_demography_analysis() 수정: 기존 이 함수의 경우 np.ndarray를 return
def perform_demography_analysis(
    enable_face_analysis: bool,
    img: np.ndarray,
    faces_coordinates: List[Tuple[int, int, int, int]],
    detected_faces: List[np.ndarray],
) -> int:

    if enable_face_analysis is False:
        return img
    for idx, (x, y, w, h) in enumerate(faces_coordinates):
        detected_face = detected_faces[idx]
        demographies = DeepFace.analyze(
            img_path=detected_face,
            actions=("age", "gender", "emotion"),
            detector_backend="skip",
            enforce_detection=False,
            silent=True,
        )

        if len(demographies) == 0:
            continue

        # safe to access 1st index because detector backend is skip
        demography = demographies[0]

        age = demography['age']
        print(demographies)
        print("age>>>>>>>", age)

    return age
  • analysis() 함수를 수정해서 perform_age_analysis() 함수 생성: 사용하지 않는 얼굴 인식 모델 제외, 키오스크에 필요없는 웹캠 화면 표시 제외, 인식 대기 시간 단축, 각 if 문의 조건 확인, 기존 return 값 None 대신 perform_demography_analysis() 에서 받아온 age 값 return
def perform_age_analysis(db_path: str,
    model_name="VGG-Face",
    detector_backend="opencv",
    distance_metric="cosine",
    enable_face_analysis=True,
    source=0,
    time_threshold=2,
    frame_threshold=2,):

    build_demography_models(enable_face_analysis=enable_face_analysis)

    # call a dummy find function for db_path once to create embeddings before starting webcam
    _ = search_identity(
        detected_face=np.zeros([224, 224, 3]),
        db_path=db_path,
        detector_backend=detector_backend,
        distance_metric=distance_metric,
        model_name=model_name,
    )

    freeze = False
    num_frames_with_faces = 0

    cap = cv2.VideoCapture(source)  # webcam
    while True:
        has_frame, img = cap.read()
        if not has_frame:
            print(">>>>>>>>>현재 카메라가 작동 가능하지 않습니다. 직원에게 문의해주세요.<<<<<<<<<<")

        faces_coordinates = []
        if freeze is False:
            faces_coordinates = grab_facial_areas(img=img, detector_backend=detector_backend)

            # we will pass img to analyze modules (identity, demography) and add some illustrations
            # that is why, we will not be able to extract detected face from img clearly
            detected_faces = extract_facial_areas(img=img, faces_coordinates=faces_coordinates)

            num_frames_with_faces = num_frames_with_faces + 1 if len(faces_coordinates) else 0

            freeze = num_frames_with_faces > 0 and num_frames_with_faces % frame_threshold == 0
            print(">>>>>>freeze가 False일 때: 카메라에서 벗어났을 때, 카메라가 가려져있을 때, freeze 발생 전 5초 때(time_threshold, frame_threshold = 2로 변경해서 2초) <<<<<<")

            if freeze:
                # add analyze results into img - derive from raw_img
                # img = highlight_facial_areas(img=img, faces_coordinates=faces_coordinates)

                # age, gender and emotion analysis 
                demo = perform_demography_analysis(
                    enable_face_analysis=enable_face_analysis,
                    img=img,
                    faces_coordinates=faces_coordinates,
                    detected_faces=detected_faces,
                )

                print("demo >>>>>>>>", demo)

                if demo:
                    break

    print("perform_age_analysis >>>>>>>", demo)
    return demo

AI 특강

  • OpenAI

튜터님 피드백

  • DeepFace와 gpt-4o 중 어느 것을 선택할지 : gpt-4o가 더 정확하다고 하면 이를 사용하는 것을 권장
  • 라이브러리 내부 수정 이렇게 하는 것 맞는지: 이렇게 함수형으로 만들어지는 라이브러리는 사용하고자 하는 함수를 생성해서 만드는 것. 상속하는 것 아님.
profile
발전 중...

0개의 댓글