Artificial Intelligence With Python 简明教程
AI with Python – Computer Vision
计算机视觉关注于使用计算机软件和硬件建模和复制人类视觉。在本章中,您将详细了解这一点。
Computer vision is concerned with modeling and replicating human vision using computer software and hardware. In this chapter, you will learn in detail about this.
Computer Vision
计算机视觉是一门学科,研究如何根据场景中结构的性质,从其二维图像中重建、解释并理解三维场景。
Computer vision is a discipline that studies how to reconstruct, interrupt and understand a 3d scene from its 2d images, in terms of the properties of the structure present in the scene.
Computer Vision Hierarchy
计算机视觉分为以下三个基本类别:
Computer vision is divided into three basic categories as following −
-
Low-level vision − It includes process image for feature extraction.
-
Intermediate-level vision − It includes object recognition and 3D scene interpretation
-
High-level vision − It includes conceptual description of a scene like activity, intention and behavior.
Computer Vision Vs Image Processing
图像处理研究图像到图像转换。图像处理的输入和输出都是图像。
Image processing studies image to image transformation. The input and output of image processing are both images.
计算机视觉是从它们的图像中构建物理对象的明确有意义的描述。计算机视觉的输出是对三维场景中结构的描述或解释。
Computer vision is the construction of explicit, meaningful descriptions of physical objects from their image. The output of computer vision is a description or an interpretation of structures in 3D scene.
Applications
计算机视觉在以下领域有应用:
Computer vision finds applications in the following fields −
Robotics
Robotics
-
Localization-determine robot location automatically
-
Navigation
-
Obstacles avoidance
-
Assembly (peg-in-hole, welding, painting)
-
Manipulation (e.g. PUMA robot manipulator)
-
Human Robot Interaction (HRI): Intelligent robotics to interact with and serve people
Medicine
Medicine
-
Classification and detection (e.g. lesion or cells classification and tumor detection)
-
2D/3D segmentation
-
3D human organ reconstruction (MRI or ultrasound)
-
Vision-guided robotics surgery
Security
Security
-
Biometrics (iris, finger print, face recognition)
-
Surveillance-detecting certain suspicious activities or behaviors
Transportation
Transportation
-
Autonomous vehicle
-
Safety, e.g., driver vigilance monitoring
Industrial Automation Application
Industrial Automation Application
-
Industrial inspection (defect detection)
-
Assembly
-
Barcode and package label reading
-
Object sorting
-
Document understanding (e.g. OCR)
Installing Useful Packages
对于使用 Python 的计算机视觉,可以使用名为 OpenCV (开源计算机视觉)的流行库。它是一个编程函数库,主要针对实时计算机视觉。它使用 C 编写,而它的主界面使用 C。可以使用以下命令安装此软件包 −
For Computer vision with Python, you can use a popular library called OpenCV (Open Source Computer Vision). It is a library of programming functions mainly aimed at the real-time computer vision. It is written in C and its primary interface is in C. You can install this package with the help of the following command −
pip install opencv_python-X.X-cp36-cp36m-winX.whl
此处 X 代表机器上安装的 Python 版本以及你拥有的 win32 或 64 位。
Here X represents the version of Python installed on your machine as well as the win32 or 64 bit you are having.
如果使用 anaconda 环境,则使用以下命令安装 OpenCV −
If you are using the anaconda environment, then use the following command to install OpenCV −
conda install -c conda-forge opencv
Reading, Writing and Displaying an Image
大多数 CV 应用程序需要获取图像作为输入,并生成图像作为输出。在本节中,你将学习如何使用 OpenCV 提供的函数读取和写入图像文件。
Most of the CV applications need to get the images as input and produce the images as output. In this section, you will learn how to read and write image file with the help of functions provided by OpenCV.
OpenCV functions for Reading, Showing, Writing an Image File
OpenCV 为此提供了以下函数 −
OpenCV provides the following functions for this purpose −
-
imread() function − This is the function for reading an image. OpenCV imread() supports various image formats like PNG, JPEG, JPG, TIFF, etc.
-
imshow() function − This is the function for showing an image in a window. The window automatically fits to the image size. OpenCV imshow() supports various image formats like PNG, JPEG, JPG, TIFF, etc.
-
imwrite() function − This is the function for writing an image. OpenCV imwrite() supports various image formats like PNG, JPEG, JPG, TIFF, etc.
Example
此示例展示了以一种格式读取图像的 Python 代码 − 以另一种格式在窗口中显示,并写入同一图像。请考虑以下所示步骤 −
This example shows the Python code for reading an image in one format − showing it in a window and writing the same image in other format. Consider the steps shown below −
按所示导入 OpenCV 软件包 −
Import the OpenCV package as shown −
import cv2
现在,要读取特定图像,请使用 imread() 函数 −
Now, for reading a particular image, use the imread() function −
image = cv2.imread('image_flower.jpg')
要显示图像,请使用 imshow() 函数。可以看到图像的窗口名称为 image_flower 。
For showing the image, use the imshow() function. The name of the window in which you can see the image would be image_flower.
cv2.imshow('image_flower',image)
cv2.destroyAllwindows()
现在,我们可以使用 imwrite() 函数将同一图像写入其他格式(例如 .png) −
Now, we can write the same image into the other format, say .png by using the imwrite() function −
cv2.imwrite('image_flower.png',image)
输出 True 表示图像已成功写入当前文件夹的 .png 文件中。
The output True means that the image has been successfully written as .png file also in the same folder.
True
注 - destroyallWindows() 函数仅销毁我们创建的所有窗口。
Note − The function destroyallWindows() simply destroys all the windows we created.
Color Space Conversion
在 OpenCV 中,图像不是使用传统的 RGB 颜色存储的,相反,它们以相反的顺序存储,即 BGR 顺序。因此,读取图像时的默认颜色代码是 BGR。s0 颜色转换函数用于将图像从一种颜色代码转换为其他颜色代码。
In OpenCV, the images are not stored by using the conventional RGB color, rather they are stored in the reverse order i.e. in the BGR order. Hence the default color code while reading an image is BGR. The cvtColor() color conversion function in for converting the image from one color code to other.
Example
考虑这个例子,将图像从 BGR 转换为灰度。
Consider this example to convert image from BGR to grayscale.
导入 s1 软件包,如下所示:
Import the OpenCV package as shown −
import cv2
现在,要读取特定图像,请使用 imread() 函数 −
Now, for reading a particular image, use the imread() function −
image = cv2.imread('image_flower.jpg')
现在,如果我们使用 s2 函数查看此图像,则我们可以看到此图像为 BGR。
Now, if we see this image using imshow() function, then we can see that this image is in BGR.
cv2.imshow('BGR_Penguins',image)
现在,使用 s3 函数将此图像转换为灰度。
Now, use cvtColor() function to convert this image to grayscale.
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray_penguins',image)
Edge Detection
人类在看到草图后,可以轻松识别许多类型的物体及其姿势。这就是为什么边缘在人类生活中以及在计算机视觉的应用中都起着重要作用。OpenCV 提供了一个非常简单且有用的函数,称为 Canny(),用于检测边缘。
Humans, after seeing a rough sketch, can easily recognize many object types and their poses. That is why edges play an important role in the life of humans as well as in the applications of computer vision. OpenCV provides very simple and useful function called *Canny()*for detecting the edges.
Example
以下示例说明了边缘的清晰识别。
The following example shows clear identification of the edges.
导入 OpenCV 软件包,如下所示:
Import OpenCV package as shown −
import cv2
import numpy as np
现在,要读取特定图像,请使用 s4 函数。
Now, for reading a particular image, use the imread() function.
image = cv2.imread('Penguins.jpg')
现在,使用 s5 函数来检测已读取图像的边缘。
Now, use the Canny () function for detecting the edges of the already read image.
cv2.imwrite(‘edges_Penguins.jpg’,cv2.Canny(image,200,300))
现在,要显示带有边缘的图像,请使用 imshow() 函数。
Now, for showing the image with edges, use the imshow() function.
cv2.imshow(‘edges’, cv2.imread(‘‘edges_Penguins.jpg’))
此 Python 程序将创建一个名为 s6 的图像,并进行边缘检测。
This Python program will create an image named edges_penguins.jpg with edge detection.
Face Detection
人脸检测是计算机视觉的一个令人着迷的应用,它使它更加真实和具有未来感。OpenCV 具有执行人脸检测的内置功能。我们将使用 s7 级联分类器进行人脸检测。
Face detection is one of the fascinating applications of computer vision which makes it more realistic as well as futuristic. OpenCV has a built-in facility to perform face detection. We are going to use the Haar cascade classifier for face detection.
Haar Cascade Data
我们需要数据来使用 Haar 级联分类器。您可以在我们的 OpenCV 软件包中找到此数据。安装 OpenCV 后,您可以看到名为 s8 的文件夹。不同的应用程序会有不同的 .xml 文件。现在,将它们全部复制以供不同的用途,然后将它们粘贴到当前项目下的新文件夹中。
We need data to use the Haar cascade classifier. You can find this data in our OpenCV package. After installing OpenCv, you can see the folder name haarcascades. There would be .xml files for different application. Now, copy all of them for different use and paste then in a new folder under the current project.
Example
以下是使用 Haar 级联检测下图中显示的阿米塔布·巴乍的 Python 代码:
The following is the Python code using Haar Cascade to detect the face of Amitabh Bachan shown in the following image −
导入 s1 软件包,如下所示:
Import the OpenCV package as shown −
import cv2
import numpy as np
现在,使用 s9 来检测人脸:
Now, use the HaarCascadeClassifier for detecting face −
face_detection=
cv2.CascadeClassifier('D:/ProgramData/cascadeclassifier/
haarcascade_frontalface_default.xml')
现在,要读取特定图像,请使用 s10 函数:
Now, for reading a particular image, use the imread() function −
img = cv2.imread('AB.jpg')
现在,将它转换为灰度,因为它会接受灰色图像 −
Now, convert it into grayscale because it would accept gray images −
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
现在,使用 face_detection.detectMultiScale 执行实际的人脸检测
Now, using face_detection.detectMultiScale, perform actual face detection
faces = face_detection.detectMultiScale(gray, 1.3, 5)
现在,在整张脸部周围绘制一个矩形 −
Now, draw a rectangle around the whole face −
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w, y+h),(255,0,0),3)
cv2.imwrite('Face_AB.jpg',img)
以此 Python 程序将创建一个名为 Face_AB.jpg 的图像,图像中包含所示的人脸检测结果
This Python program will create an image named Face_AB.jpg with face detection as shown
Eye Detection
眼部检测是计算机视觉的另一项了不起的应用,它使屏幕显示效果更真实并且更具未来感。OpenCV 具有执行眼部检测的内置功能。我们将使用 Haar cascade 分类器来进行眼部检测。
Eye detection is another fascinating application of computer vision which makes it more realistic as well as futuristic. OpenCV has a built-in facility to perform eye detection. We are going to use the Haar cascade classifier for eye detection.
Example
以下示例提供了使用 Haar 级联来检测下图中提供的大巴哈昌的脸部的 Python 代码 −
The following example gives the Python code using Haar Cascade to detect the face of Amitabh Bachan given in the following image −
导入 OpenCV 软件包,如下所示:
Import OpenCV package as shown −
import cv2
import numpy as np
现在,使用 s9 来检测人脸:
Now, use the HaarCascadeClassifier for detecting face −
eye_cascade = cv2.CascadeClassifier('D:/ProgramData/cascadeclassifier/haarcascade_eye.xml')
现在,要读取特定图像,请使用 imread() 函数
Now, for reading a particular image, use the imread() function
img = cv2.imread('AB_Eye.jpg')
现在,将它转换为灰度,因为它会接受灰色图像 −
Now, convert it into grayscale because it would accept grey images −
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
现在,借助 eye_cascade.detectMultiScale 执行实际的人脸检测
Now with the help of eye_cascade.detectMultiScale, perform actual face detection
eyes = eye_cascade.detectMultiScale(gray, 1.03, 5)
现在,在整张脸部周围绘制一个矩形 −
Now, draw a rectangle around the whole face −
for (ex,ey,ew,eh) in eyes:
img = cv2.rectangle(img,(ex,ey),(ex+ew, ey+eh),(0,255,0),2)
cv2.imwrite('Eye_AB.jpg',img)
以此 Python 程序将创建一个名为 Eye_AB.jpg 的图像,图像中包含所示的眼部检测结果
This Python program will create an image named Eye_AB.jpg with eye detection as shown −