问题描述
错误:OpenCV(4.5.1) /tmp/pip-req-build-tk9iuyva/opencv/modules/objdetect/src/cascadedetect.cpp:1389: 错误: (-215:Assertion Failed) scaleFactor > 1 && _image .depth() == CV_8U 函数'detectMultiScale'
我尝试添加 gray = np.array(gray,dtype='uint8')
并且错误消失了但是检测人脸时应该出现的框没有出现
import tensorflow as tf
import keras
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img
import numpy as np
import argparse
import cv2
import os
import matplotlib.pyplot as plt
%matplotlib inline
model = tf.keras.models.load_model("../input/modelh5/My_Model.h5")
images=['../input/face-mask1/examples/example_01.png','../input/face-mask1/examples/example_02.png','../input/face-mask1/examples/example_03.png' ]
face_cascade = cv2.CascadeClassifier('../input/haarcascade/haarcascade_frontalface_default.xml')
img = images[0] # Add path here
img = plt.imread(img,format='8UC1')
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(gray,1.3,5)
# Draw the rectangle around each face
for (x,y,w,h) in faces:
face = img[y:y+h,x:x+w]
face = cv2.resize(face,(224,224))
face = img_to_array(face)
face = preprocess_input(face)
face = np.expand_dims(face,axis=0)
(mask,withoutMask) = model.predict(face)[0]
mask = mask*100
withoutMask = withoutMask*100
font = cv2.FONT_HERShey_SIMPLEX
# Getting Text Size in pixel
print("Image Width: ",w)
textSize = cv2.getTextSize(text="No Mask: " + str("%.2f" % round(mask,2)),fontFace=cv2.FONT_HERShey_SIMPLEX,fontScale=1,thickness=3)
print("Text Width: ",textSize[0][0])
if mask > withoutMask:
cv2.putText(img,text = "Mask: " + str("%.2f" % round(mask,org = (x-5,y-15),fontFace=font,fontScale = (2*w)/textSize[0][0],color = (0,255,0),thickness = 3,lineType = cv2.LINE_AA)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,5)
else:
cv2.putText(img,text = "No Mask: " + str("%.2f" % round(withoutMask,fontScale = (1.8*w)/textSize[0][0],color = (255,(255,5)
# display
plt.imshow(img)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)