我的 Django 应用程序无法在视图中打开 shape_predictor_68_face_landmarks.dat。但是在简单的 .py 文件中,此代码有效。请帮助解决这个问题。我曾尝试使用线程和异步,但它不起作用。
class Main(View):
def post(self, request):
images = Images.objects.all()
image = request.FILES.get('image')
name = request.POST.get('name')
if not image and not name:
# images = Images.objects.all()
return render(request, 'find_photo/main.html', context={'images': images, 'result': True, })
group = Groups.objects.create(photo=image, title=name,)
find_photos("../" + group.photo.url)
# x = threading.Thread(target=find_photos, args=("../" + group.photo.url,))
# x.setDaemon(True)
# x.start()
#
# print(x.is_alive())
# mypath = '/find_photos/'
# onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
#
# for file in onlyfiles:
# print(file)
# group.find_images.add(file)
# group.save()
return render(request, 'find_photo/main.html', context={'images': images, })
find_photos 函数:
def find_photos(image):
sp = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
face_rec = dlib.face_recognition_model_v1('dlib_face_recognition_resnet_model_v1.dat')
detector = dlib.get_frontal_face_detector()
min_distance = 2
f1 = get_face_descriptors(image, sp, face_rec, detector)[0]
files = getfilelist(dirpath)
flag = 0
for f in files:
flag = flag + 1
print('Анализ ' + f + ' - ' + str(flag) + ' фото из ' + str(vsego))
if os.path.exists(f):
try:
findfaces = get_face_descriptors(f)
print('На фото: ' + str(len(findfaces)) + ' лиц')
for f2 in findfaces:
if (f2 != []):
euc_distance = distance.euclidean(f1, f2)
print(euc_distance)
if euc_distance < 0.65:
print('Найдено лицо: ' + f)
shutil.copyfile(f, resultpath + str(flag) + '.jpg')
except:
continue
print("DONE")