frames = pipeline.wait_for_frames() color_frame = frames.get_color_frame() bgr_image = np.asanyarray(color_frame.get_data()) gaze.refresh(bgr_image) bgr_image = gaze.annotated_frame() text = "" if gaze.is_right(): text = "Looking mono chino de pelo morado" elif gaze.is_left(): text = "Looking mona china" elif gaze.is_up(): text = "Looking mono chino rubio intenso" elif gaze.is_down(): text = "Looking logo" elif gaze.is_center(): text = "Looking mono chino de pelo verde" cv2.putText(bgr_image, text, (30, 30), cv2.FONT_HERSHEY_DUPLEX, 1, (147, 58, 31), 2) left_pupil = gaze.pupil_left_coords() right_pupil = gaze.pupil_right_coords() gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY) rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB) faces = detector(rgb_image) for face_coordinates in faces:
def run(models): #카메라 열기 cap = cv2.VideoCapture(0) gaze = GazeTracking() cheat_cnt = 0 right_cnt = 0 left_cnt = 0 up_cnt = 0 down_cnt = 0 name = "" while True: #카메라로 부터 사진 한장 읽기 ret, frame = cap.read() gaze.refresh(frame) # 얼굴 검출 시도 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)) # 유사도 화면에 표시 display_string = str( confidence) + '% Confidence it is ' + min_score_name name = min_score_name cv2.putText(image, display_string, (90, 170), cv2.FONT_HERSHEY_COMPLEX, 1, (250, 120, 255), 2) #75 보다 크면 동일 인물로 간주해 Match! if confidence > 75: cv2.putText(image, "Match : " + min_score_name, (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) cv2.imshow('Face Detection', image) frame = gaze.annotated_frame() text = "" if gaze.is_blinking(): text = "Blinking" elif gaze.is_right(): text = "Looking right" right_cnt += 1 elif gaze.is_left(): text = "Looking left" left_cnt += 1 elif gaze.is_up(): text = "Looking up" up_cnt += 1 elif gaze.is_down(): text = "Looking down" down_cnt += 1 elif gaze.is_center(): text = "Looking center" cv2.putText(frame, text, (90, 60), cv2.FONT_HERSHEY_DUPLEX, 1.6, (147, 58, 31), 2) left_pupil = gaze.pupil_left_coords() right_pupil = gaze.pupil_right_coords() cv2.putText(frame, "Left pupil: " + str(left_pupil), (90, 100), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1) cv2.putText(frame, "Right pupil: " + str(right_pupil), (90, 135), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1) cv2.imshow("Face Detection", frame) if left_pupil is None and right_pupil is None: cheat_cnt += 1 elif right_cnt > 5: cheat_cnt += 1 print('Too much looking right') elif left_cnt > 5: cheat_cnt += 1 print('Too much looking left') elif up_cnt > 5: cheat_cnt += 1 print('Too much looking up') elif down_cnt > 5: cheat_cnt += 1 print('Too much looking down') else: #75 이하면 Unmatch cv2.putText(image, "Unmatch", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2) cv2.imshow('Face Detection', image) except: #얼굴 검출 안됨 cv2.putText(image, "Face Not Found", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2) cv2.imshow('Face Detection', image) pass if cheat_cnt > 100: print(name + " Cheating Probability is high") break if cv2.waitKey(1) == 13: break cap.release() cv2.destroyAllWindows()