如何将Python文件转为导入cv2的exe文件

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

嗨,我有一个问题,我的代码变成了可执行文件,屏幕上会出现一条错误消息,有人知道如何修复此错误吗?这是针对某些身体跟踪软件的,真正的原因是 cv2,但我需要一些帮助使其成为可执行文件,以便打开时不会出现错误消息非常感谢雅各布,请帮助

[1]:https://i.sstatic.net/LKJ5r.png

import cv2
import mediapipe as mp
import pandas as pd
from ast import literal_eval
from tkinter import *
from tkinter import filedialog
import numpy





root = Tk()
#create lebel widget
ScreenLabel = Label(root, text="Body tracking")
#put on screen
root.geometry("500x450+700+200")

#def open():
    #global track
    #track = filedialog.askopenfilename(initialdir="/dowloads", title="select A File", filetypes=([("all files","*.*")]))
e = Entry(root, width=100)
e.pack()  

def tracking():
    mp_drawing = mp.solutions.drawing_utils
    mp_pose = mp.solutions.pose

    video_file_path = e.get()
    cap = cv2.VideoCapture(video_file_path)

    coordinates_list = []

    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))
    size = (frame_width, frame_height)
    fps = 30

    v_split = video_file_path.split(".")
    v_split[-2] = v_split[-2] + "_processed"
    prep_vid_file_path = ".".join(v_split)

    video_saver = cv2.VideoWriter(prep_vid_file_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)

    keypoints_list = ['CENTER_HEAD',
    'LEFT_SHOULDER',
    'RIGHT_SHOULDER',
    'LEFT_ELBOW',
    'RIGHT_ELBOW',
    'LEFT_WRIST',
    'RIGHT_WRIST',
    'LEFT_PINKY',
    'RIGHT_PINKY',
    'LEFT_INDEX',
    'RIGHT_INDEX',
    'LEFT_THUMB',
    'RIGHT_THUMB',
    'LEFT_HIP',
    'RIGHT_HIP',
    'LEFT_KNEE',
    'RIGHT_KNEE',
    'LEFT_ANKLE',
    'RIGHT_ANKLE',
    'LEFT_HEEL',
    'RIGHT_HEEL',
    'LEFT_FOOT_INDEX',
    'RIGHT_FOOT_INDEX']

    with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:

        while True:

            ret, frame = cap.read()
            if ret == False:
                cap.release()
                video_saver.release()
                cv2.destroyAllWindows()
                break

            image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image.flags.writeable = False

            results = pose.process(image)

            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            timestamp_name = str(cap.get(cv2.CAP_PROP_POS_MSEC))

            try:
                landmarks = results.pose_landmarks.landmark
                CENTER_HEAD = (landmarks[mp_pose.PoseLandmark.NOSE.value].x, landmarks[mp_pose.PoseLandmark.NOSE.value].y, landmarks[mp_pose.PoseLandmark.NOSE.value].z)
                LEFT_SHOULDER = (landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].z)
                RIGHT_SHOULDER = (landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].z)
                LEFT_ELBOW = (landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].z)
                RIGHT_ELBOW = (landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].z)
                LEFT_WRIST = (landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y, landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].z)
                RIGHT_WRIST = (landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].z)
                LEFT_PINKY = (landmarks[mp_pose.PoseLandmark.LEFT_PINKY.value].x, landmarks[mp_pose.PoseLandmark.LEFT_PINKY.value].y, landmarks[mp_pose.PoseLandmark.LEFT_PINKY.value].z)
                RIGHT_PINKY = (landmarks[mp_pose.PoseLandmark.RIGHT_PINKY.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_PINKY.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_PINKY.value].z)
                LEFT_INDEX = (landmarks[mp_pose.PoseLandmark.LEFT_INDEX.value].x, landmarks[mp_pose.PoseLandmark.LEFT_INDEX.value].y, landmarks[mp_pose.PoseLandmark.LEFT_INDEX.value].z)
                RIGHT_INDEX = (landmarks[mp_pose.PoseLandmark.RIGHT_INDEX.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_INDEX.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_INDEX.value].z)
                LEFT_THUMB = (landmarks[mp_pose.PoseLandmark.LEFT_THUMB.value].x, landmarks[mp_pose.PoseLandmark.LEFT_THUMB.value].y, landmarks[mp_pose.PoseLandmark.LEFT_THUMB.value].z)
                RIGHT_THUMB = (landmarks[mp_pose.PoseLandmark.RIGHT_THUMB.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_THUMB.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_THUMB.value].z)
                LEFT_HIP = (landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y, landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].z)
                RIGHT_HIP = (landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].z)
                LEFT_KNEE = (landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y, landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].z)
                RIGHT_KNEE = (landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].z)
                LEFT_ANKLE = (landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].y, landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].z)
                RIGHT_ANKLE = (landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].z)
                LEFT_HEEL = (landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].y, landmarks[mp_pose.PoseLandmark.LEFT_HEEL.value].z)
                RIGHT_HEEL = (landmarks[mp_pose.PoseLandmark.RIGHT_HEEL.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_HEEL.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_HEEL.value].z)
                LEFT_FOOT_INDEX = (landmarks[mp_pose.PoseLandmark.LEFT_FOOT_INDEX.value].x, landmarks[mp_pose.PoseLandmark.LEFT_FOOT_INDEX.value].y, landmarks[mp_pose.PoseLandmark.LEFT_FOOT_INDEX.value].z)
                RIGHT_FOOT_INDEX = (landmarks[mp_pose.PoseLandmark.RIGHT_FOOT_INDEX.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_FOOT_INDEX.value].y, landmarks[mp_pose.PoseLandmark.RIGHT_FOOT_INDEX.value].z)
                coordinates_list.append(str(timestamp_name) + " : " + str({'CENTER_HEAD':CENTER_HEAD,
                                                                            'LEFT_SHOULDER':LEFT_SHOULDER,
                                                                            'RIGHT_SHOULDER':RIGHT_SHOULDER,
                                                                            'LEFT_ELBOW':LEFT_ELBOW,
                                                                            'RIGHT_ELBOW':RIGHT_ELBOW,
                                                                            'LEFT_WRIST':LEFT_WRIST,
                                                                            'RIGHT_WRIST':RIGHT_WRIST,
                                                                            'LEFT_PINKY':LEFT_PINKY,
                                                                            'RIGHT_PINKY':RIGHT_PINKY,
                                                                            'LEFT_INDEX':LEFT_INDEX,
                                                                            'RIGHT_INDEX':RIGHT_INDEX,
                                                                            'LEFT_THUMB':LEFT_THUMB,
                                                                            'RIGHT_THUMB':RIGHT_THUMB,
                                                                            'LEFT_HIP':LEFT_HIP,
                                                                            'RIGHT_HIP':RIGHT_HIP,
                                                                            'LEFT_KNEE':LEFT_KNEE,
                                                                            'RIGHT_KNEE':RIGHT_KNEE,
                                                                            'LEFT_ANKLE':LEFT_ANKLE,
                                                                            'RIGHT_ANKLE':RIGHT_ANKLE,
                                                                            'LEFT_HEEL':LEFT_HEEL,
                                                                            'RIGHT_HEEL':RIGHT_HEEL,
                                                                            'LEFT_FOOT_INDEX':LEFT_FOOT_INDEX,
                                                                            'RIGHT_FOOT_INDEX':RIGHT_FOOT_INDEX}))
            
            
            except:
                pass


            mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)

            video_saver.write(image)
            cv2.imshow("Video Feed", image)
            cv2.waitKey(1)
            
    v_split = video_file_path.split(".")
    v_split[-1] = "txt"
    v_split[-2] = v_split[-2] + "_coordinates"
    coordinates_file_path = ".".join(v_split)

    with open(coordinates_file_path, "w") as f:
        f.write("\n".join(coordinates_list))

    v_split = video_file_path.split(".")
    v_split[-1] = "xlsx"
    v_split[-2] = v_split[-2] + "_excel"
    coordinates_excel_path = ".".join(v_split)

    def get_vals(x):
        vals = []
        for i in x:
            for j in i:
                vals.append(j)
        return vals

    df = pd.DataFrame(coordinates_list)
    df["frame"] = df[0].apply(lambda x: x.split(" : ")[0])
    df[0] = df[0].apply(lambda x: get_vals(literal_eval(x.split(" : ")[1]).values()))
    df_columns = []
    mapper_dict = {0:"x", 1:"y", 2:"z"}
    for keypoint in keypoints_list:
        for i in range(0,3):
            df_columns.append(keypoint+"_"+mapper_dict.get(i))
    df = pd.concat([df["frame"], pd.DataFrame(df[0].to_list(), columns=df_columns)], axis=1)
    df.to_excel(coordinates_excel_path, index=False)

#open_btn = Button( root, text="open File", command=open, borderwidth=0).pack()





ExportButton = Button(root,  text="Track", command=tracking ,borderwidth=0 ,fg="blue")
ExportButton.pack()

root.mainloop()
python exe execute
1个回答
0
投票

使用Cxfreeze和这个教程就在这里,这个工具甚至可以让你为最终的.exe文件创建安装程序

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