12

I noticed something very odd in trying a motion detector for Raspberry Pi:

Removing the camera logging from the script, makes it use almost 0 CPU:

#from gpiozero import MotionSensor
#import cv2
from datetime import datetime
from time import sleep
#camera = cv2.VideoCapture(0)
#pir = MotionSensor(4, queue_len=2, sample_rate=2, threshold=0.5)
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
PIR_PIN = 4
GPIO.setup(PIR_PIN, GPIO.IN)
while True:
    sleep(1)
    if GPIO.input(PIR_PIN):
        print( "detected!")
        filename = 'motionpics/' + datetime.now().strftime("%Y-%m-%d_%H.%M.%S.jpg")
        #ret, frame = camera.read()
        #cv2.imwrite(filename, frame)
        #camera.release()
        #pir.wait_for_no_motion()

However, uncommenting just one line - the import cv2, makes this script go to 300% CPU Usage!!

What is wrong with OpenCV and why can't I even start to use it to grab usb camera images without it using a bunch of cpu, and wearing down the battery?

NoBugs
  • 9,310
  • 13
  • 80
  • 146
  • 3
    I have been noticing the exact same issue on my Raspberry Pi3. I have tried installing OpenCV3.2-dev using the exact details listed on the Pyimagesearch blog, but just importing the library puts me at 75% CPU. Trying to grab a frame from my USB camera results in the program not reaponding. Using latest Raspbian (Pixel) and python 3.4. –  Mar 13 '17 at 03:43
  • Can you attach to your application with `gdb` (`gdb -p pid_of_it`) and check all thread states (`info threads`, `thread 1`, `backtrace`, `thread 2`, `backtrace` and so on). I think it can be from some kind of parallel threads, ready to work and polling on the workqueue (like active mode of OpenMP or TBB). Please, also post result of `env` command (there can be OMP or CV or TBB related variables here). (Cross post by Tyler: http://raspberrypi.stackexchange.com/questions/63021). What is your version of opencv (and root image), how cv lib was compiled and what is `ldd .../libopencv.so`? – osgx Mar 13 '17 at 23:44

2 Answers2

11

Hmmmm, if I am not mistaken opencv needs numpy right? Could you try the following:

$ sudo apt-get install libatlas3-base
$ sudo update-alternatives --config libblas.so.3

choose the libatlas option

$ sudo update-alternatives --config liblapack.so.3

choose the libatlas option

$ sudo aptitude purge libopenblas-{base,dev}

Source

Giannis Spiliopoulos
  • 2,628
  • 18
  • 27
  • I can not even understand the relationship between the question and this answer. But I know this answer is right. I admire you so much! Please give some explanations to make this answer more generic, thanks! – ToughMind Sep 12 '19 at 07:23
5

I can confirm that Giannis' answer is correct. I just performed the steps listed in his answer and am able to import cv2 in python 3.4 without the high cpu usage. So at least there is that. I am able to grab a frame and display an image. This works for my use case.

I did notice however that during the aforementioned steps, libtiff5, wolfram, and several other libraries were uninstalled.

If you need these libraries and applications (I do not have a full list at the moment) I would reccomend temporarily NOT performing

Sudo apt-get dist-upgrade

And

Sudo rpi-update

At this time, and remain at raspbian jessie. This is just out of my personal experience.

EDIT:

Also I would like to add that Giannis was right, this is seemingly a numpy issue, and can easily be tested by simply:

going on your Raspberry Pi3's desktop->Start Menu->Code->Python 3; type "import numpy" (without quotes).

You should see your cpu usage go through the roof. This is a way of telling that you are eligible to have this fix work.

  • Also I would like to add that Giannis was right, this is seemingly a numpy issue, and can easily be tested by simply: going on your Raspberry Pi3's desktop->Start Menu->Code->Python 3; type "import numpy" (without quotes). You should see your cpu usage go through the roof. This is a way of telling that you are eligible to have this fix work. –  Mar 19 '17 at 01:54
  • You can edit your own answer to include relevant information from your comment. – Regular Jo Mar 19 '17 at 02:55
  • Whoops. Good point. I had no rep anymore so it wasn't letting me do things like edit/comment in a few places. It is done. –  Mar 19 '17 at 04:42