带Python的AI –计算机视觉

带Python的AI –计算机视觉 (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.

计算机视觉是一门研究如何根据场景中存在的结构的属性从其2d图像重建,中断和理解3d场景的学科。

计算机视觉层次结构 (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

    中级视觉 -包括对象识别和3D场景解释

  • 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.

计算机视觉是根据物理对象的图像对它们进行清晰,有意义的描述的构造。 计算机视觉的输出是对3D场景中的结构的描述或解释。

应用领域 (Applications)

Computer vision finds applications in the following fields −

计算机视觉在以下领域找到了应用-

Robotics

机器人技术

  • Localization-determine robot location automatically

    定位自动确定机器人位置

  • Navigation

    导航

  • Obstacles avoidance

    避障

  • Assembly (peg-in-hole, welding, painting)

    组装(Kong内钉,焊接,喷漆)

  • Manipulation (e.g. PUMA robot manipulator)

    操纵(例如PUMA机器人操纵器)

  • Human Robot Interaction (HRI): Intelligent robotics to interact with and serve people

    人机交互(HRI):与人互动并为人服务的智能机器人

Medicine

药物

  • Classification and detection (e.g. lesion or cells classification and tumor detection)

    分类和检测(例如病变或细胞分类和肿瘤检测)

  • 2D/3D segmentation

    2D / 3D分割

  • 3D human organ reconstruction (MRI or ultrasound)

    3D人体器官重建(MRI或超声)

  • Vision-guided robotics surgery

    视觉引导机器人手术

Security

安全

  • Biometrics (iris, finger print, face recognition)

    生物识别(虹膜,指纹,面部识别)
  • Surveillance-detecting certain suspicious activities or behaviors

    监视检测某些可疑的活动或行为

Transportation

运输

  • Autonomous vehicle

    自动驾驶汽车
  • Safety, e.g., driver vigilance monitoring

    安全性,例如驾驶员警惕性监控

Industrial Automation Application

工业自动化应用

  • Industrial inspection (defect detection)

    工业检查(缺陷检测)
  • Assembly

    部件
  • Barcode and package label reading

    条形码和包装标签读取
  • Object sorting

    对象分类
  • Document understanding (e.g. OCR)

    文件理解(例如OCR)

安装有用的软件包 (Installing Useful Packages)

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 −

对于使用Python的计算机视觉,您可以使用一个流行的库,称为OpenCV (开源计算机视觉)。 它是主要针对实时计算机视觉的编程功能库。 它是用C ++编写的,其主要接口是C ++。 您可以在以下命令的帮助下安装此软件包-


pip install opencv_python-X.X-cp36-cp36m-winX.whl

Here X represents the version of Python installed on your machine as well as the win32 or 64 bit you are having.

X代表您计算机上安装的Python版本以及您所拥有的Win32或64位。

If you are using the anaconda environment, then use the following command to install OpenCV −

如果您正在使用anaconda环境,则使用以下命令安装OpenCV-


conda install -c conda-forge opencv

读取,写入和显示图像 (Reading, Writing and Displaying an Image)

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.

大多数CV应用程序需要获取图像作为输入并生成图像作为输出。 在本节中,您将学习如何在OpenCV提供的功能的帮助下读写图像文件。

OpenCV函数用于读取,显示,写入图像文件 (OpenCV functions for Reading, Showing, Writing an Image File)

OpenCV provides the following functions for this purpose −

OpenCV为此提供了以下功能-

  • imread() function − This is the function for reading an image. OpenCV imread() supports various image formats like PNG, JPEG, JPG, TIFF, etc.

    imread()函数 -这是读取图像的函数。 OpenCV imread()支持各种图像格式,例如PNG,JPEG,JPG,TIFF等。

  • 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.

    imshow()函数 -这是用于在窗口中显示图像的函数。 窗口自动适合图像尺寸。 OpenCV imshow()支持各种图像格式,例如PNG,JPEG,JPG,TIFF等。

  • imwrite() function − This is the function for writing an image. OpenCV imwrite() supports various image formats like PNG, JPEG, JPG, TIFF, etc.

    imwrite()函数 -这是用于写入图像的函数。 OpenCV imwrite()支持各种图像格式,例如PNG,JPEG,JPG,TIFF等。

(Example)

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 −

此示例显示了以一种格式读取图像的Python代码-在窗口中显示图像,并以另一种格式写入同一图像。 考虑下面显示的步骤-

Import the OpenCV package as shown −

导入OpenCV软件包,如图所示-


import cv2

Now, for reading a particular image, use the imread() function −

现在,要读取特定图像,请使用imread()函数-


image = cv2.imread('image_flower.jpg')

For showing the image, use the imshow() function. The name of the window in which you can see the image would be image_flower.

要显示图像,请使用imshow()函数。 您可以在其中看到图像的窗口的名称为image_flower


cv2.imshow('image_flower',image)
cv2.destroyAllwindows()

image flower

Now, we can write the same image into the other format, say .png by using the imwrite() function −

现在,我们可以使用imwrite()函数将同一图像写入其他格式,例如.png-


cv2.imwrite('image_flower.png',image)

The output True means that the image has been successfully written as .png file also in the same folder.

输出True表示图像已成功也以.png文件格式写入同一文件夹中。


True

Note − The function destroyallWindows() simply destroys all the windows we created.

注意-函数destroyallWindows()只会破坏我们创建的所有窗口。

色彩空间转换 (Color Space Conversion)

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.

在OpenCV中,不使用常规RGB颜色存储图像,而是以相反的顺序(即BGR顺序)存储图像。 因此,读取图像时的默认颜色代码为BGR。 cvtColor()颜色转换函数用于将图像从一种颜色代码转换为另一种颜色代码。

(Example)

Consider this example to convert image from BGR to grayscale.

考虑以下示例,将图像从BGR转换为灰度。

Import the OpenCV package as shown −

导入OpenCV软件包,如图所示-


import cv2

Now, for reading a particular image, use the imread() function −

现在,要读取特定图像,请使用imread()函数-


image = cv2.imread('image_flower.jpg')

Now, if we see this image using imshow() function, then we can see that this image is in BGR.

现在,如果我们使用imshow()函数看到此图像,则可以看到该图像在BGR中。


cv2.imshow('BGR_Penguins',image)

penguine

Now, use cvtColor() function to convert this image to grayscale.

现在,使用cvtColor()函数将此图像转换为灰度。


image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray_penguins',image)

grey penguine

边缘检测 (Edge Detection)

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.

看到粗略的草图后,人类可以轻松识别许多物体类型及其姿势。 这就是为什么边缘在人类的生活以及计算机视觉的应用中起着重要作用的原因。 OpenCV提供了非常简单和有用的函数Canny()来检测边缘。

(Example)

The following example shows clear identification of the edges.

下面的示例清楚地标识了边缘。

Import OpenCV package as shown −

导入OpenCV软件包,如图所示-


import cv2
import numpy as np

Now, for reading a particular image, use the imread() function.

现在,要读取特定图像,请使用imread()函数。


image = cv2.imread('Penguins.jpg')

Now, use the Canny () function for detecting the edges of the already read image.

现在,使用Canny()函数检测已读取图像的边缘。


cv2.imwrite(‘edges_Penguins.jpg’,cv2.Canny(image,200,300))

Now, for showing the image with edges, use the imshow() function.

现在,要显示带有边缘的图像,请使用imshow()函数。


cv2.imshow(‘edges’, cv2.imread(‘‘edges_Penguins.jpg’))

This Python program will create an image named edges_penguins.jpg with edge detection.

该Python程序将创建带有边缘检测功能的名为edge_penguins.jpg的图像。

edges penguins

人脸检测 (Face Detection)

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.

人脸检测是计算机视觉的引人入胜的应用之一,这使其更加逼真且具有未来感。 OpenCV具有执行面部检测的内置功能。 我们将使用Haar级联分类器进行人脸检测。

Haar级联数据 (Haar Cascade Data)

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.

我们需要数据才能使用Haar级联分类器。 您可以在我们的OpenCV软件包中找到此数据。 安装OpenCv后,您可以看到文件夹名称haarcascades 。 将存在用于不同应用程序的.xml文件。 现在,复制所有这些文件以用于不同用途,然后粘贴到当前项目下的新文件夹中。

Example

The following is the Python code using Haar Cascade to detect the face of Amitabh Bachan shown in the following image −

以下是使用Haar Cascade来检测下图所示的Amitabh Bachan头像的Python代码-

ab face

Import the OpenCV package as shown −

导入OpenCV软件包,如图所示-


import cv2
import numpy as np

Now, use the HaarCascadeClassifier for detecting face −

现在,使用HaarCascadeClassifier来检测人脸-


face_detection=
cv2.CascadeClassifier('D:/ProgramData/cascadeclassifier/
haarcascade_frontalface_default.xml')

Now, for reading a particular image, use the imread() function −

现在,要读取特定图像,请使用imread()函数-


img = cv2.imread('AB.jpg')

Now, convert it into grayscale because it would accept gray images −

现在,将其转换为灰度,因为它将接受灰度图像-


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

Now, using face_detection.detectMultiScale, perform actual face detection

现在,使用face_detection.detectMultiScale ,执行实际的面部检测


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)

This Python program will create an image named Face_AB.jpg with face detection as shown

该Python程序将创建一个名为Face_AB.jpg的图像, 进行面部检测,如图所示

Face AB

眼睛检测 (Eye Detection)

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.

眼睛检测是计算机视觉的另一个引人入胜的应用,它使计算机视觉更加逼真和未来。 OpenCV具有执行眼睛检测的内置功能。 我们将使用Haar级联分类器进行眼睛检测。

(Example)

The following example gives the Python code using Haar Cascade to detect the face of Amitabh Bachan given in the following image −

以下示例给出了使用Haar Cascade来检测下图中给出的Amitabh Bachan头像的Python代码-

Haar AB Face

Import OpenCV package as shown −

导入OpenCV软件包,如图所示-


import cv2
import numpy as np

Now, use the HaarCascadeClassifier for detecting face −

现在,使用HaarCascadeClassifier来检测人脸-


eye_cascade = cv2.CascadeClassifier('D:/ProgramData/cascadeclassifier/haarcascade_eye.xml')

Now, for reading a particular image, use the imread() function

现在,要读取特定图像,请使用imread()函数


img = cv2.imread('AB_Eye.jpg')

Now, convert it into grayscale because it would accept grey images −

现在,将其转换为灰度,因为它将接受灰度图像-


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

Now with the help of eye_cascade.detectMultiScale, perform actual face detection

现在借助eye_cascade.detectMultiScale ,执行实际的人脸检测


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)

This Python program will create an image named Eye_AB.jpg with eye detection as shown −

该Python程序将创建带有眼睛检测功能的名为Eye_AB.jpg的图像,如下所示-

Eye AB

翻译自: https://www.tutorialspoint.com/artificial_intelligence_with_python/artificial_intelligence_with_python_computer_vision.htm

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值