我的代码中出现以下错误。
import mtcnn
# print version
print(mtcnn.__version__)
# demonstrate face detection on 5 Celebrity Faces Dataset
from os import listdir
from PIL import Image
from numpy import asarray
from matplotlib import pyplot
from mtcnn.mtcnn import MTCNN
print("MTCNN: {}".format(mtcnn.__version__))
import tensorflow as tf
from tensorflow import keras
# extract a single face from a given photograph
def extract_face(filename, required_size=(160, 160)):
# load image from file
image = Image.open(filename)
# convert to RGB, if needed
image = image.convert('RGB')
# convert to array
pixels = asarray(image)
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
results = detector.detect_faces(pixels)
# extract the bounding box from the first face
x1, y1, width, height = results[0]['box']
# bug fix
x1, y1 = abs(x1), abs(y1)
x2, y2 = x1 + width, y1 + height
# extract the face
face = pixels[y1:y2, x1:x2]
# resize pixels to the model size
image = Image.fromarray(face)
image = image.resize(required_size)
face_array = asarray(image)
return face_array
# specify folder to plot
#folder = '5-celebrity-faces-dataset/train/ben_afflek/'
folder = '5-celebrity-faces-dataset/train/ben_afflek'
i = 1
# enumerate files
for filename in listdir(folder):
# path
path = folder + '/' + filename
# get face
face = extract_face(path)
print(i, face.shape)
# plot
pyplot.subplot(2, 7, i)
pyplot.axis('off')
pyplot.imshow(face)
i += 1
pyplot.show()
错误:
anaconda3 \ envs \ py3 \ lib \ site-packages \ keras \ backend \ tensorflow_backend.py”, 第68行,在get_uid中 图= tf.get_default_graph()
AttributeError:模块“ tensorflow”没有属性 'get_default_graph'
我尝试了几种不同的进口方式,但是没有任何效果。看来这个错误很常见,但是我找不到能解决我问题的任何东西。
答案 0 :(得分:1)
在答案here中,像您一样从tensorflow导入keras即可解决问题。
但是您遇到的问题是MTCNN在纯Keras而不是TensorFlow上运行,因此从tensorflow加载“ main.py” keras中这一事实没有任何效果。您要么需要降级tensorflow版本,要么修改MTCNN中的每个导入,但不能保证无法正常工作。