1

I'm trying to create a hand tracker using opencv, mediapipe and the time module. It works perfectly when I run it in my IDE, but when I try to turn it into an exe using the following pyinstaller command: "pyinstaller -w -F HandTrackingProgram.py", it says a bunch of stuff and at the end, gives the following error "IndexError: tuple index out of range". Here's my code:

Here's the code:

import cv2
import mediapipe as mp
import time

cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils

pTime = 0
cTime = 0

while True:
    success, img = cap.read()
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = hands.process(imgRGB)
    if results.multi_hand_landmarks:
        for handLms in results.multi_hand_landmarks:
            for id, lm in enumerate(handLms.landmark):
                h, w, c = img.shape
                cx, cy = int(lm.x*w), int(lm.y*h)
            mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)

    cTime = time.time()
    fps = 1/(cTime-pTime)
    pTime = cTime

    cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)

    cv2.imshow("Image", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

I suspect that the problem might be with the mediapipe library because when I used Python 3.11, mediapipe wouldn't install. So, I used 3.10. Mediapipe successfully installed but now, I'm facing this issue. The entire error message is too long and apparently looks like spam, so here's an image instead (this isn't the entire error message": enter image description here

**UPDATE: ** I was able to convert into an exe somehow but when I run the exe file, I get this error, " Traceback (most recent call last): File "nauru.py", line 7, in File "mediapipe\python\solutions\hands.py", line 114, in init File "mediapipe\python\solution_base.py", line 264, in init FileNotFoundError: The path does not exist. "

2
  • Does this answer your question? IndexError: tuple index out of range PyInstaller Commented May 23, 2023 at 6:22
  • I implemented the suggested solution but it doesn't work. Still the same error. Commented May 23, 2023 at 6:33

1 Answer 1

0

Using the approach described in this answer I managed to solve the FileNotFoundError: The path does not exist issue.

You can use the following HandTrackingProgram.spec to build single-file exe by calling python -m PyInstaller HandTrackingProgram.spec:

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None

def get_mediapipe_path():
    import mediapipe
    mediapipe_path = mediapipe.__path__[0]
    return mediapipe_path

a = Analysis(
    ['HandTrackingProgram.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

mediapipe_tree = Tree(get_mediapipe_path(), prefix='mediapipe', excludes=["*.pyc"])
a.datas += mediapipe_tree
a.binaries = filter(lambda x: 'mediapipe' not in x[0], a.binaries)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='HandTrackingProgram',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

The compiled exe works well (gets data from the camera and tracks hands).

Note: I use Python 3.9 (mediapipe 0.9.0), but hopefully, it will also work for Python 3.10.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.