美文网首页
ImageAI:对象检测

ImageAI:对象检测

作者: 程序员Ameng | 来源:发表于2020-07-22 16:17 被阅读0次

ImageAI 提供了非常方便和强大的方法来对图像执行对象检测并从图像中提取每个对象。目前仅支持当前最先进的 RetinaNet 算法进行对象检测,后续版本会加入对其他算法的支持。在开始对象检测任务前,您必须通过以下链接下载 RetinaNet 模型文件:

下载 RetinaNet 模型文件后,应将模型文件复制到.py文件所在的项目文件夹中。然后创建一个python文件并为其命名; 例如 FirstObjectDetection.py 。然后将下面的代码写入python文件中:

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = 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 , "image2.jpg"), output_image_path=os.path.join(execution_path , "image2new.jpg"))

for eachObject in detections:
    print(eachObject["name"] + " : " + eachObject["percentage_probability"] )
    print("--------------------------------")

运行代码
python FirstObjectDetection.py

示例额结果:

输入图片


输出图片


控制台输出
car : 98.6770749092102

相关文章

网友评论

      本文标题:ImageAI:对象检测

      本文链接:https://www.haomeiwen.com/subject/otltlktx.html