简体   繁体   English

为什么 haarcascades 在 opencv 上不起作用

[英]why does the haarcascades does not work on opencv

I am trying to detect faces in opencv,but I'm running in some issues: 1-When I put the following syntax: gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY) ,it shows up in red and does not work.我正在尝试检测 opencv 中的人脸,但我遇到了一些问题: 1-当我输入以下语法时: gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY) ,它显示为红色并且不起作用。 2-The haarcascade also shows up in red: faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE) .I tried to do like in some tutorials but it does not work.Would you have any idea? 2-haarcascade 也显示为红色: faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE) 。我尝试在一些教程中这样做,但是它不起作用。你有什么想法吗? Here is my code:这是我的代码:

#importing packages
import cv2
import numpy as np

#variables
webcam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
w_size = (700,500)
#turn the webcam on
while (True):
    #reading camera and turing into frames
    ret,frames = webcam.read()
    frames = cv2.resize(frames,w_size)
    #detection
    gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE)
    for (x, y, w, h) in faces:
        cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
    cv2.imshow('face_recognition',frames)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
#running script
webcam.release()
cv2.destroyAllWindows()

i simply:我只是:

  1. added cv2.data.haarcascades as prefix of the type CascadeClassifier添加cv2.data.haarcascades作为 CascadeClassifier 类型的前缀
  2. deleted cv2.CV_HAAR_SCALE_IMAGE (parameter not used anymore)删除了cv2.CV_HAAR_SCALE_IMAGE (参数不再使用)

Code:代码:

import cv2
import numpy as np
import cv2.data
#variables
webcam = cv2.VideoCapture(0)

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')

w_size = (700,500)
#turn the webcam on
while (True):

    #reading camera and turing into frames
    ret,frames = webcam.read()
    frames = cv2.resize(frames,w_size)
    #detection
    gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30))

    for (x, y, w, h) in faces:
        cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
    cv2.imshow('face_recognition',frames)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
#running script
webcam.release()
cv2.destroyAllWindows()

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

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