处女作品,人脸检查之脸部roi检查

环境:python2.7 运行后按q截取图片并保存两眼ROI区域,按w结束
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plot
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')

cap = cv.VideoCapture(0)

while(1):
    # get a frame
    ret, img = cap.read()
    # show a frame
    cv.imshow("capture", img)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break

# img = cv.imread('3-1.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:
    cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    i = 1
    for (ex,ey,ew,eh) in eyes:
        cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

 #       roi_eyegray = gray[y:y + h, x:x + w]
 #       roi_eyegray = gray[ey:ey + eh, ex:ex + ew]
        roi_eyegray = roi_color[ey:ey + eh, ex:ex + ew]

        cv.imwrite(str(i)+"roi.png", roi_eyegray)
        i = i + 1


cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()

if cv.waitKey(1) & 0xFF == ord('w'):

    cap.release()
    cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值