CX_Freeze 没有响应 Pygame 和 Pykinect

Posted

技术标签:

【中文标题】CX_Freeze 没有响应 Pygame 和 Pykinect【英文标题】:CX_Freeze come up not responding with Pygame and Pykinect 【发布时间】:2016-02-25 15:49:39 【问题描述】:

我的代码在这里:

import thread
import pygame
from pykinect import nui
import os
import Tkinter as tk
import tkMessageBox
root = tk.Tk()
root.withdraw()

DEPTH_WINSIZE = (640, 480)

screen_lock = thread.allocate()
screen = None

tmp_s = pygame.Surface(DEPTH_WINSIZE, 0, 16)

s=None
global DIR
k = open("DIR.dir", "r")
DIR = k.read()
if (not os.path.exists("Patients\\" + DIR)):
    os.makedirs("Patients\\" + DIR)
k.close()
def exit():
    exit()
def video_frame_ready(frame):
    frame.image.copy_bits(screen._pixels_address)
    pygame.display.update()
    global s
    if (s == 4):
        R = open("Patients\\" + DIR + "\\" + "RED.clr", "w")
        G = open("Patients\\" + DIR + "\\" + "GREEN.clr", "w")
        B = open("Patients\\" + DIR + "\\" + "BLUE.clr", "w")
        for i in range(screen.get_height()):
            for i1 in range(screen.get_width()):
                R.write(str(screen.get_at((i1, i))[0]) + " ")
                G.write(str(screen.get_at((i1, i))[1]) + " ")
                B.write(str(screen.get_at((i1, i))[2]) + " ")
            R.write("\n")
            G.write("\n")
            B.write("\n")
        R.close()
        G.close()
        B.close()
def depth_frame_ready(frame):
    with screen_lock:
        global arr
        frame.image.copy_bits(tmp_s._pixels_address)
        arr.append(pygame.surfarray.pixels2d(tmp_s))
        arr2d = (pygame.surfarray.pixels2d(tmp_s) >> 7) & 255

        global s
        s=s+1
        if (s==5):
            tkMessageBox.showinfo("Points", "Select Some Points in the Window")
            click = 0
            coords = []
            while (True):
                action = pygame.mouse.get_pressed()
                if (action[0]):
                    if (click != 0):
                        if (coords[-1] == pygame.mouse.get_pos()):
                            break
                    coords.append([])
                    coords[click] = pygame.mouse.get_pos()
                    pygame.time.wait(300)
                    DEPTH_MULT = arr[3][coords[click][0]][coords[click][1]] / 8800.0
                    MULT_FINAL = 1 / DEPTH_MULT
                    screen.set_at((coords[click][0]-1, coords[click][1]-1), (255, 0, 0))
                    arr2d_N = (pygame.surfarray.pixels2d(tmp_s) >> 7) & 255
                    pygame.display.update()
                    click = click + 1
                    action = pygame.mouse.get_pressed()
            File = open("NM.dta", "w")
            File.write(str(click))
            File.close()
            tkMessageBox.showinfo("Finished", "Scanning Finished, Please Wait...")
            File = open("Patients\\" + DIR + "\\coords_MLT.crd", "w")
            FILE = open("Patients\\" + DIR + "\\coords_MLT_VIDEO.crd", "w")
            for i in range (len(coords)):
                for i1 in range (2):
                    DEPTH_MULT = arr[3][coords[i][0]][coords[i][1]] / 8800.0
                    MULT_FINAL = 1 / DEPTH_MULT
                    if (i1 == 0):
                        File.write(str(coords[i][i1] - int(19 * MULT_FINAL)) + " ")
                        FILE.write(str(coords[i][i1]) + " ")
                    else:
                        File.write(str(coords[i][i1] - int(22 * MULT_FINAL)) + " ")
                        FILE.write(str(coords[i][i1]) + " ")
                File.write("\n")
                FILE.write("\n")
            File.close()
            FILE.close()
            for k in range(len(arr)):
                test = open("Patients\\" + DIR + "\\out"+str(k)+".dpm", "w")
                for i in arr[k]:
                    for i1 in i:
                        test.write(str(i1) + " ")
                    test.write("\n")
                test.close()
            pygame.quit()
            k = open("Finished.FNS", "w")
            k.write("FNS")
            k.close()
            os.startfile("Kinect_Matrix_generation_FINAL_VERSION.exe")
            pygame.time.wait(100)
            exit()

def main():
    pygame.init()
    global s
    s=0
    global arr
    arr = []

    global screen
    screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 32)
    pygame.display.set_caption("Skelenct Scan")

    angle = 0
    with nui.Runtime() as kinect:
        kinect.depth_frame_ready += depth_frame_ready
        kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Depth)

        kinect.video_frame_ready += video_frame_ready
        kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Color)

        # Main game loop
        while (True):
            event = pygame.event.wait()

            if (event == pygame.QUIT):
                break
if (__name__ == "__main__"):
    import sys
    main()
    sys.exit(app.exec_())

这是我的 setup.py 文件:

import cx_Freeze
import sys

base = None

if (sys.platform == "win32"):
    base = "Win32GUI"
Executables = [          cx_Freeze.Executable("Kinect_wint_Python_Multi_Points_Final_With_Video.py", base = base, icon = "Icon.ico")]

cx_Freeze.setup(
    name = "Skelenect",
    options = "build_exe": "packages":["thread", "pygame", "sys", "os"], "include_files":["Icon.ico", "Icon.png", "SKEL_PIC.png"],
    version = "0.1",
    description = "Body Scanning Software",
    executables = Executables
    )

当这个程序运行时,它工作正常,直到它想运行另一个 .exe 文件并退出。它总是说它没有响应,但程序在使用 CX_Freeze 冻结它之前运行良好。

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

您只需将 pygame.quit() 方法向下移动并添加一个 try/except 语句并使用 os._exit(0)

像这样:

import thread
import pygame
import easygui
from pykinect import nui
import os
import Tkinter as tk
import tkMessageBox
root = tk.Tk()
root.withdraw()


DEPTH_WINSIZE = (640, 480)

screen_lock = thread.allocate()
screen = None

tmp_s = pygame.Surface(DEPTH_WINSIZE, 0, 16)

s=None
global DIR
k = open("DIR.dir", "r")
DIR = k.read()
if (not os.path.exists("Patients\\" + DIR)):
    os.makedirs("Patients\\" + DIR)
k.close()
def video_frame_ready(frame):
    frame.image.copy_bits(screen._pixels_address)
    pygame.display.update()
    global s
    if (s == 4):
        R = open("Patients\\" + DIR + "\\" + "RED.clr", "w")
        G = open("Patients\\" + DIR + "\\" + "GREEN.clr", "w")
        B = open("Patients\\" + DIR + "\\" + "BLUE.clr", "w")
        for i in range(screen.get_height()):
            for i1 in range(screen.get_width()):
                R.write(str(screen.get_at((i1, i))[0]) + " ")
                G.write(str(screen.get_at((i1, i))[1]) + " ")
                B.write(str(screen.get_at((i1, i))[2]) + " ")
            R.write("\n")
            G.write("\n")
            B.write("\n")
        R.close()
        G.close()
        B.close()
def depth_frame_ready(frame):
    with screen_lock:
        global arr
        frame.image.copy_bits(tmp_s._pixels_address)
        arr.append(pygame.surfarray.pixels2d(tmp_s))
        arr2d = (pygame.surfarray.pixels2d(tmp_s) >> 7) & 255

        global s
        s=s+1
        if (s==5):
            click = 0
            coords = []
            while (True):
                action = pygame.mouse.get_pressed()
                if (action[0]):
                    if (click != 0):
                        if (coords[-1] == pygame.mouse.get_pos()):
                            break
                    coords.append([])
                    coords[click] = pygame.mouse.get_pos()
                    pygame.time.wait(300)
                    DEPTH_MULT = arr[3][coords[click][0]][coords[click][1]] / 8800.0
                    MULT_FINAL = 1 / DEPTH_MULT
                    screen.set_at((coords[click][0]-1, coords[click][1]-1), (255, 0, 0))
                    arr2d_N = (pygame.surfarray.pixels2d(tmp_s) >> 7) & 255
                    pygame.display.update()
                    click = click + 1
                    action = pygame.mouse.get_pressed()
            File = open("NM.dta", "w")
            File.write(str(click))
            File.close()
            tkMessageBox.showinfo("Finished", "Scanning Finished, Please Wait...")
            File = open("Patients\\" + DIR + "\\coords_MLT.crd", "w")
            FILE = open("Patients\\" + DIR + "\\coords_MLT_VIDEO.crd", "w")
            for i in range (len(coords)):
                for i1 in range (2):
                    DEPTH_MULT = arr[3][coords[i][0]][coords[i][1]] / 8800.0
                    MULT_FINAL = 1 / DEPTH_MULT
                    if (i1 == 0):
                        File.write(str(coords[i][i1] - int(19 * MULT_FINAL)) + " ")
                        FILE.write(str(coords[i][i1]) + " ")
                    else:
                        File.write(str(coords[i][i1] - int(22 * MULT_FINAL)) + " ")
                        FILE.write(str(coords[i][i1]) + " ")
                File.write("\n")
                FILE.write("\n")
            File.close()
            FILE.close()
            for k in range(len(arr)):
                test = open("Patients\\" + DIR + "\\out"+str(k)+".dpm", "w")
                for i in arr[k]:
                    for i1 in i:
                        test.write(str(i1) + " ")
                    test.write("\n")
                test.close()
            k = open("Finished.FNS", "w")
            k.write("FNS")
            k.close()
            os.startfile("Kinect_Matrix_generation_FINAL_VERSION.exe")
            pygame.time.wait(100)
            global G
            G = False
            os._exit(0)
            pygame.quit()


def main():
    pygame.init()
    global s
    s=0
    global arr
    arr = []

    global G
    G = True

    global screen
    screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 32)
    pygame.display.set_caption("Skelenct Scan")

    angle = 0
    with nui.Runtime() as kinect:
        kinect.depth_frame_ready += depth_frame_ready
        kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Depth)

        kinect.video_frame_ready += video_frame_ready
        kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Color)

        while (G):
            try:
                event = pygame.event.wait()

                if (event == pygame.QUIT):
                    break
            except :
                os._exit(0)
if (__name__ == "__main__"):
    import sys
    main()
    sys.exit(app.exec_())

安装脚本不需要任何更改。

【讨论】:

以上是关于CX_Freeze 没有响应 Pygame 和 Pykinect的主要内容,如果未能解决你的问题,请参考以下文章

Pygame 和 cx_freeze:分段错误

无法运行播放通过 cx_Freeze 编译的 OGG 文件的 Pygame 脚本

cx_Freeze 'list' 对象没有属性 'items'

Pygame cx_freeze 语法错误

cx_Freeze 将 Python 和 Pygame 文档编译为 .exe 文件时出现奇怪的错误

使用 pygame 在 cx_Freeze 中包含整个文件夹