3

Hello everyone i have a python script that has dependency on dlib such as import dlib now i have created an executable out of it (using pyinstaller) and it works fine on my machine but gives ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed on another machine. and after digging out the line at which this occurs is basically importing dlib which makes me think dlib is not getting properly included in my executable. My dlib version 19.18.0 and the other machine i am trying to run exe on does'nt have python installed.Need help Error on another machine

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

My recognizefaces.py script

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)

Both my machines have windows 10 OS

Jesus Fung
  • 76
  • 8
JayD
  • 748
  • 1
  • 13
  • 38
  • @JayD dlib are machine dependent. You need to make sure there is binary available for the machine on which that is being run. – Adnan Umer Nov 22 '19 at 14:21
  • @AdnanUmer ok and can you please tell me how to exactly find that out – JayD Nov 22 '19 at 14:22
  • What is the OS on which you compiled that binary and what is the OS running on target machine? Its pretty obvious from Stack trace that is Windows. But which version of windows? 32-bit or 64-bit? You can find that info in System Properties – Adnan Umer Nov 22 '19 at 14:24
  • @AdnanUmer windows10 EducationN the one where exe works fine and windows10 Pro N where exe throws error both 64 bit – JayD Nov 22 '19 at 14:27
  • Try compiling it for windows 10 Pro N. You may have to do that on the target machine. – Maxxik CZ Nov 27 '19 at 06:20
  • @MaxxikCZ ok but does this makes a difference generally?? – JayD Nov 27 '19 at 06:26
  • If I'd be compiling for X machine, I'd surely compile it on that specific machine, because the pyInstaller and such may rely on the specifics of that machine. I'm not an export though. Maybe it's possible to compile it on Y machine with Xs parameters but if that works it's surely harder to do. – Maxxik CZ Nov 27 '19 at 07:40
  • ok but dosent it defeats the purpose of pyinstaller i thought an exe generated for a 64 bit windows os shall run on another 64 bit windows os without the need to install python or anyother thing but i guess what you say implies that the windows os speciifc version needs to be taken into consideration as well – JayD Nov 27 '19 at 07:45
  • btw havent tried it will post an update if this works – JayD Nov 27 '19 at 07:46
  • PyInstaller doesn’t pick up all your dependencies. This happens sometimes. You’ve to look into the packaging step and read the PyInstaller manual. – fpbhb Dec 01 '19 at 17:30
  • [When things go wrong -- PyInstaller documentation](https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html) – Xukrao Dec 02 '19 at 19:57
  • Hi @JayD, Did you resolved this issue ? please guide me. I am facing same issue. – Ahmad Raza Aug 23 '21 at 14:04
  • @AhmadRaza no brother unfortunately not, i went ahead with installing python on the machine where the exe had to be run – JayD Aug 30 '21 at 05:12
  • @Jayd I resolved the issue my brother. I am posting my steps as answer. Try this, may be it works in your case as well. – Ahmad Raza Aug 30 '21 at 17:28

3 Answers3

3

Pyinstaller doesn't seem to be picking up dlib for some reason. When you construct your binary on the command line, try explicitly adding dlib to the bundle using the following flag pyinstaller --hidden-import dlib.

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

CognizantApe
  • 1,149
  • 10
  • 10
1

I have resolved the issue, In my case it was due to direct compilation of dlib (I don't know why).

Here are steps:

  • Create a new environment
  • Install other requirements of your project
  • Make sure visual studio is installed on your development PC
  • pip install cmake
  • pip install face_recognition
  • Now you can use dlib, as face_recognition will install dlib as well.
  • Now you can create exe file, if still problem persists copy dlib folder from site_packages folder into folder where your exe file is placed.

In my case, it works on two other version of windows, where it was not working previously

Ahmad Raza
  • 1,923
  • 2
  • 14
  • 19
0
  1. The problem might also be in the environmental variable , can you check the %PATH% in the machine where you have executed the file. Multiple python versions and its configured correctly in the PATH.

  2. The problem might also be due to the Visual C++ distribution , Check once you have the same distribution in both the machines.

  3. Try to add the opencv DLL's in the path variables and check.The problem is a missing python3.dll in the anaconda distribution. You can download the python binaries here and extract dll out of the zip archive. Put it in a folder in your PATH (e.g. C:\Users\MyName\Anaconda3) and the import should work.

Can't import cv2; "DLL load failed"

redhatvicky
  • 1,912
  • 9
  • 8