'Opencv in python gives me a AttributeError
Im trying to get opencv working for a face recognition program. My webcam is working since I tested it using cv2.VideoCapture and the window popped up, then when i added the next step which is turning the image gray using cv2.cvtcolor(frame, cv2.COLOR_BGR2GRAY) I got the error message saying AttributeError: module 'cv2.cv2' has no attribute 'cvtcolor. I tried uninstalling and reinstalling opencv using pip3 install opencv-contrib-python. Im not sure what im doing wrong here, my guess is maybe my files arnt in the write place but im not sure where they would need to be
When I use this code my window of my webcam pops up and works fine. Working code
import os
import PIL
import cv2
import numpy
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
"haarcascade_frontalface_alt2.xml")
cap = cv2.VideoCapture(0)
while(True):
#frame by frame
ret, frame = cap.read()
gray = cv2.cvtcolor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiscale(gray, scaleFactor=1.5,
minNeighbors=5)
#display frame
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
#when done relase cap
cap.release()
cv2.destroyAllWindows()
Traceback (most recent call last): File "c:/Users/Nick Miller/all_for_vs/Code/facerecg.py", line 14, in gray = cv2.cvtcolor(frame, cv2.COLOR_BGR2GRAY) AttributeError: module 'cv2.cv2' has no attribute 'cvtcolor' [ WARN:0] terminating async callback
Solution 1:[1]
Change
cv2.cvtcolor(frame, cv2.COLOR_BGR2GRAY)
to cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
.
Also, change faces = face_cascade.detectMultiscale(gray,scaleFactor=1.5,minNeighbors=5)
to faces = face_cascade.detectMultiScale(gray,scaleFactor=1.5,minNeighbors=5)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |