py 到 exe 错误。 (FileNotFoundError:路径不存在。)

问题描述 投票:0回答:3

所以我正在开发一个项目,检测手的移动并用它来控制电脑鼠标,一切都很顺利,但现在我需要将其转换为 exe 文件。 当我在 pysharm 或 CMD 控制台中运行我的代码时,它工作得很好,但是当我将其转换为 exe 文件时,我无法打开它,并且它不断向我显示一些我无法解决或在线找到任何解决方案的错误,我将非常感谢您的帮助,因为我真的需要它(这个代码不是我的,我把它放在网上并做了一些修改)

系统信息: 蟒蛇3.7.8
pyinstaller 4.8
媒体管道 0.8.9.1
尸检 4.0.0
opencv 4.5.5.62
numpy 1.21.5
视窗 10

我的第一个代码:

import cv2
import mediapipe as mp
import math


class HandDetector:
    def __init__(self,
                 mode=False,
                 max_num_hands=1,
                 complexity=1,
                 detection=0.5,
                 track=0.5):
        self.mode = mode
        self.maxHands = max_num_hands
        self.complexity = complexity
        self.detectionCon = detection
        self.trackCon = track
        self.mpHands = mp.solutions.hands
        self.hands = self.mpHands.Hands(self.mode,
                                        self.maxHands,
                                        self.complexity,
                                        self.detectionCon,
                                        self.trackCon)
        self.mpDraw = mp.solutions.drawing_utils
        self.tipIds = [4, 8, 12, 16, 20]

    def findHands(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.hands.process(imgRGB)

        if self.results.multi_hand_landmarks:
            for handLms in self.results.multi_hand_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img, handLms,
                                               self.mpHands.HAND_CONNECTIONS)

        return img

    def findPosition(self, img, handNo=0, draw=True):
        xlist = []
        ylist = []
        bbox = []
        self.lmList = []
        if self.results.multi_hand_landmarks:
            myHand = self.results.multi_hand_landmarks[handNo]
            for id, lm in enumerate(myHand.landmark):
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                xlist.append(cx)
                ylist.append(cy)
                self.lmList.append([id, cx, cy])
                if draw:
                    cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)

            xmin, xmax = min(xlist), max(xlist)
            ymin, ymax = min(ylist), max(ylist)
            bbox = xmin, ymin, xmax, ymax

            if draw:
                cv2.rectangle(img, (xmin - 20, ymin - 20), (xmax + 20, ymax + 20),
                              (0, 255, 0), 2)

        return self.lmList, bbox

    def fingersUp(self):
        fingers = []

        # Thumb
        if self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 1][1]:
            fingers.append(1)
        else:
            fingers.append(0)

        # Fingers
        for id in range(1, 5):

            if self.lmList[self.tipIds[id]][2] < self.lmList[self.tipIds[id] - 2][2]:
                fingers.append(1)
            else:
                fingers.append(0)

        return fingers

    def findDistance(self, p1, p2, img, draw=True, r=15, t=3):
        x1, y1 = self.lmList[p1][1:]
        x2, y2 = self.lmList[p2][1:]
        cx, cy = (x1 + x2) // 2, (y1 + y2) // 2

        if draw:
            cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), t)
            cv2.circle(img, (x1, y1), r, (255, 0, 255), cv2.FILLED)
            cv2.circle(img, (x2, y2), r, (255, 0, 255), cv2.FILLED)
            cv2.circle(img, (cx, cy), r, (0, 0, 255), cv2.FILLED)
        length = math.hypot(x2 - x1, y2 - y1)

        return length, img, [x1, y1, x2, y2, cx, cy]


def main():
    cam = cv2.VideoCapture(1, cv2.CAP_DSHOW)
    detector = HandDetector()
    while True:
        success, img = cam.read()
        img = detector.findHands(img)
        lmlist, bbox = detector.findPosition(img)
        if len(lmlist) != 0:
            print(lmlist[4])

        cv2.imshow("Image", img)
        cv2.waitKey(1)


if __name__ == "__main__":
    main()

第二个代码:

import cv2
import first as htm
import numpy as np
import autopy
import mouse

wCam, hCam = 640, 480
frameR = 90
smoothening = 7
plocX, plocY = 0, 0
clocX, clocY = 0, 0

cam = cv2.VideoCapture(1, cv2.CAP_DSHOW)
cam.set(3, wCam)
cam.set(4, hCam)
detector = htm.HandDetector(max_num_hands=1)
wScr, hScr = autopy.screen.size()


while True:

    # 1. Find hand Landmarks
    success, img = cam.read()
    img = detector.findHands(img)
    lmList, bbox = detector.findPosition(img)

    # 2. Get the tip of the index and thump
    if len(lmList) != 0:
        x1, y1 = lmList[8][1:]
        x2, y2 = lmList[4][1:]

        # 3. Check which fingers are up
        fingers = detector.fingersUp()
        cv2.rectangle(img, (frameR, frameR), (wCam - frameR, 250),
                      (255, 0, 255), 2)

        # 4. Only Index Finger : Moving Mode
        if fingers[1] == 1:

            # 5. Convert Coordinates
            x3 = np.interp(x1, (frameR, wCam - frameR), (0, wScr))
            y3 = np.interp(y1, (frameR, 250), (0, hScr))

            # 6. Smoothen Values
            clocX = plocX + (x3 - plocX) / smoothening
            clocY = plocY + (y3 - plocY) / smoothening

            # 7. Move Mouse
            autopy.mouse.move(wScr - clocX, clocY)
            cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED)
            plocX, plocY = clocX, clocY

            # 8. Find distance between fingers
            length, img, lineInfo = detector.findDistance(4, 8, img)
            length_2, img, lineInfo_2 = detector.findDistance(8, 20, img)

            # 9. Click mouse if distance long / Short
            if length > 125:
                cv2.circle(img, (lineInfo[4], lineInfo[5]),
                           15, (0, 255, 0), cv2.FILLED)
                mouse.click('left')
            if length_2 < 115:
                mouse.click('right')

    # 10. Display
    cv2.imshow("Image", img)
    cv2.waitKey(1)

错误:

Traceback (most recent call last):
  File "Second.py", line 16, in <module>
    detector = htm.HandDetector(max_num_hands=1)
  File "first.py", line 23, in __init__
    self.trackCon)
  File "mediapipe\python\solutions\hands.py", line 129, in __init__
  File "mediapipe\python\solution_base.py", line 238, in __init__
FileNotFoundError: The path does not exist.
python windows pyinstaller exe mediapipe
3个回答
1
投票

我在windows10上以这种方式解决了它。 样本

*.spec

:

block_cipher = None

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


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)




0
投票
pyinstaller --onefile main.py

这将构建应用程序文件(未处于工作状态)和规范文件。删除除规范文件之外的所有内容,并根据 

https://python.tutorialink.com/issues-compiling-mediapipe-with-pyinstaller-on-macos/

对规范文件进行更改 。 现在我们将使用命令根据此更新的规范文件构建应用程序文件:pyinstaller main.spec

(main 是规范文件的名称)。现在已构建了一个可工作的 exe 文件。


0
投票
mediapipe

库的安装路径。对我来说,路径是:

/home/arrafi/squat-counting-application/venv/lib/python3.10/site-packages/mediapipe

然后在调用 
pyinstaller

模块时使用 --add-data 参数和 mediapipe-installed-path 手动添加路径,然后正确构建:

pyinstaller --onedir --add-data "/home/arrafi/squat-counting-application/venv/lib/python3.10/site-packages/mediapipe:mediapipe/" --icon icon.ico app.py

所以,对你来说,将会是:

pyinstaller --onedir --add-data "your_mediapipe_installed_path:mediapipe/" --icon icon.ico app.py

现在,
pyinstaller

将正确找到路径,您可以对面临相同问题的任何其他库使用类似的方法。

    

© www.soinside.com 2019 - 2024. All rights reserved.