所以我正在做一个项目,检测手的移动并用它来控制电脑鼠标,一切都很顺利,但现在我需要将它转换为exe文件。当我在pysharm或CMD控制台中运行我的代码时,它工作得很好,但当我将我转换为exe文件时,我无法打开它,它不断向我显示一些我无法解决或在线找到任何解决方案的错误,我会非常乐意得到帮助,因为我真的需要它(这段代码不是我的,我把它带到网上,并对它进行了一点修改(
系统信息:python 3.7.8
pyinstaller 4.8
mediapipe 0.8.9.1
autopy 4.0.0
opencv 4.5.5.62
numpy 1.21.5
windows 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 "mediapipepythonsolutionshands.py", line 129, in __init__
File "mediapipepythonsolution_base.py", line 238, in __init__
FileNotFoundError: The path does not exist.
我在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)
首先创建规范文件,然后根据该文件构建应用程序。因此,您首先运行命令:pyinstaller --onefile main.py
这将生成应用程序文件(不处于工作状态(和规范文件。删除除等级库文件外的所有内容,并根据https://python.tutorialink.com/issues-compiling-mediapipe-with-pyinstaller-on-macos/。现在,我们将使用命令pyinstaller main.spec
(main是规范文件的名称(根据此更新的规范文件构建应用程序文件。现在构建了一个可工作的exe文件。