1

I am doing a robotic project on raspberry pi in which i need frame from a webcam, attached to raspberry pi, continuously so i am extracting frames from videocapture() in a separate thread than main program and saving the frames in a queue. The problem is that, when i run the program , initially CPU usage is normal i.e. 30-40 max in each core of pi but after a while CPU usage on one core reaches to 100 % while other 3 cores are less than 20% which slows down the program.

I've searched a lot but couldn't find any related solution. I am not able to understand what the problem is, is it thread (python GIL problem) or there is something else. Any suggestions would be helpful.

These are the function in camera module which are getting the frame and saving in queue

self.framerate_ms = 1.0/float(20)
def start(self):
    if self.t:
        return

    f = None
    if self.camera_type == USB:
        f = self.update_usb

    self.t = Thread(target=f, args=())
    #self.t.daemon = True
    self.t.start()



def update_usb(self):
    while True:
        if self.stopped:
            return

        (grabbed, frame) = self.camera.read()

        if not grabbed:
            self.stop()
            return

        if self.Q.full():
            self.Q.get()

        self.Q.put(frame)            
        self.ready = True

        #sleep(self.framerate_ms)

ps sorry for bad English i am not native English speaker.

whoosis
  • 454
  • 7
  • 25
  • We probably can't help because we can see none of the code. We would need to see something representative that recreates the issue. – roganjosh Mar 13 '18 at 23:33
  • Hard to tell without a magic crystal ball that would allow us to catch a glimpse of the code. Best guess is that one of your threads enters a loop with no `time.sleep` (or any other call that would release the *CPU*) inside. – CristiFati Mar 13 '18 at 23:37
  • Thank for your response, i have updated the question – whoosis Mar 14 '18 at 00:03
  • @CristiFati i've also tried adding sleep but that didnt help either – whoosis Mar 14 '18 at 00:04
  • That doesn't help, provided code is not enough. Try to recreate the problem on your desktop/laptop, try to minimize it till it fits into the [\[SO\]: How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) specs, and then, if you still have the problem (although I doubt), update your question. – CristiFati Mar 14 '18 at 00:16

0 Answers0