我在网上找的用tensorflow实现人脸识别,代码如下
import tensorflow as tf
import cv2
import dlib
import sys
from tensorflow.python.training.saver import Saver
from train_faces import cnnLayer, keep_prob_5, keep_prob_75, x
my_faces_path = ‘./my_faces’
other_faces_path = ‘./other_faces’
size = 64
imgs = []
labs = []
output = cnnLayer()
predict = tf.argmax(output, 1)
saver: Saver = tf.train.Saver()
sess = tf.Session()
saver.restore(sess=sess,save_path=tf.train.latest_checkpoint(’./train_faces.model’))
def is_my_face(image):
res = sess.run(predict, feed_dict={x: [image / 255.0], keep_prob_5: 1.0, keep_prob_75: 1.0})
if res[0] == 1:
return True
else:
return False
# 使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
cam = cv2.VideoCapture(0)
while True:
_, img = cam.read()
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = detector(gray_image, 1)
if not len(dets):
# print(‘Can`t get face.’)
cv2.imshow(‘img’, img)
key = cv2.waitKey(30) & 0xff
if key == 27:
sys.exit(0)
for i, d in enumerate(dets):
x1 = d.top() if d.top() > 0 else 0
y1 = d.bottom() if d.bottom() > 0 else 0
x2 = d.left() if d.left() > 0 else 0
y2 = d.right() if d.right() > 0 else 0
face = img[x1:y1, x2:y2]
# 调整图片的尺寸
face = cv2.resize(face, (size, size))
print('Is this my face? %s' % is_my_face(face))
cv2.rectangle(img, (x2, x1), (y2, y1), (255, 0, 0), 3)
cv2.imshow('image', img)
key = cv2.waitKey(30) & 0xff
if key == 27:
sys.exit(0)
sess.close()
遇到的问题是:
TypeError: expected bytes, NoneType found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File “F:/学校资料/毕业设计/网上python代码/FaceRecognition-tensorflow-master/is_my_face.py”, line 22, in
saver.restore(sess=sess,save_path=tf.train.latest_checkpoint(’./train_faces.model’))
File “C:\Users\HYD\anaconda3\envs\tfenv\lib\site-packages\tensorflow\python\training\saver.py”, line 1548, in restore
{self.saver_def.filename_tensor_name: save_path})
File “C:\Users\HYD\anaconda3\envs\tfenv\lib\site-packages\tensorflow\python\client\session.py”, line 789, in run
run_metadata_ptr)
File “C:\Users\HYD\anaconda3\envs\tfenv\lib\site-packages\tensorflow\python\client\session.py”, line 997, in _run
feed_dict_string, options, run_metadata)
File “C:\Users\HYD\anaconda3\envs\tfenv\lib\site-packages\tensorflow\python\client\session.py”, line 1132, in _do_run
target_list, options, run_metadata)
File “C:\Users\HYD\anaconda3\envs\tfenv\lib\site-packages\tensorflow\python\client\session.py”, line 1139, in _do_call
return fn(*args)
File “C:\Users\HYD\anaconda3\envs\tfenv\lib\site-packages\tensorflow\python\client\session.py”, line 1121, in _run_fn
status, run_metadata)
SystemError: returned a result with an error set
有哪位大神能帮我解决一下啊,感激不尽!!