依赖dlib的python可执行文件不起作用

时间:2019-11-22 14:10:16

标签: python python-3.x opencv3.0 face-recognition dlib

大家好,我有一个依赖于dlib的python脚本,例如import dlib,现在我已经创建了一个可执行文件(使用pyinstaller),并且在我的机器上运行良好,但给ImportError:DLL加载失败:动态链接库(DLL)初始化例程在另一台计算机上失败。在找出发生这种情况的行之后,基本上是导入dlib,这使我认为dlib没有正确包含在我的可执行文件中。我的dlib版本19.18.0和我尝试在其上运行exe的另一台计算机未安装python。需要帮助另一台计算机上的错误

F:\FaceRecogDemo\FaceRecogDemo\dist>recognizefaces.exe --debug --encodings ../encodings.pickle --image ../example1.jpg
Traceback (most recent call last):
  File "D:\FaceRecogDemo\recognizefaces.py", line 2, in <module>
  File "c:\programdata\anaconda3\envs\mywindowscv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
  File "D:\FaceRecogDemo\face_recognition\__init__.py", line 7, in <module>
  File "c:\programdata\anaconda3\envs\mywindowscv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
  File "D:\FaceRecogDemo\face_recognition\api.py", line 4, in <module>
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
[14720] Failed to execute script recognizefaces

我的ogniceaces.py脚本

import face_recognition
import argparse
import pickle
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--encodings", required=True,
    help="path to serialized db of facial encodings")
ap.add_argument("-i", "--image", required=True,
    help="path to input image")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
    help="face detection model to use: either `hog` or `cnn`")
args = vars(ap.parse_args())
# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(args["encodings"], "rb").read())

# load the input image and convert it from BGR to RGB
image = cv2.imread(args["image"])
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# detect the (x, y)-coordinates of the bounding boxes corresponding
# to each face in the input image, then compute the facial embeddings
# for each face
print("[INFO] recognizing faces...")
boxes = face_recognition.face_locations(rgb,
    model=args["detection_method"])
encodings = face_recognition.face_encodings(rgb, boxes)

# initialize the list of names for each face detected
names = []
# loop over the facial embeddings
for encoding in encodings:
    # attempt to match each face in the input image to our known
    # encodings
    matches = face_recognition.compare_faces(data["encodings"],
        encoding)
    name = "Unknown"
    # check to see if we have found a match
    if True in matches:
        # find the indexes of all matched faces then initialize a
        # dictionary to count the total number of times each face
        # was matched
        matchedIdxs = [i for (i, b) in enumerate(matches) if b]
        counts = {}

        # loop over the matched indexes and maintain a count for
        # each recognized face face
        for i in matchedIdxs:
            name = data["names"][i]
            counts[name] = counts.get(name, 0) + 1

        # determine the recognized face with the largest number of
        # votes (note: in the event of an unlikely tie Python will
        # select first entry in the dictionary)
        name = max(counts, key=counts.get)

    # update the list of names
    names.append(name)
print(names)

我的机器都具有Windows 10操作系统

2 个答案:

答案 0 :(得分:2)

Pyinstaller似乎由于某种原因未选择dlib。在命令行上构建二进制文件时,请尝试使用以下标志pyinstaller --hidden-import dlib将dlib显式添加到捆绑软件中。

https://pyinstaller.readthedocs.io/en/stable/usage.html#what-to-bundle-where-to-search

答案 1 :(得分:0)

  1. 问题也可能出在环境变量中,可以在执行文件的机器中检查%PATH%吗?多个python版本及其在PATH中的正确配置。

  2. 问题也可能是由于Visual C ++发行版所致,请检查两台计算机上的发行版是否相同。

  3. 尝试在路径变量中添加opencv DLL并检查。问题是anaconda发行版中缺少python3.dll。您可以下载python二进制文件here,然后从zip归档文件中提取dll。将其放在PATH的文件夹中(例如C:\ Users \ MyName \ Anaconda3),导入应该可以进行。

Can't import cv2; "DLL load failed"

相关问题