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

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

大家好,我有一个依赖于 dlib 的 python 脚本,例如 import dlib 现在我已经创建了一个可执行文件(使用 pyinstaller),它在我的机器上运行良好,但给出了 ImportError: DLL load failed: A Dynamic link库 (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

我的 recognizefaces.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 操作系统

最佳答案

由于某种原因,Pyinstaller 似乎没有获取 dlib。当您在命令行上构建二进制文件时,请尝试使用以下标志 pyinstaller --hidden-import dlib 显式地将 dlib 添加到 bundle 中。

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

关于依赖于 dlib 的 python 可执行文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58995969/

相关文章:

opencv - OpenCV:Lucas Kanade应用于特定区域(以检测面部特征)

c++ - 灰度图像创建 16 位

python - 乘法结果为负零

python - 一切正常,但无法将数据添加到 mysql 数据库

python - 通过计算传播 NaN

Python。 from dateutil.relativedelta import * 在 shell 中有效,但在脚本中无效

python - 第 293 行的 OpenCV 错误 : Assertion failed in undistort. cpp

python - sparql.query().convert() 处 SPARQLWrapper 出现 URLError

python - 由于缺乏传递引用,如何在 python 2 中保持干燥

python - 为什么 QTabBar 给我错误的索引号?