Рубрики
Без рубрики

Как сделать детектор улыбки в реальном времени

Так что после долгого времени я вернулся с Python A.i Так что давайте начнем скачать эти файлы haarcas … Помечено Python, открытый источник, машинное обучение.

Так что после долгого времени я вернулся с Python A.i Так что давайте начнем

Скачать эти файлы haarcascade_frontalface_default.xml. haarcascade_smile.xml.

Теперь давайте добавим лица и улыбку классификаторы

# Face and Smile classifiers
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
smile_detector = cv2.CascadeClassifier('haarcascade_smile.xml')

давайте получим нашу веб-камеру

# Grab Webcam feed
webcam = cv2.VideoCapture(0)

Давайте добавим кадры

while True:

    successful_frame_read, frame = webcam.read()

    # if there is an error or abort
    if not successful_frame_read:
        break

    # Change to grayscale
    frame_grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect faces first
    faces = face_detector.detectMultiScale(frame_grayscale, 1.3, 5)

Теперь давайте определим лица сделать перенапряжение вокруг лица и добавить текст, он улыбается и выйдет на функционально

# Run smile detection within each of those faces
    for (x, y, w, h) in faces:
        # draw a square around smile
        cv2.rectangle(frame, (x, y), (x+w, y+h), (100, 200, 50), 4)

        # Draw a sub image
        face = frame[y:y+h, x:x+w]

        # Grayscale the face
        face_grayscale = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

        # Detect Smile in the face 😄
        smile = smile_detector.detectMultiScale(face_grayscale, 1.7,20)

        # Label the face as smiling 

        if len(smile) > 0:
            cv2.putText(frame, 'Smiling', (x,y+h+40), fontScale=3,
            fontFace=cv2.FONT_HERSHEY_PLAIN, color=(255,255,255))

        # Show the current frame
        cv2.imshow('Smile Detector', frame)

        # Stop if 'Q' is pressed
        key = cv2.waitKey(1)   
        if key == 81 or key==113:
            break

Теперь давайте очистим и выпустить веб-камеру

# Clear up!
webcam.release() 
cv2.destroyAllWindows()

давайте посмотрим полный код

#============================================================
#   This is an a.i smile detector written in python
#           By - 'Sadman Sakib'
#============================================================
import cv2 

# Face and Smile classifiers
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
smile_detector = cv2.CascadeClassifier('haarcascade_smile.xml')

# Grab Webcam feed
webcam = cv2.VideoCapture(0)

while True:

    successful_frame_read, frame = webcam.read()

    # if there is an error or abort
    if not successful_frame_read:
        break

    # Change to grayscale
    frame_grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect faces first
    faces = face_detector.detectMultiScale(frame_grayscale, 1.3, 5)

    # Run smile detection within each of those faces
    for (x, y, w, h) in faces:
        # draw a square around smile
        cv2.rectangle(frame, (x, y), (x+w, y+h), (100, 200, 50), 4)

        # Draw a sub image
        face = frame[y:y+h, x:x+w]

        # Grayscale the face
        face_grayscale = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

        # Detect Smile in the face 😄
        smile = smile_detector.detectMultiScale(face_grayscale, 1.7,20)

        # Label the face as smiling 

        if len(smile) > 0:
            cv2.putText(frame, 'Smiling', (x,y+h+40), fontScale=3,
            fontFace=cv2.FONT_HERSHEY_PLAIN, color=(255,255,255))

        # Show the current frame
        cv2.imshow('Smile Detector', frame)

        # Stop if 'Q' is pressed
        key = cv2.waitKey(1)   
        if key == 81 or key==113:
            break

# Clear up!
webcam.release() 
cv2.destroyAllWindows()

Это на Github Код Пожалуйста, дайте ему звезду

Оригинал: “https://dev.to/professor_2390/realtime-smile-detector-299e”