我试图创建一个程序,可以检测我的面部表情(从摄像头)。
然而,在显示我的面部时,我得到了以下错误
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-e0549b59dd89> in <module>()
47 print("\n\n")
48
---> 49 cv2_imshow(frame)
50 if cv2.waitKey(1) & 0xFF == ord('q'):
51 break
/usr/local/lib/python3.6/dist-packages/google/colab/patches/__init__.py in cv2_imshow(a)
20 image.
21 """
---> 22 a = a.clip(0, 255).astype('uint8')
23 # cv2 stores colors as BGR; convert to RGB
24 if a.ndim == 3:
AttributeError: 'NoneType' object has no attribute 'clip'
我在Google Colab上使用Python 3.6。
我使用的是谷歌补丁的cv2_imshow()
,因为Colab不支持cv2.imshow()
下面是我的代码:
from google.colab.patches import cv2_imshow
from keras.models import load_model
from time import sleep
from keras.preprocessing.image import img_to_array
from keras.preprocessing import image
import cv2
import numpy as np
face_classifier = cv2.CascadeClassifier('/content/drive/My Drive/Colab Notebooks/haarcascade_frontalface_default.xml')
classifier = load_model('/content/drive/My Drive/Colab Notebooks/fer_68acc.h5')
class_labels = ['Angry','Happy','Neutral','Sad','Surprise']
cap = cv2.VideoCapture(0)
while True:
# Grab a single frame of video
ret, frame = cap.read()
labels = []
gray = cv2.imread(frame, cv2.IMREAD_GRAYSCALE)
faces = face_classifier.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h,x:x+w]
roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
if np.sum([roi_gray])!=0:
roi = roi_gray.astype('float')/255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi,axis=0)
# make a prediction on the ROI, then lookup the class
preds = classifier.predict(roi)[0]
print("\nprediction = ",preds)
label=class_labels[preds.argmax()]
print("\nprediction max = ",preds.argmax())
print("\nlabel = ",label)
label_position = (x,y)
cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),3)
else:
cv2.putText(frame,'No Face Found',(20,60),cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),3)
print("\n\n")
cv2_imshow(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
有没有人能帮帮忙?不幸的是,我不能在我的本地机器上运行,所以如果有人给出一个可以在Google Colab上运行的解决方案,那将是很有帮助的。
谢谢
发布于 2020-08-16 11:49:51
以下内容是否为您提供了非零大小:
print(frame.shape)
如果不是,则图像未正确加载。非类型表示在名为frame
的变量中没有存储任何内容
https://stackoverflow.com/questions/63429058
复制相似问题