I wanted to improve the performance of my program

1

Good afternoon,

I'm currently creating an interface for an application, but I'm having 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 turn on this by the main program, the interface gets stuck and not responsive.

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

This is the method that 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, programming in python soon.

    
asked by anonymous 14.02.2017 / 19:01

1 answer

1

The ideal is to put the function that takes time to calculate (even if "time" is 0.2s - is enough for the interface to seem unresponsive) on a separate thread.

In this way, whereas in the main thread, Qt "does things of it", responding to user events, etc ..., in another thread, the calculations are done in the background.

You may have heard that "multi-threading programming is complicated". Well, it may be rather complicated, but especially if you have (1) threads that move in parallel on the same data structures (even if they are on the disk); (2) implement the entire communication logic between threads. - for (2), Python is already quite alleviated, causing global variables, lists, and dictionaries to be thread safe - and Qt alleviates 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 the PyQT itself, and will only need to update the final result.

Lastly - Python has the thread mechanisms itself, 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 the " 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):

link

    
21.02.2017 / 13:07