Im having an issue where a tkinter app, when converted to exe, will freeze and open a new window every time a YOLO model is loaded. My original program is quite extensive, so ive recreated one demonstrating the issue:
from io import StringIO
memory_buffer = StringIO()
sys.stdout = memory_buffer
import tkinter as tk
from ultralytics import YOLO
import cv2 as cv
global testimg
testimg = cv.imread("testimg.jpg")
class MyGUI:
def __init__(self):
self.root = tk.Tk() # root here is window name now
self.root.geometry("600x400")
self.root.columnconfigure(0, weight = 1)
self.root.rowconfigure(0, weight = 1)
self.root.rowconfigure(1, weight = 1)
self.root.rowconfigure(2, weight = 1)
self.buttonmodel1 = tk.Button(self.root, text = "Model 1", bg = "#c9c9c9", bd= 5, width = 200, command = self.button1pressed)
self.buttonmodel1.grid(row =0, column=0, sticky= "WENS", padx = 3)
self.buttonmodel2 = tk.Button(self.root, text = "Model 2", bg = "#c9c9c9", bd= 5, width = 200)
self.buttonmodel2.grid(row =1, column=0, sticky= "WENS", padx = 3)
self.buttonmodel3 = tk.Button(self.root, text = "Model 3", bg = "#c9c9c9", bd= 5, width = 200)
self.buttonmodel3.grid(row =2, column=0, sticky= "WENS", padx = 3)
self.root.mainloop()
def button1pressed(self):
global testimg
model1 = YOLO("model1.pt")
model2 = YOLO("model2.pt")
model3 = YOLO("model3.pt")
resultsmodel1 = model1.predict(testimg)
print("model1")
resultsmodel2 = model2.predict(testimg)
print("model2")
resultsmodel3 = model3.predict(testimg)
print("model3")
MyGUI()
converted to exe using:
pyinstaller --collect-data ultralytics Model_Test.py --onefile --windowed
with
from io import StringIO
memory_buffer = StringIO()
sys.stdout = memory_buffer
Used to mitigate the known issue with using ultralytics + pyinstaller:
https://github.com/ultralytics/ultralytics/issues/7064
Problem when converting Python with Ultralytics to EXE file
When debugging in VS 2022 there are no such issues, only once the program has been packed and run as an exe. All model files are in the same directory as the exe.
It appears that as soon as .predict()
is called the main window freezes, the console pops up momentarily, and a new identical window appears. If closed the program will resume as intended with no additional errors:
Seems like a very niche issue, I would guess something is going wrong across Ultralytics + Pyinstaller + Tkinter.
Please do advise or let me know if more information is needed.