简介人工智能的一个重要领域是计算机视觉。计算机视觉是计算机和软件系统识别和理解图像和场景的能力的科学。计算机视觉还包括图像识别、目标检测、图像生成、图像超分辨率等诸多方面。由于大量的实际用例,目标检测可能是计算机视觉中最深刻的方面。对象检测是指计算机和软件系统在图像/场景中定位对象并识别每个对象的能力。目标检测已广泛应用于人脸检测、车辆检测、行人计数、网络图像、安全系统和自动驾驶汽车等领域。在许多领域的实践中,也有许多方法可用于对象检测。与其他计算机技术一样,对象检测的广泛创造性和惊人用途肯定会来自计算机程序员和软件开发人员的努力。这次我将介绍一个名为ImageAI的项目,这是一个python库,它允许程序员和软件开发人员只需中间几行代码就可以轻松地将最先进的计算机视觉技术集成到他们现有的和新的应用程序中。ImageAI安装工作要使用ImageAI执行对象检测,您需要做的是:在您的计算机系统上安装Python安装ImageAI及其依赖项下载对象检测模型文件运行示例代码(仅10行)我们开始吧:来自Python语言官方网站下载安装Python3.通过pip安装:TensorFlow,OpenCV,Keras,ImageAIpip3installtensorflowpip3installopencv-pythonpip3installkeraspip3installimageai--upgrade3)通过本文链接下载用于物体检测的RetinaNet模型文件:https://towardsdatascience。com/object-detection-with-10-lines-of-code-d6cb4d86f606运行程序很好。我们现在已经安装了依赖项,可以编写我们的第一个对象检测代码。创建一个Python文件并为其命名(例如,FirstDetection.py),然后将以下代码写入其中。将要检测的RetinaNet模型文件图片复制到包含python文件的文件夹中。fromimageai.DetectionimportObjectDetectionimportosexecution_path=os.getcwd()detector=ObjectDetection()detector.setModelTypeAsRetinaNet()detector.setModelPath(os.path.join(execution_path,"resnet50_coco_best_v2.0.1.h5"))detector.loadModel()detections=检测器。(input_image=os.path.join(execution_path,"image.jpg"),output_image_path=os.path.join(execution_path,"imagenew.jpg"))foreachObjectdetections:print(eachObject["name"],":",eachObject["percentage_probability"])请注意,如果您遇到此错误:ValueError:Unabletoimportbackend:theanopythonmymodel.py那么您可以尝试:importosos.environ['KERAS_BACKEND']='tensorflow'fromimageai.DetectionimportObjectDetection并运行代码并等待在控制台打印的结果。结果打印到控制台后,转到FirstDetection.py所在的文件夹,您会发现保存了一张新图像。看看下面的两个图像样本和检查后保存的新图像。检测前:检测后:数据结果我们可以看到程序会打印输出一些各个物体的概率数据:person:55.8402955532074person:53.21805477142334person:69.25139427185059person:76.41745209693909bicycle:80.30363917350769person:83.58567953109741person:89.06581997871399truck:63.10953497886658person:69.82483863830566person:77.11606621742249bus:98.00949096679688truck:84.02870297431946car:71.98476791381836Itcanbeseenthattheprogramcandetectthefollowingobjectsinthepicture:people,bicycles,trucks,cars,buses.Youcandirectlyputthephotoyouwanttodetectintotheprogramandrunittoseetheeffect.PrincipleExplainedNowlet'sexplainhowthe10linesofcodework.fromimageai.DetectionimportObjectDetectionimportosexecution_path=os.getcwd()Intheabove3linesofcode,weimportedtheImageAIobjectdetectionclassinthefirstline,importedthepythonosclassinthesecondline,anddefinedavariabletosavethepythonfile,RetinaNetThepathtothefolderwherethemodelfilesandimagesarelocated.检测器=ObjectDetection()detector.setModelTypeAsRetinaNet()detector.setModelPath(os.path.join(execution_path,"resnet50_coco_best_v2.0.1.h5"))detector.loadModel()detections=detector.detectObjectsFromImage(input_image=os.path.join(execution_path,"image.jpg"),output_image_path=os.path.join(execution_path,"imagenew.jpg"))在上面的代码中,我们在第一行定义了目标检测类,在第二行,第三行设置模型路径为RetinaNet模型的路径,第四行将模型加载到目标检测类中,然后我们调用检测函数,解析输入图像的路径和第五行输出图像的路径。foreachObjectindetections:print(eachObject["name"],":",eachObject["percentage_probability"])在上面的代码中,我们在第一行遍历所有的detector.detectObjectsFromImage函数返回的结果,然后在第二行打印line打印出图像中检测到的每个对象的模型名称和百分比概率。
