如何解决使用tf.while_loop
我正在编写代码以检测图片中的人脸。它起作用了,但是有一些警告。然后,我尝试解决问题并使用tf.while_loop(),但我无处可寻。
# 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
# 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 = '/content/drive/My Drive/ufc-project/5-celebrity-faces-dataset/train/ben_afflek'
# enumerate files
# create a list with the path of all images
paths = []
for filename in listdir(folder):
path = folder + '/' + filename
paths.append(path)
faces=[]
c = lambda i,faces: i < len(paths)
b = lambda i,faces: (i + 1,faces.append(extract_face(paths[i])))
i = 0
r = tf.while_loop(c,b,(i,faces))
然后,在r [1]中,我希望从图片中获得一张带有面孔的列表,但它不会重复进行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。