To find the model file, glue the path directly into the code, avoid typing
You can write r' path ', adding r to indicate \ is no longer an escape
C:\Users\529\PycharmProjects\demo\Utils\models\cnn_fv.h5
import cv2
from keras.models import load_model
import numpy as np
import dlib
model = load_model('C:\\Users\\529\\PycharmProjects\\demo\\Utils\\models\\cnn_fv.h5')
detector = dlib.get_frontal_face_detector()
video = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
def rec(img):
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dets = detector(gray,1)
if dets is not None:
for face in dets:
left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
#绘制矩形框
cv2.rectangle(img,(left,top),(right,bottom),(0,255,0),2)
#cv2.resize将原始图像调整为指定大小 scr:原始图像 dsize:输出图像的尺寸
img1=cv2.resize(img[top:bottom,left:right],dsize=(224,224))
#颜色空间转换函数
img1=cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)
img1 = np.array(img1)/255.
img_tensor = img1.reshape(1,224,224,3)
prediction =model.predict(img_tensor)
if prediction[0][0]<0.5:
result='nosmile'
else:
result='smile'
print(result)
cv2.putText(img, result, (left,top), font, 2, (0, 255, 0), 2, cv2.LINE_AA)
# 实时展示效果画面
cv2.imshow('smile detector', img)
while video.isOpened():
res, img_rd = video.read()
if not res:
break
rec(img_rd)
# 每5毫秒监听一次键盘动作
if cv2.waitKey(5) & 0xFF == ord('q'):
break
# 最后,关闭所有窗口
video.release()
cv2.destroyAllWindows()
0 Answer
To find the model file, glue the path directly into the code, avoid typing
You can write r' path ', adding r to indicate \ is no longer an escape
Check this path C:\Users\529\PycharmProjects\demo\Utils\models\cnn_fv.h5 There really is an h5 file. < br / > if there is a will try the backslash model = load_model(' C: / Users / 529 / PycharmProjects/demo/Utils/models/cnn_fv h5 ') < br / > or the model test under = load_model('C:\Users\529\PycharmProjects\demo\Utils\models\cnn_fv.h5')
这家伙很懒,什么都没留下...