I wanted to improve the performance of my program

Asked

Viewed 128 times

1

Good Afternoon,

I am currently creating an interface, for an application, but I have a problem, with the function I created.

def contornos(self):

    self.im = cv2.imread(self.imagem)
    im_copia = self.im.copy()

    imagem_cinza = cv2.cvtColor(im_copia, cv2.COLOR_BGR2GRAY) # Transforma a imagem em tons de cinza
    _,thresh = cv2.threshold(imagem_cinza,self.minThresh,255,cv2.THRESH_TOZERO_INV) # Limiarização

    res = cv2.bitwise_and(im_copia,im_copia,mask = thresh)
    res_cinza = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
    _,thresht = cv2.threshold(res_cinza,0,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)


    _, self.contorno, _= cv2.findContours(thresht, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 
    cv2.drawContours(im_copia,self.contorno,-1,(255,255,255),3)
    (self.contorno, _) = contours.sort_contours(self.contorno)    

    plt.imshow(cv2.cvtColor(im_copia, cv2.COLOR_BGR2RGB))
    plt.show()

When I call this from the main program, the interface is locked and not responsive.

self.threshold_slice.valueChanged.connect(self.segmenta_foto)

this is the method the widget activates:

def segmenta_foto(self):
    if self.diretorio_imagem.text() == '':
        self.msg = QtGui.QMessageBox()
        self.msg.setIcon(QtGui.QMessageBox.Critical)

        self.msg.setText("Erro: Não há imagem selecionada")
        self.msg.setInformativeText("Favor inserir uma imagem apertando o botão de OK")
        self.msg.setWindowTitle("Erro - 01")
        retval = self.msg.exec_()
    else:
        valor = self.threshold_slice.value()
        self.threshold_indicador.setText(str(valor))
        imagem = Tratamento_imagem(self.diretorio_imagem.text(),int(valor))
        imagem.contornos()

Is there any way to improve this code, so that it gets faster? sorry for any wrong term, I program in python a short time.

1 answer

1

Ideally put the function that takes time to calculate (even if "time" is 0.2s - is enough for the interface to appear non-responsive) into a separate thread.

In this way, while in the main thread, Qt "does its thing", responding to user events, etc..., in another thread, calculations are done in the background.

You may have heard that "multi-threading programming is complicated". Well, it might be rather complicated, but especially if you have (1) Threads that move in parallel in the same data structures (even if they are on disk); (2) implement all logical communication between threads. - for (2), Python already relieves enough, making global variables, lists and dictionaries "thread safe" - and Qt relieves even more. In fact, Qt has just implemented all the communication logic you would need. And (1) your function will perform operations on the image independently of Pyqt itself, and will only need to update the final result.

Finally - Python has its own thread mechanisms in the threading module - but if you use these, they will conflict with the main Qt loop. So if your application is in Qt, you should use "Qthreads"

I believe this tutorial is detailed enough for you to solve your problem there - (alias, it should be simple, since you already have ready and isolated the code that will run in the thread):

https://nikolak.com/pyqt-threading-tutorial/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.