为啥我在 facenet 中收到此错误?
Posted
技术标签:
【中文标题】为啥我在 facenet 中收到此错误?【英文标题】:Why am I getting this error in facenet?为什么我在 facenet 中收到此错误? 【发布时间】:2018-10-08 12:32:47 【问题描述】:我正在尝试在我的虚拟机(谷歌云平台)上运行 facematch(facenet)。起初,一切运行顺利,它正在嵌入面部的点,但突然之间,我的代码停止了工作。
第一个代码,你可以看到里面有导入
对于第二个代码,您可以看到导入。
这是 ls 命令,因此您可以看到所有目录/模块都在那里,并查看我遇到的错误
任何人都可以分享一些关于我做错了什么的见解?
Face_match_demo代码:
import tensorflow as tf
import numpy as np
import facenet
from align import detect_face
import cv2
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--img1", type = str, required=True)
parser.add_argument("--img2", type = str, required=True)
args = parser.parse_args()
# some constants kept as default from facenet
minsize = 20
threshold = [0.6, 0.7, 0.7]
factor = 0.709
margin = 44
input_image_size = 160
sess = tf.Session()
# read pnet, rnet, onet models from align directory and files are det1.npy, det2.npy, det3.npy
pnet, rnet, onet = detect_face.create_mtcnn(sess, 'align')
# read 20170512-110547 model file downloaded from https://drive.google.com/file/d/0B5MzpY9kBtDVZ2RpVDYwWmxoSUk
facenet.load_model("20170512-110547/20170512-110547.pb")
# Get input and output tensors
images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
embedding_size = embeddings.get_shape()[1]
def getFace(img):
faces = []
img_size = np.asarray(img.shape)[0:2]
bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
if not len(bounding_boxes) == 0:
for face in bounding_boxes:
if face[4] > 0.50:
det = np.squeeze(face[0:4])
bb = np.zeros(4, dtype=np.int32)
bb[0] = np.maximum(det[0] - margin / 2, 0)
bb[1] = np.maximum(det[1] - margin / 2, 0)
bb[2] = np.minimum(det[2] + margin / 2, img_size[1])
bb[3] = np.minimum(det[3] + margin / 2, img_size[0])
cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
resized = cv2.resize(cropped, (input_image_size,input_image_size),interpolation=cv2.INTER_CUBIC)
prewhitened = facenet.prewhiten(resized)
faces.append('face':resized,'rect':[bb[0],bb[1],bb[2],bb[3]],'embedding':getEmbedding(prewhitened))
return faces
def getEmbedding(resized):
reshaped = resized.reshape(-1,input_image_size,input_image_size,3)
feed_dict = images_placeholder: reshaped, phase_train_placeholder: False
embedding = sess.run(embeddings, feed_dict=feed_dict)
return embedding
def compare2face(img1,img2):
face1 = getFace(img1)
face2 = getFace(img2)
if face1 and face2:
# calculate Euclidean distance
dist = np.sqrt(np.sum(np.square(np.subtract(face1[0]['embedding'], face2[0]['embedding']))))
return dist
return -1
img1 = cv2.imread(args.img1)
img2 = cv2.imread(args.img2)
distance = compare2face(img1, img2)
threshold = 1.10 # set yourself to meet your requirement
print("distance = "+str(distance))
face_embeddings_demo 代码:
import tensorflow as tf
from align import detect_face
import facenet
import cv2
import imutils
import numpy as np
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--img", type = str, required=True)
args = parser.parse_args()
# some constants kept as default from facenet
minsize = 20
threshold = [0.6, 0.7, 0.7]
factor = 0.709
margin = 44
input_image_size = 160
sess = tf.Session()
# read pnet, rnet, onet models from align directory and files are det1.npy, det2.npy, det3.npy
pnet, rnet, onet = detect_face.create_mtcnn(sess, 'align')
# read 20170512-110547 model file downloaded from https://drive.google.com/file/d/0B5MzpY9kBtDVZ2RpVDYwWmxoSUk
facenet.load_model("20170512-110547/20170512-110547.pb")
# Get input and output tensors
images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
embedding_size = embeddings.get_shape()[1]
def getFace(img):
faces = []
img_size = np.asarray(img.shape)[0:2]
bounding_boxes, points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
if not len(bounding_boxes) == 0:
for face in bounding_boxes:
if face[4] > 0.50:
det = np.squeeze(face[0:4])
bb = np.zeros(4, dtype=np.int32)
bb[0] = np.maximum(det[0] - margin / 2, 0)
bb[1] = np.maximum(det[1] - margin / 2, 0)
bb[2] = np.minimum(det[2] + margin / 2, img_size[1])
bb[3] = np.minimum(det[3] + margin / 2, img_size[0])
cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
resized = cv2.resize(cropped, (input_image_size,input_image_size),interpolation=cv2.INTER_CUBIC)
prewhitened = facenet.prewhiten(resized)
faces.append('face':resized,'rect':[bb[0],bb[1],bb[2],bb[3]],'embedding':getEmbedding(prewhitened))
return faces
def getEmbedding(resized):
reshaped = resized.reshape(-1,input_image_size,input_image_size,3)
feed_dict = images_placeholder: reshaped, phase_train_placeholder: False
# print(feed_dict)
embedding = sess.run(embeddings, feed_dict=feed_dict)
return embedding
img = cv2.imread(args.img)
img = imutils.resize(img,width=1000)
faces = getFace(img)
for face in faces:
print("Embeddings = "+str(face['embedding']))
cv2.waitKey(0)
cv2.destroyAllWindows()
【问题讨论】:
当您只发布图片时,您的帖子看起来像垃圾邮件,其他人需要能够复制和粘贴您的代码来帮助您。请在此处发布您的代码,以便社区可以帮助您。此外,请确保对代码使用 4 个空格缩进,以便它显示为代码块。编码愉快! 删除链接,添加图片。 【参考方案1】:您have to have the __init__.py
in the package directory 被识别为一个包裹。它可以是一个空文件,但它必须存在。 align
目录中没有这个。
来自documentation:
需要 __init__.py 文件才能使 Python 将目录视为包含包
根据您的评论,错误
用法:face_match_demo.py [-h] --img1 IMG1 --img2 IMG2 face_match_demo.py:错误:不明确的选项:--img=images/faces.jpg 可以匹配--img2,--img1
表示face_match_demo.py
实际上是一个匹配两张图片的实用程序,以判断它们是否包含相同的脸。所以你必须给它提供两张图片,它会告诉你这张脸是否相同。您需要使用 --img1 和 --img2 选项来执行此操作:
python face_match_demo.py --img1 images/faces.jpg --img2 [[another face image]]
【讨论】:
那么就在align目录下创建一个名为init.py的空文件? 是的,但是前后有两个下划线,所以 __init__.py 哦,我看到引号中的下划线变成了粗体格式,让我来解决这个问题。 非常感谢,它适用于嵌入代码,但对于面部匹配演示,我收到此错误:“usage: face_match_demo.py [-h] --img1 IMG1 --img2 IMG2 face_match_demo。 py: error: ambiguous option: --img=images/faces.jpg 可以匹配 --img2, --img1" 更新答案。以上是关于为啥我在 facenet 中收到此错误?的主要内容,如果未能解决你的问题,请参考以下文章