Demo entry 6721715

eye detection

   

Submitted by anonymous on Mar 22, 2018 at 10:32
Language: Python 3. Code size: 1.8 kB.

import cv2 as cv
import numpy as np


def face_and_eye_detect_demo(src):
    gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY)
    face_detector = cv.CascadeClassifier("E:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt_tree.xml")
    faces = face_detector.detectMultiScale(gray,1.1,2)  #视频中改成1.1和2,提高第一个值、降低第二个值则减小精度,增加数量
    count = 0
    for x,y,w,h in faces:
        cv.rectangle(src,(x,y),(x+w,y+h),(0,0,255),2)
        count = count+1
    #cv.imshow("eye-detection-result",src)
    print("%s faces detected the picture"%count)
    print("\t%s people in the picture"%int(count/2))

    eye_detector = cv.CascadeClassifier("E:/opencv/sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml")
    eyes = eye_detector.detectMultiScale(gray,1.1,2)  
    count = 0
    for x,y,w,h in eyes:
        cv.circle(src,(int((2*x+w)/2),int((2*y+h)/2)),int((w+h)/3),(0,200,255),2)
        count = count+1
    cv.imshow("face and eye detection result",src)
    print("%s eyes detected the picture"%count)
    print("\t%s people in the picture"%int(count/2))

'''
#print("----------Hello Python----------")
src = cv.imread("e:/class_2.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.namedWindow("face and eye detection result",cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
face_and_eye_detect_demo()
cv.imwrite("e:/class_2_result.jpg",src)
cv.waitKey(0)

cv.destroyAllWindows()
'''
print("----------face detection demo----------")
capture = cv.VideoCapture(0)
cv.namedWindow("face-detection-result",cv.WINDOW_AUTOSIZE)
while(True):
    ret, frame = capture.read()
    frame = cv.flip(frame,1)
    face_and_eye_detect_demo(frame)
    c = cv.waitKey(5)
    if c == 27: #esc
        break


cv.waitKey(0)
cap.realease()
cv.destroyAllWindows()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).