-2

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. 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? 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()
Emiliano Viotti
  • 1,619
  • 2
  • 16
  • 30
ricvz
  • 1
  • "does not work" is a lousy description of the issue. be detailed about it. -- "show up in red" means your IDE is confused. that doesn't really matter because some IDEs aren't all that reliable in their "spell check" capability. what matters is whether the code runs or throws errors. – Christoph Rackwitz Jan 21 '22 at 19:32
  • Thank you for your advice, will keep it in mind for the next time. – ricvz Jan 22 '22 at 11:32

1 Answers1

0

i simply:

  1. added cv2.data.haarcascades as prefix of the type CascadeClassifier
  2. deleted cv2.CV_HAAR_SCALE_IMAGE (parameter not used anymore)

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()
XxJames07-
  • 1,833
  • 1
  • 4
  • 17