Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

@CAPSLOCK. Your code isn't working properly. You're missing something. Do not set values too high.

!usr/bin/python37

#OpenCV 4.1.2, Raspberry p[i 3/4. Buster ver10
#Date: 13th December, 2019

import cv2 as cv

cascade_face = cv.CascadeClassifier('haarcascade_frontalface_default.xml') 
cascade_eye = cv.CascadeClassifier('haarcascade_eye.xml') 
cascade_smile = cv.CascadeClassifier('haarcascade_smile.xml')

cap = cv.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX

def detected(img, gray):
    #Face
    face = cascade_face.detectMultiScale(gray, 1.3, 5)
    for x, y, w, h in face:
        cv.rectangle(img, (x, y), (x+we, y+h),(255, 130, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[ye:y+h, x_:x+w]

        #For Eyes
        eye = cascade_eye.detectMultiScale(roi_gray, 1.2, 18)
        cv.putText(img, 'Face{}'.format(x, y), font, 2, (255,0,0),5)
        for (ex, ey, ew, eh) in eye:
            cv.rectangle(roi_color,(ex, ey),
                        (ex+ew,ey+eh),(0, 180, 60), 2)

        #Smile
        smile = cascade_smile.detectMultiScale(roi_gray, 1.7, 20)
        for (sx, sy, sw, sh) in smile: 
            cv.rectangle(roi_color,(sx, sy),
                        (sx+sw, sy+sh), (255, 0, 130), 2)

    return img  

while True:
    img = cap.read() 
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) 
    result = detectied(gray, img) 
    cv.imshow('Detected', result) 
    if cv.waitKey(1) & 0xFF == ord('q'):
        break 

cap.release() 
cv.destroyAllWindows()

@CAPSLOCK. Your code isn't working properly. You're missing something. Do not set values too high.

!usr/bin/python37

#!usr/bin/python37

#OpenCV 4.1.2, Raspberry p[i 3/4. Buster ver10
#Date: 13th December, 2019

import cv2 as cv

cascade_face = cv.CascadeClassifier('haarcascade_frontalface_default.xml') 
cascade_eye = cv.CascadeClassifier('haarcascade_eye.xml') 
cascade_smile = cv.CascadeClassifier('haarcascade_smile.xml')

cap = cv.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX

def detected(img, gray):
    #Face
    face = cascade_face.detectMultiScale(gray, 1.3, 5)
    for x, y, w, h in face:
        cv.rectangle(img, (x, y), (x+we, y+h),(255, 130, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[ye:y+h, x_:x+w]

        #For Eyes
        eye = cascade_eye.detectMultiScale(roi_gray, 1.2, 18)
        cv.putText(img, 'Face{}'.format(x, y), font, 2, (255,0,0),5)
        for (ex, ey, ew, eh) in eye:
            cv.rectangle(roi_color,(ex, ey),
                        (ex+ew,ey+eh),(0, 180, 60), 2)

        #Smile
        smile = cascade_smile.detectMultiScale(roi_gray, 1.7, 20)
        for (sx, sy, sw, sh) in smile: 
            cv.rectangle(roi_color,(sx, sy),
                        (sx+sw, sy+sh), (255, 0, 130), 2)

    return img  

while True:
    img = cap.read() 
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) 
    result = detectied(gray, img) 
    cv.imshow('Detected', result) 
    if cv.waitKey(1) & 0xFF == ord('q'):
        break 

cap.release() 
cv.destroyAllWindows()