Python OpenCV 实时人脸检测裁剪保存 [英] Python OpenCV live face detection crop saved

查看:167
本文介绍了Python OpenCV 实时人脸检测裁剪保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此进行了大量研究,我认为我的逻辑已经过时了,几乎到此为止,但似乎无法理解为什么 cv2.imshow() 窗口中什么都没有显示,只是一个灰色框,但是好消息我是否能够检测到人脸并裁剪该人脸,然后将其保存在文件夹中.

I have done tons of research into this , and I think my logic are worn out , almost there but can't seem to understand why nothing is display in the cv2.imshow() windows just a grey box , however good news is I am able to detect a face and crop that face then save it in the folder.

你能不能解释一下我哪里出错了

can you please shed some light of where I have gone wrong

#Author: Waheed Rafiq
#Research Student Birmingham City University
#Date: 03/11/2016
#Description :detect and Save capture face in a folder.

#Import library required for Capture face.

import cv2


#import the cascade for face detection
FaceClassifier =cv2.CascadeClassifier
('haarcascade_frontalface_default.xml')
# access the webcam (every webcam has 
capture = cv2.VideoCapture(0)

   while(True):
     # Capture frame-by-frame

    ret, frame = capture.read()
    if not capture:
    print "Error opening webcam device"
    sys.exit(1)


    # to detect faces in video
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = FaceClassifier.detectMultiScale(gray, 1.3, 5)

    # Resize Image 
    minisize = (frame.shape[1],frame.shape[0])
    miniframe = cv2.resize(frame, minisize)
    # Store detected frames in variable name faces
   faces =  FaceClassifier.detectMultiScale(miniframe)
   # Draw rectangle 
   for f in faces:
    x, y, w, h = [ v for v in f ]
    cv2.rectangle(frame, (x,y), (x+w,y+h), (255,255,255))
    #Save just the rectangle faces in SubRecFaces
    sub_face = frame[y:y+h, x:x+w]
    FaceFileName = "unknowfaces/face_" + str(y) + ".jpg"
    cv2.imwrite(FaceFileName, sub_face)
    #Display the image 
    cv2.imshow('Result',frame)


    break

    # When everything done, release the capture

    img.release()
    cv2.waitKey(20)
    cv2.destroyAllWindows()

真的很想得到您的支持

推荐答案

我不得不修改我的代码,并再次重新思考逻辑:对于那些想知道如何从网络摄像头或 Raspberry PI 检测人脸的人使用 Opencv 然后裁剪检测到的人脸 这是你在 python 2.7 中使用 OpenCV 2.4.12 的方法

I had to revamp my code , and re-think the logics again: for those of you who wish to know how to detect a face from webcam or Raspberry PI using Opencv and then crop that detected face this is how you do it in python 2.7 using OpenCV 2.4.12

# croppfacedetection.py
#Author: Waheed Rafiq
#Research Student Birmingham City University
#Date: 03/11/2016
#Description : Save capture face in a folder.

#Import library required for Capture face.
# Should you wish to use this code for 
#education purpose in your assignment or dissertation
# please use the correct citation and give credit where required. 


import cv2
size = 4
webcam = cv2.VideoCapture(0) #Use camera 0

# We load the xml file
classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#  Above line normalTest
#classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') 
#Above line test with different calulation
#classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt_tree.xml')
#classifier = cv2.CascadeClassifier('lbpcascade_frontalface.xml')


while True:
    (rval, im) = webcam.read()
    im=cv2.flip(im,1,0) #Flip to act as a mirror

    # Resize the image to speed up detection
    mini = cv2.resize(im, (im.shape[1] / size, im.shape[0] / size))

    # detect MultiScale / faces 
    faces = classifier.detectMultiScale(mini)

    # Draw rectangles around each face
    for f in faces:
        (x, y, w, h) = [v * size for v in f] #Scale the shapesize backup
        cv2.rectangle(im, (x, y), (x + w, y + h),(0,255,0),thickness=4)
        #Save just the rectangle faces in SubRecFaces
        sub_face = im[y:y+h, x:x+w]
        FaceFileName = "unknowfaces/face_" + str(y) + ".jpg"
        cv2.imwrite(FaceFileName, sub_face)

    # Show the image
    cv2.imshow('BCU Research by Waheed Rafiq (c)',   im)
    key = cv2.waitKey(10)
    # if Esc key is press then break out of the loop 
    if key == 27: #The Esc key
    break

请记住,您需要创建一个文件夹,在该区域内,您需要一个名为 unknownfaces 的文件夹,从该文件夹的根目录运行脚本,它将检测到的任何人脸保存到 unknowfaces 文件夹中.有关此代码的更多信息将很快在我的网站上提供
waheedrafiq.net

remember you will need to create a folder and within that area you will need a folder named unknownfaces run the script from root of the folder and it should save any faces it detects into unknowfaces folder. further information about this code will be available soon my site
waheedrafiq.net

这篇关于Python OpenCV 实时人脸检测裁剪保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆