main.py 1.83 KB
Newer Older
tihmels's avatar
tihmels committed
1 2
import cv2
import sys
3 4 5 6
import dlib

from projectmood import cvhelper

tihmels's avatar
tihmels committed
7 8

def main():
9 10 11 12 13
    # Set up some required objects
    detector = dlib.get_frontal_face_detector()  # Face detector
    detectors = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    predictor = dlib.shape_predictor(
        "shape_predictor_68_face_landmarks.dat")  # Landmark identifier. Set the filename to whatever you named the downloaded file
tihmels's avatar
tihmels committed
14 15 16 17 18 19 20 21 22 23 24 25 26

    if len(sys.argv) > 1:
        cap = cv2.VideoCapture(str(sys.argv[1]))
    else:
        cap = cv2.VideoCapture(0)

    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
        clahe_image = clahe.apply(gray)
        faces = detectors.detectMultiScale(gray, 1.3, 5)

        detections = detector(clahe_image, 1)  # Detect the faces in the image

        for k, d in enumerate(detections):  # For each detected face

            shape = predictor(clahe_image, d)  # Get coordinates
            for i in range(1, 68):  # There are 68 landmark points on each face
                cv2.circle(gray, (shape.part(i).x, shape.part(i).y), 1, (0, 0, 255),
                           thickness=2)  # For each point, draw a red circle with thickness2 on the original frame
                for (x, y, w, h) in faces:
                    cv2.rectangle(gray, (x, y), (x + w, y + h), (255, 0, 0), 2)
                    roi_gray = gray[y:y + h, x:x + w]
                    roi_color = frame[y:y + h, x:x + w]

tihmels's avatar
tihmels committed
44 45 46 47 48 49 50 51 52 53 54
        cvhelper.createwindow('Grayscale', gray)


        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()