islamahmed
Posts: 4
Joined: Wed Jan 27, 2016 7:04 am

raspberry pi 100% cpu usage

Wed Jan 27, 2016 7:14 am

I'm running a simple python 3 code that uses webcam to detect faces and picamera to capture pictures. whenever i run this code the CPU usage spikes to 100%. i tried Haar cascade as well as LBP cascade but still the same.

Code: Select all

import numpy as np
import cv2
import time
import pygame
import os
import datetime
import sys
from picamera.array import PiRGBArray
from picamera import PiCamera
from subprocess import call


face_cascade = cv2.CascadeClassifier('/home/pi/Desktop/MyCode/haarcascade_frontalface_default.xml')
if face_cascade.empty(): raise Exception("your face_cascade is empty.
are you sure, the path is correct ?")

eye_cascade = cv2.CascadeClassifier('/home/pi/Desktop/MyCode/haarcascade_eye.xml')
if eye_cascade.empty(): raise Exception("your eye_cascade is empty.
are you sure, the path is correct ?")

video = cv2.VideoCapture(0)
pygame.mixer.init(0)



while True:

 video = cv2.VideoCapture(0)
 pygame.mixer.init(0)



 while(video.isOpened()):
    ret, frame = video.read()
    if frame.any():

       gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

       faces = face_cascade.detectMultiScale(gray, 1.3, 5)

            # stop display message if face detected


       for (x,y,w,h) in faces:

  

        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        cv2.imwrite('test' + time.strftime("%Y-%m-%d-%H:%M:%S")+'.png', frame)



        for (ex,ey,ew,eh) in eyes:
           waited = time.time()
           print('Waited {} seconds'.format(waited))
           cv2.imwrite('test' +
time.strftime("%Y-%m-%d-%H:%M:%S")+'.png', frame)
           video.release()
           cv2.destroyAllWindows()
           break

        pygame.mixer.music.load("/home/pi/Downloads/mirror mirror.mp3")
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy() == True:
           continue

        video = cv2.VideoCapture(0)


os.listdir('home/pi/Dropbox-Uploader') if file.endswith('.jpg')
  
        camera = PiCamera()
        camera.resolution = (640, 480)
        camera.framerate = 22
        camera.sharpness = 0
      
        camera.brightness = 50
        camera.saturation = -40
      
        camera.exposure_mode= 'night'

        camera.Hue= 1780
        camera.capture('frame.data' , 'jpeg')
        rawCapture = PiRGBArray(camera, size=(640, 480))
        date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")

    
        time.sleep(0.1)
        #my_file= (time.strftime("%Y-%m-%d-%H:%M:%S")+'.jpg')

        camera.capture(date_string+".jpg")
        camera.close()
    
        photofile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh
upload /home/pi/Dropbox-Uploader/"+date_string+".jpg
"+date_string+".jpg"
        call ([photofile], shell=True)
       


        pygame.mixer.music.load("/home/pi/Downloads/thank you.mp3")
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy() == True:
         continue]

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6107
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: raspberry pi 100% cpu usage

Wed Jan 27, 2016 1:00 pm

Yes, that's exactly what I'd expect. They're very CPU intensive algorithms, not accelerated by the GPU in any way.

dimonic
Posts: 41
Joined: Fri Jun 08, 2012 9:08 pm

Re: raspberry pi 100% cpu usage

Wed Jan 27, 2016 3:01 pm

D'yah think?

Although YOU have hardly written any code, the CPU is doing a vast amount of code in OpenCV - recognizing faces is very hard.

islamahmed
Posts: 4
Joined: Wed Jan 27, 2016 7:04 am

Re: raspberry pi 100% cpu usage

Thu Jan 28, 2016 1:13 am

thank u guys for ur reply.... any tips on how i can achieve face detection with minimal CPU usage?

ghans
Posts: 7880
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: raspberry pi 100% cpu usage

Thu Jan 28, 2016 8:18 am

Unless the system becomes unresponsive or hot ( over 85° C) i see no problem with 100 % CPU usage.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

jahboater
Posts: 4929
Joined: Wed Feb 04, 2015 6:38 pm

Re: raspberry pi 100% cpu usage

Thu Jan 28, 2016 9:10 am

What kind of Pi is this running on?

The Pi2 has four CPU's, so if your program takes up 100% of one CPU its no big deal, the system will continue to be very fast with the remaining three - and each CPU is considerably quicker. The Pi2 also has the NEON SIMD instruction set which can speed up image processing a lot. Twice as much memory on the Pi2 can help sometimes, but that 100% CPU utilisation implies you are not running out of memory.

If you have any other kind of Pi, it will be sharing a single CPU. I agree with this:-
Unless the system becomes unresponsive or hot ( over 85° C) i see no problem with 100 % CPU usage.
It will not be damaged by overheating nor will its lifetime be reduced (still in decades).

You can of course overclock any Pi for a few percent more CPU power.

No matter how powerful your computer, the application will always take 100% of CPU - just for less time.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

islamahmed
Posts: 4
Joined: Wed Jan 27, 2016 7:04 am

Re: raspberry pi 100% cpu usage

Fri Jan 29, 2016 2:30 am

thank u jahboater for the reply.... my issue with the 100% usage that it makes the face detection slower too... u have to stand in fornt of d camera for around 10 seconds till it detects the face... whereas when i run the face detection code alone its kinda faster than that

ghans
Posts: 7880
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: raspberry pi 100% cpu usage

Fri Jan 29, 2016 7:21 am

I don't think 100% CPU usage is the cause for slow face detection, but it's effect..

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6107
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: raspberry pi 100% cpu usage

Fri Jan 29, 2016 12:19 pm

islamahmed wrote:thank u jahboater for the reply.... my issue with the 100% usage that it makes the face detection slower too... u have to stand in fornt of d camera for around 10 seconds till it detects the face... whereas when i run the face detection code alone its kinda faster than that
Alone as opposed to with what? If you can run the face detection code and have it work faster, but you're adding something else and that's slowing it down.... what's that something else?

I have a C++ example of pretty fast face detection, but need to clean it up. Not sure if it would help the python folk though.

fmobrj75
Posts: 2
Joined: Thu Mar 16, 2017 3:39 pm

Re: raspberry pi 100% cpu usage

Thu Mar 16, 2017 3:47 pm

I am facing the same issue as islamahmed.

The problem is not with the resource usage when doing operations using OpenCV. Just by importing the library in the Python Script, the CPU usage jumps to 75%, without any other line of code.

My configuration:
Raspberry Pi3 running the latest Raspbian
Python2.7
OpenCV 3.2.0

This issue have two annoying effects: 1- leave less processing room for the known expensive OpenCV computation; 2 - raises the temperature of the computer.

I tried everything I could:
- tried differente versions of OpenCV (2.4.9, 2.4.10, 3.1.0, 3.2.0)
- tried to use ptthreads instead of TBB in CMAKE options, using -D WITH...

But nothing seems to work. There is another catch in compiling and binding OpenCV with Python that cast CPU usage to 75% just by importing OpenCV

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6107
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: raspberry pi 100% cpu usage

Thu Mar 16, 2017 6:13 pm

Likely to be the same issue:
viewtopic.php?f=32&t=171012#p1128948

fmobrj75
Posts: 2
Joined: Thu Mar 16, 2017 3:39 pm

Re: raspberry pi 100% cpu usage

Thu Mar 16, 2017 9:50 pm

ShiftPlusOne wrote:Likely to be the same issue:
viewtopic.php?f=32&t=171012#p1128948
Thank you very much, ShiftPlusOne! Problem solved perfectly!

I hope this solution helps the others too.

Return to “Beginners”