<jupyter_start><jupyter_text>Loading face detector<jupyter_code>import dlib
# Create a HOG face detector using the built-in dlib class
face_detector = dlib.get_frontal_face_detector()
# Using cnns
# face_detector = dlib.cnn_face_detection_model_v1('../models/mmod_human_face_detector.dat')<jupyter_output><empty_output><jupyter_text>Loading alignment model<jupyter_code>from cpe775.align import AlignDlib
align = AlignDlib()<jupyter_output><empty_output><jupyter_text>Loading embedding model<jupyter_code>from cpe775.networks.openface import OpenFace
from cpe775.face_detector import FaceEmbedding
embedding = FaceEmbedding('../models/openface/nn4.small2.v1.pth', cuda=cuda)<jupyter_output><empty_output><jupyter_text>Loading landmarks regressor<jupyter_code>from cpe775.networks.resnet import resnet18
from cpe775.face_detector import LandmarksDetector
net = resnet18(in_shape=(-1, 1, 256, 256), out_shape=(-1, 68, 2))
landmarks_detector = LandmarksDetector(net, '../models/landmarks_detector.pth', cuda=cuda)<jupyter_output><empty_output><jupyter_text>Loading classification model<jupyter_code>from cpe775.face_detector import FaceClassify
classify = FaceClassify('../models/face_classification_svm.pkl')<jupyter_output><empty_output><jupyter_text>Loading mapping  Using the webcamAdapted from: https://github.com/ageitgey/face_recognition<jupyter_code>import cv2

# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Initialize some variables
face_locations = []
face_names = []
face_landmarks = []
face_probas = []
process_this_frame = True
scale = 0.25
while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=scale, fy=scale)
    
    # Only process every other frame of video to save time
    if process_this_frame:
        
        face_locations = []
        face_names = []
        face_probas = []
        face_landmarks = []
        
        # Find all the faces and face encodings in the current frame of video
        detected_faces = face_detector(small_frame, 2)
        
        # Loop through each face we found in the image
        for i, face_rect in enumerate(detected_faces):

            # Find the landmarks given the image and the bounding boxes
            landmarks, rect = landmarks_detector.from_rect(small_frame, face_rect)
            
            face_locations.append(rect)
            
            face_landmarks.append(landmarks)
        
            # Align and resize the face given the landmarks
            thumbnail = align(small_frame, landmarks)
            
            # Extract the embedding from the aligned face 
            embed = embedding(thumbnail)
            
            # Classify
            name, prob = classify(embed)
            
            face_probas.append(prob)
            
            if prob > 0.5:
                face_names.append(name)
            else:
                face_names.append('unknown')

    process_this_frame = not process_this_frame


    # Display the results
    for (left, top, right, bottom), name, prob, landmarks in zip(face_locations, face_names, face_probas, face_landmarks):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top = int(top/scale) - 15
        right = int(right/scale) + 15
        bottom = int(bottom/scale) + 45
        left = int(left/scale) - 15

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, '{}, {:.2f}%'.format(name, prob*100), (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
        
        # loop over the (x, y)-coordinates for the facial landmarks and draw them on the image
        for (x, y) in landmarks:
            cv2.circle(frame, (int(x/scale), int(y/scale)), 1, (0, 0, 255), -1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08923318297705432, 'fraction_numerical': 0.02294567562267111, 'mean_word_length': 3.156479217603912, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '202486', 'license': 'MIT', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1543, 'n_words': 484, 'path': 'notebooks/14. Real-time classification?.ipynb', 'repo_name': 'wesleylp/CPE775', 'size': '5099'}