我的Raspberry Pi 2昨天刚邮到,这家伙看上去很小巧可爱。
这小家伙有4核900MHZ的处理器,1G内存。要知道,Raspberry Pi 2 可比我中学电脑实验室里大多数电脑快多了。
话说,自从Raspberry Pi 2发布以来,我收到了很多请求,要求我能写一个在它上面安装OpenCV和Python的详细说明。
因此如果你想在Raspberry Pi启动运行OpenCV和Python,就往下面看!
在博文的剩余部分,我将提供在Raspberry Pi 2 和Raspberry Pi B+上的详细安装说明。
我也会说明每一步的安装耗时。其中一些步骤需要较多的处理时间。举个例子,在Raspberry Pi 2 编译OpenCV花费大概2.8小时,在 Raspberry Pi B+上则是9.5小时。所以请你据此安排你的安装吧。
最后,提醒一下我们会在PyImageSearch Gurus 计算机视觉课程中用到Raspberry Pi。我们的项目将会包括家庭监控应用,例如说房间里面的运动检测和人员跟踪。
下面是一个运动检测和跟踪的简单例子,当时我正打着电话在公寓里面来回走动。
在 Raspberry Pi 2/B+上安装OpenCV和Python
这里假定你已经有了Raspberry Pi 2/B+,且已经安装好。如果没有的话,我建议你买一个,它们既便宜也好玩。
个人来说,我更愿意多花一点钱从Canakit买。他们的物流很快也比较可靠,加之他们的 ready-to-go服务也很好。
好吧,我们开始OpenCV和Python的安装
步骤0:
我们假定你已经打开Raspberry Pi 2/B+。打开终端,我们先更新和升级已安装的软件包,然后更新Raspberry Pi固件。
$ sudo apt-get update $ sudo apt-get upgrade $ sudo rpi-update
步骤1:
安装所需的安装工具和包:
$ sudo apt-get install build-essential cmake pkg-config
build-essential 和 pkg-config可能都已经安装。以防万一,我们还是将它们键入apt-get命令中。
耗时:
Raspberry Pi B+:小于2分钟
Raspberry Pi 2:小于40秒
步骤2:
安装必要的图像I/O包,这样你才可以读入JPEG,PNG,TIFF等这些格式的图像。
$ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
耗时:
Raspberry Pi B+: 小于5钟
Raspberry Pi 2: 小于30秒
步骤3:
安装GTK开发库,这个库用来构建GUI。同时OpenCV中的highgui库也需要它来在屏幕上显示图像。
$ sudo apt-get install libgtk2.0-dev
耗时:
Raspberry Pi B+: 小于10分钟
Raspberry Pi 2:小于3分钟
步骤4:
安装必要的视频I/O包,OpenCV需要它们来读入视频文件。
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
耗时:
Raspberry Pi B+: 小于5分钟
Raspberry Pi 2: 小于30秒
步骤5:
安装OpenCV优化操作时所需库。
$ sudo apt-get install libatlas-base-dev gfortran
耗时:
Raspberry Pi B+:小于2分钟
Raspberry Pi 2:小于30秒
步骤6:
安装pip:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
耗时:
Raspberry Pi B+:小于2分钟
Raspberry Pi 2:小于30秒
步骤7:
安装virtualenv和virtualenvwrapper
$ sudo pip install virtualenv virtualenvwrapper
然后,更新~/.profile文件如下:
export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
重新载入.profile文件:
$ source ~/.profile
创建你的计算机视觉虚拟环境
$ mkvirtualenv cv
耗时:
Raspberry Pi B+: 小于2分钟
Raspberry Pi 2: 小于2分钟
步骤8:
现在我们安装Python 2.7开发工具:
$ sudo apt-get install python2.7-dev
注意:我们将使用Python2.7。因为OpenCV 2.4.X不支持Python 3,也不太清楚OpenCV 3.0的python接口什么时候才能完善。所以我建议现在还是使用OpenCV2.4.X的好。
我们也需要安装NumPy,因为OpenCV的Python接口通过Numpy的多维数组来表示图像。
$ pip install numpy
耗时:
Raspberry Pi B+: 小于45分钟
Raspberry Pi 2: 小于15分钟
步骤9:
下载和解压OpenCV:
$ wget -O opencv-2.4.10.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.10/opencv-2.4.10.zip/download $ unzip opencv-2.4.10.zip $ cd opencv-2.4.10
安装:
$ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON ..
耗时:
Raspberry Pi B+: 小于3分钟
Raspberry Pi 2: 小于1.5分钟
编译OpenCV:
$ make
重要:确定你在cv虚拟环境中,OpenCV也要根据该环境中的Python和NumPy中进行编译。否则的话,OpenCV就会和系统中的Python和NumPy编译在一起,会产生各种问题。
耗时:
Raspberry Pi B+: 小于 9.5 小时
Raspberry Pi 2: 小于2.8小时
最后,我们安装OpenCV:
$ sudo make install $ sudo ldconfig
耗时:
Raspberry Pi B+: 小于 3分钟
Raspberry Pi 2: 小于1分钟
步骤10:
进行到这一步的时候,OpenCV已经应该已经安装到/usr/local/lib/python2.7/site-packages中了。
但是为了在cv虚拟环境中使用OpenCV,我们先需要符合链接到我们的site-packages目录:
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ $ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so $ ln -s /usr/local/lib/python2.7/site-packages/cv.py cv.py
步骤11:
最后,我们测试一下OpenCV和Python的安装:
$ workon cv $ python >>> import cv2 >>> cv2.__version__ '2.4.10'
OpenCV和Python已经成功安装到你的Raspberry Pi了。
这是在我的Raspberry Pi上的运行例子,我ssh登录到 Raspberry Pi,然后读取和显示一幅图像。
总结
这篇博文中,我详细介绍了如何在Raspberry Pi 2 /Raspberry Pi B+上安装Python和OpenCV。也提供了他们的安装耗时,请酌情考虑你的安装。
随着Raspberry Pi的升级,安装指令可能有所变化。如果你遇到一些极端情况或安装指令的变化,请随时联系我。当然我也不能保证每封邮件都能回复,所以最好是将如何在 Raspberry Pi上安装OpenCV和Python的方法总结成一个列表。
不久以后,我会讲如何在Raspberry Pi操作其附加的camera。
在那之前,看看PyImageSearch Gurus计算机视觉课程。我们将在其中一些工程中用到Raspberry Pi,就比如说构建一个监控应用来进行房间内部的运动检测和人员跟踪。

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)