python multiprocessing runs slower at second time without terminating whole program

Viewed 63

I have a python program with opencv gui that uses a multiprocessing in threading in class instance.

After running below script, when I type q it will quit the sub loop and it terminates or join all existing thread, process and close queue in modules.

And then if I type r then it will start sub loop again, but this time, the worker in multiprocessing runs twice slower. I have no idea why it is happening.

code is like below.

    module_started = False
    height = 480
    width = 800
    img_base = np.zeros((height,width,3), np.uint8)
    cv2.namedWindow("VIN", cv2.WINDOW_NORMAL)
    
    while True:
        
        if not module_started:
            cam1 = PersonDetector(0)
            cam1.start()
            outputs = []
            module_started = True
    
        while True:
     
                if cam1.new_detection:
                    outputs = cam1.get_output()
                
                if outputs:
                    
                    for xys in outputs:
                        (startX, startY, endX, endY) = xys
                        cv2.rectangle(cam1.frame, (startX, startY), (endX, endY), (255,5,150),2)
                
    
                # show the output image
                cv2.imshow("VIN", cam1.frame)
                key = cv2.waitKey(40) & 0xFF
                # if the `q` key was pressed, break from the loop
                if key == ord("q"):
                    break
                
            cam1.stop()
            del cam1
        
        # show the output frame
        
        cv2.imshow("VIN", img_base)
        key = cv2.waitKey(40) & 0xFF
      
        if key == ord("e"):          
            break
        if key == ord("r"):             # go back to start sub loop
            module_started = False
    
    cv2.destroyAllWindows()

it is on raspberry pi 4. it has cpu 4 core. first time, htop shows CPU% over 300% but second time, htop shows CPU% just 99 or 100%...

why it happens?

1 Answers

I have tested by myself for a whole day. It was not because of multiprocessing nor threading.

It was because of cv2.imshow(), I removed imshow then everything keep the same speed but when repeating imshow like above code, it shows program down, I still dont know why though...

Related