简体   繁体   English

Python OpenCV2 Haar分类器

[英]Python opencv2 haar classifier

I am attempting to combine two scripts for getting facial recognition to work with a live video feed from my laptop web cam. 我正在尝试结合使用两个脚本来获得面部识别,以与笔记本电脑网络摄像头中的实时视频源配合使用。 I have one working opencv2 script that works with my webcam to view live footage and the other is a facial recognition script with the haar classifier on a jpeg still image. 我有一个可以使用的opencv2脚本与我的网络摄像头一起使用,以查看实时镜头,另一个是面部识别脚本,在jpeg静止图像上具有haar分类器。 I am working with Python 3.6 IDE, open cv2. 我正在使用Python 3.6 IDE,打开cv2。 This script below works for viewing a live feed thru my laptop web camera. 下面的脚本可用于通过笔记本电脑网络摄像头查看实时供稿。

import numpy as np
import cv2, time

video = cv2.VideoCapture(0)  
a = 0

while True:

    a = a + 1    
    check, frame = video.read()

    print(check)
    print(frame)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow("Capturing", gray)
    cv2.waitKey(1)

    key = cv2.waitKey(1)    
    if key == ord('q'):
        break

print(a)

video.release()

And I got this script that draw a box around a face to work for the haar classifier with a function on a still .jpeg image. 我得到了这个脚本,该脚本在面部周围绘制了一个框,可用于在.jpeg静止图像上具有功能的haar分类器。 How would I go about combining these two scripts with using the haar classifier for facial recognition on live video feed? 我将如何结合使用这两个脚本以及使用haar分类器在实时视频源上进行面部识别? The haar classifier XML and jpeg are files on my local PC directory. haar分类器XML和jpeg是我本地PC目录中的文件。

import cv2
import matplotlib.pyplot as plt
import time 



def detect_faces(f_cascade, colored_img, scaleFactor = 1.1):
    img_copy = colored_img.copy()          
    gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)           
    faces = f_cascade.detectMultiScale(gray, scaleFactor=scaleFactor, minNeighbors=5);           
    for (x, y, w, h) in faces:
        cv2.rectangle(img_copy, (x, y), (x+w, y+h), (0, 255, 0), 2)               
    return img_copy



test2 = cv2.imread('C:/Python/opencv/sAndb.jpg')
haar_face_cascade = cv2.CascadeClassifier('C:/Python/opencv/opencv-master/opencv-master/data/haarcascades/haarcascade_frontalface_alt.xml')
faces_detected_img = detect_faces(haar_face_cascade, test2)
cv2.imshow('Faces', faces_detected_img)

Try this: 尝试这个:

import numpy as np
import cv2, time
import matplotlib.pyplot as plt

haar_face_cascade = cv2.CascadeClassifier('C:/Python/opencv/opencv-master/opencv-master/data/haarcascades/haarcascade_frontalface_alt.xml')
video = cv2.VideoCapture(0)  
a = 0

while True:

    a = a + 1    
    check, frame = video.read()

    print(check)
    print(frame)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = f_cascade.detectMultiScale(gray, scaleFactor=scaleFactor, minNeighbors=5);
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)


    cv2.imshow("Face Detector", frame)
    cv2.waitKey(1)

    key = cv2.waitKey(1)    
    if key == ord('q'):
        break

print(a)

video.release()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM