CX_Freeze come up not responding with Pygame and Pykinect

104 views Asked by At

I have my code here:

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_())

And this is my setup.py file:

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
    )

When this program runs, it works fine until it wants to run the other .exe file and exits. It always says that it is not responding but the program works fine before Freezing it with CX_Freeze.

Any help would be greatly appreciated.

1

There are 1 answers

0
Nvidip On BEST ANSWER

You just have to move the pygame.quit() method down and add a try/except statement and also use os._exit(0)

Like this:

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_())

The setup script does not need any changes.