Reputation: 57
I'm getting the following error in my code.
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()
Error:
anaconda3\envs\py3\lib\site-packages\keras\backend\tensorflow_backend.py", line 68, in get_uid graph = tf.get_default_graph()
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
I tried several different imports, but nothing works. It seems that this error is usual, but I'm not finding anything that solves my problem.
Upvotes: 3
Views: 881
Reputation: 15043
In the answer here import keras from tensorflow, like you did, solves the problem.
But the problem in your case is that the MTCNN works on pure Keras instead of TensorFlow, so the fact that you load in your "main.py" keras from tensorflow does not have any effect. You either need to downgrade the tensorflow version or you modify every import in MTCNN which is not guaranteed to work unfortunately.
Upvotes: 1