My GUI with following code does not have threads.The image display hogs up lots of memory and GUI is blocked ,and I can only call one function at a time.Please suggest simple hacks to make the GUI faster.Anyways The Image processing tasks like Clustering takes 5-6 mins.
import wx
import sys
import os
import matplotlib
import OpenGL
import PIL
import time
from spectral.graphics.hypercube import hypercube
from spectral import *
init_graphics()
class RedirectText(object):
def __init__(self,awxTextCtrl):
self.out=awxTextCtrl
def write(self,string):
self.out.WriteText(string)
class Frame(wx.Frame):
def __init__(self, title,*args,**kwargs):
wx.Frame.__init__(self, None, title=title, size= (1000,85),style=wx.MINIMIZE_BOX|wx.CLOSE_BOX|wx.RESIZE_BORDER|wx.SYSTEM_MENU|wx.CAPTION|wx.CLIP_CHILDREN)
self.Bind(wx.EVT_CLOSE, self.OnClose)
panel=wx.Panel(self,-1)
self.button=wx.Button(panel,label="Open",pos=(0,0),size=(50,30))
self.button1=wx.Button(panel,label="Save",pos=(51,0),size=(50,30))
self.button2=wx.Button(panel,label="ROI",pos=(102,0),size=(50,30))
self.button3=wx.Button(panel,label="Tone",pos=(153,0),size=(50,30))
self.slider=wx.Slider(panel,pos=(204,0))
self.button4=wx.Button(panel,label="Header",pos=(305,0),size=(50,30))
self.button5=wx.Button(panel,label="Cluster",pos=(356,0),size=(50,30))
self.button6=wx.Button(panel,label="Cube",pos=(407,0),size=(50,30))
self.button7=wx.Button(panel,label="Gaussian",pos=(458,0),size=(50,30))
self.Bind(wx.EVT_BUTTON, self.OnCubeClick,self.button5)
self.Bind(wx.EVT_BUTTON, self.OnHeadClick,self.button4)
self.Bind(wx.EVT_BUTTON, self.OnSaveClick,self.button1)
self.Bind(wx.EVT_BUTTON, self.OnButtonClick,self.button)
self.Bind(wx.EVT_BUTTON, self.OnCClick,self.button6)
self.Bind(wx.EVT_BUTTON, self.OnGClick,self.button7)
#self.std=wx.TextCtrl(panel,pos=(0,31), size=(500,-1))
self.loc=wx.TextCtrl(panel,pos=(700,0), size=(300,-1))
self.status = wx.TextCtrl(panel,-1,'Choose file',pos=(800,22),size=(200,-1))
#redir=RedirectText(self.std)
#sys.stdout=redir
def OnButtonClick(self,event):
wild="HSi Files|*.lan*|All Files|*.*"
dlg=wx.FileDialog(self,message="Choose a File",wildcard=wild,style=wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
time.sleep(0.5)
self.loc.SetValue(dlg.GetPath())
dlg.Destroy()
self.Onview()
def Onview(self):
filepath=self.loc.GetValue()
img=image(filepath)
time.sleep(1)
view(img)
time.sleep(1)
self.status.SetValue('View Ready')
def OnHeadClick(self,event):
filepath=self.loc.GetValue()
img=image(filepath)
self.status.SetValue(img.shape)
def OnCubeClick(self,event):
time.sleep(0.2)
self.status.SetValue('Clustering')
filepath=self.loc.GetValue()
img= image(filepath).load()
(m, c) = cluster(img, 20)
view_indexed(m)
self.status.SetValue('Clustering View Ready')
def OnCClick(self,event):
self.status.SetValue('Cube view')
time.sleep(5)
filepath=self.loc.GetValue()
img= image(filepath).load()
hypercube(img, bands=[29, 19, 9])
def OnGClick(self,event):
self.status.SetValue('Gaussian procesing')
time.sleep(30)
filepath=self.loc.GetValue()
gt=image(filepath).read_band(0)
img=image(filepath)
classes = create_training_classes(img,gt)
gmlc = GaussianClassifier(classes)
clMap = gmlc.classify_image(img)
view_indexed(clMap)
self.status.SetLabel('Gaussian Ready')
def OnSaveClick(self,event):
self.status.SetValue('Save File')
wild="HSi Files|*.lan*|All Files|*.*"
dlg=wx.FileDialog(self,message="Save AS",wildcard=wild,style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
path=dlg.GetPath()
self.Save(path)
self.file=path
dlg.Destroy()
def OnClose(self, event):
dlg = wx.MessageDialog(self,
"Do you really want to close BBvw ?",
"Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
self.Destroy()
app = wx.App(redirect=False)
top = Frame("BBvw")
top.Show()
app.MainLoop()
Long running processes should always go into separate threads. Otherwise they will block the GUI’s mainloop. If displaying the image is taking up lots of memory, then I’m guessing you’re displaying high res photos at full resolution. Try creating a set of lower resolution photos for displaying instead (like thumbnails or something). Unless you need the full resolution displayed, don’t do it.