简体   繁体   English

为什么只导入OpenCV导致大量CPU使用?

[英]Why does just importing OpenCV cause massive CPU usage?

I noticed something very odd in trying a motion detector for Raspberry Pi: 在为Raspberry Pi尝试运动检测器时,我注意到了一些非常奇怪的事情:

Removing the camera logging from the script, makes it use almost 0 CPU: 从脚本中删除摄像机记录,使其几乎使用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!! 但是,取消注释只有一行 - 导入cv2 ,使这个脚本达到300%的CPU使用率!!

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? OpenCV有什么问题,为什么我不能开始使用它来获取usb相机图像而不使用一堆cpu,并且耗尽电池?

Hmmmm, if I am not mistaken opencv needs numpy right? 嗯,如果我没弄错,opencv需要numpy吧? Could you try the following: 你能尝试以下方法吗?

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

choose the libatlas option 选择libatlas选项

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

choose the libatlas option 选择libatlas选项

$ sudo aptitude purge libopenblas-{base,dev}

Source 资源

I can confirm that Giannis' answer is correct. 我可以确认Giannis的回答是正确的。 I just performed the steps listed in his answer and am able to import cv2 in python 3.4 without the high cpu usage. 我刚刚执行了他的答案中列出的步骤,并且能够在没有高CPU使用率的情况下在python 3.4中导入cv2。 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. 我注意到,在上述步骤中,libtiff5,wolfram和其他几个库都被卸载了。

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 Sudo apt-get dist-upgrade

And

Sudo rpi-update Sudo rpi-update

At this time, and remain at raspbian jessie. 这时,还留在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: 另外我想补充一点,Giannis是对的,这似乎是一个numpy问题,并且很容易通过以下方式进行测试:

going on your Raspberry Pi3's desktop->Start Menu->Code->Python 3; 继续你的Raspberry Pi3桌面 - >开始菜单 - >代码 - > Python 3; type "import numpy" (without quotes). 键入“import numpy”(不带引号)。

You should see your cpu usage go through the roof. 您应该看到您的CPU使用率通过屋顶。 This is a way of telling that you are eligible to have this fix work. 这是一种告诉您有资格使此修复工作的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pydev大量的CPU使用 - pydev massive cpu usage Python:与MongoDB的100个连接导致CPU使用率很高,为什么? - Python: 100 connections to MongoDB cause a high cpu usage, Why? 为什么以不同的方式生成数组会导致代码的不同部分大量加速? - Why does generating an array in a different way cause massive speed-up of the different part of the code? 为什么在Sublime Text中导入Pytorch会导致ModuleNotFoundError? - Why does importing Pytorch in Sublime Text cause a ModuleNotFoundError? 为什么在我的 IDLE 上导入 Tensorflow 会导致错误? - Why does importing Tensorflow on my IDLE cause an error? 为什么导入包含 print() 的文件会导致打印 output? - Why does importing a file that contains a print() cause the output to be printed? 为什么 ThreadPool 会导致超过一个核心的 CPU 使用率? - Why would ThreadPool cause more than one core worth of CPU usage? 为什么引用(python)pyglet.image中的类会导致Windows上的CPU负载过重? - Why does referring to a class in (python) pyglet.image cause heavy cpu load on Windows? 为什么使用py-leveldb的WriteBatch会导致内存泄漏? - Why does this usage of py-leveldb's WriteBatch cause a memory leak? 运行10秒后,Python OpenCV的CPU使用率极高 - Python OpenCV extremely high CPU usage after 10 second runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM