ImageAI (二) 使用Python快速简单实现物体检测 Object Detection

上一篇已经讲解了ImageAI实现图片预测的方法,现在再来讲解一下ImageAI的第二个功能物体检测。
ImageAI提供了非常方便和强大的方法来对图像执行对象检测并从图像中提取每个对象。 ImageAI使用的模型是RetinaNet,并提供了已经训练完成的模型文件。
同样,仅需几行代码就能完成物体检测的过程。
ImageAI github地址
准备工作以及ImageAI的安装可以详见上一篇 ImageAI (一)
使用版本 ImageAI-2.1.0


Object Detection
训练好的RetinaNet模型:RetinaNet

detection.py

from imageai.Detection import ObjectDetection
import os
import time
#计时
start = time.time()


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

#结束计时
end = time.time()

for eachObject in detections:
    print(eachObject["name"] ," : " ,eachObject["percentage_probability"] , " : ", eachObject["box_points"] )  ##预测物体名:预测概率:物体两点坐标(左上,右下)
    print("--------------------------------")

print ("\ncost time:",end-start)

下面是github提供的图片
这里写图片描述

我跑出来的结果
这里写图片描述

person  :  76.33113265037537  :  [463 139 512 221]
--------------------------------
person  :  83.83048176765442  :  [600 131 639 213]
--------------------------------
person  :  96.08134031295776  :  [534 102 579 225]
--------------------------------
person  :  96.66982889175415  :  [  8 103  63 248]
--------------------------------
motorcycle  :  71.07154726982117  :  [273 180 346 306]
--------------------------------
dog  :  94.58073377609253  :  [398 314 444 433]
--------------------------------
car  :  55.41401505470276  :  [215 140 388 299]
--------------------------------
person  :  86.19718551635742  :  [154 145 255 393]
--------------------------------

cost time: 13.959604263305664

我自己随便找的一张图
这里写图片描述
结果
这里写图片描述

person  :  61.145514249801636  :  [175 580 202 650]
--------------------------------
person  :  63.822001218795776  :  [210 591 237 680]
--------------------------------
person  :  93.19865703582764  :  [314 592 345 688]
--------------------------------
person  :  71.38553857803345  :  [ 54 585 103 682]
--------------------------------
person  :  80.48739433288574  :  [272 594 309 692]
--------------------------------
motorcycle  :  71.90784811973572  :  [ 36 626  98 704]
--------------------------------
handbag  :  55.64073324203491  :  [818 654 876 764]
--------------------------------
person  :  92.45884418487549  :  [650 575 774 793]
--------------------------------
person  :  83.37823152542114  :  [787 597 880 796]
--------------------------------
person  :  96.3289201259613  :  [1096  586 1204  790]
--------------------------------

cost time: 15.170496702194214

完成!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值