imageai库里面提供了目标识别,其实也可以说是目标检测,和现在很多的收集一样就是物体识别。他可以帮你识别出各种各样生活中遇见的事物。比如猫、狗、车、马、人、电脑、收集等等。

  感觉imageai有点差就是没有返回检测目标的坐标出来,所以感觉很low,而且和计算消耗很大,耗时很大,与opencv做实时检测效果很差。不推荐使用。

  安装imageai方法见:https://github.com/OlafenwaMoses/ImageAI

  resnet50_coco_best_v2.1.0.h5 模型下载地址:https://github.com/fizyr/keras-retinanet/releases/

下面提供两种调用方式。

  第一文件流调用:

# coding:utf-8
# imageai下载地址:https://github.com/OlafenwaMoses/ImageAI
# resnet50_coco_best_v2.1.0.h5 模型下载地址:https://github.com/fizyr/keras-retinanet/releases/
from imageai.Detection import ObjectDetection # 导入了 ImageAI 目标检测类
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' execution_path = os.path.join(os.getcwd(),'imgData/') # 定义了一个变量用来保存我们的 python 文件
print(execution_path)
detector = ObjectDetection() # 定义了目标检测类
detector.setModelTypeAsRetinaNet() # 模型的类型设置为 RetinaNet
detector.setModelPath(os.path.join(execution_path, "resnet50_coco_best_v2.1.0.h5")) # 将模型路径设置为 RetinaNet 模型的路径
detector.loadModel() # 模型加载到的目标检测类
# 调用目标检测函数,解析输入的和输出的图像路径。
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path, "ji.jpg"),
output_image_path=os.path.join(execution_path, "imagenew1.jpg"),input_type='file') for eachObject in detections:
print(eachObject["name"] + " : " + eachObject["percentage_probability"]) # 打印出所检测到的每个目标的名称及其概率值。 print(detections)

  

  第二种numpy数据类型调用:

# coding:utf-8
# imageai下载地址:https://github.com/OlafenwaMoses/ImageAI
# resnet50_coco_best_v2.1.0.h5 模型下载地址:https://github.com/fizyr/keras-retinanet/releases/
from imageai.Detection import ObjectDetection # 导入了 ImageAI 目标检测类
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt def targetDetection(imgArray,model_path):
"""
:param imgArray: 图片数据,类型为ndarray
:param model_path: retinanet模型路径
:return:
"""
path = os.path.abspath(model_path)
detector = ObjectDetection() # 定义了目标检测类
detector.setModelTypeAsRetinaNet() # 模型的类型设置为 RetinaNet
detector.setModelPath(path) # 将模型路径设置为 RetinaNet 模型的路径
detector.loadModel() # 模型加载到的目标检测类
# 调用目标检测函数,解析输入的和输出的图像路径。
detections = detector.detectObjectsFromImage(input_image=imgArray,
input_type='array',output_type='array')
return detections data = plt.imread('./imgData/avenue.jpg')
model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')
imgInfo = targetDetection(data,model_path)
plt.imshow(imgInfo[0])
plt.show()

  

下面内容作为扩展,有兴趣的朋友可以试试,但是很不理想。

# coding:utf-8
# imageai下载地址:https://github.com/OlafenwaMoses/ImageAI
# resnet50_coco_best_v2.1.0.h5 模型下载地址:https://github.com/fizyr/keras-retinanet/releases/
from imageai.Detection import ObjectDetection # 导入了 ImageAI 目标检测类
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt def targetDetection(imgArray,model_path):
"""
:param imgArray: 图片数据,类型为ndarray
:param model_path: retinanet模型路径
:return:
"""
path = os.path.abspath(model_path)
detector = ObjectDetection() # 定义了目标检测类
detector.setModelTypeAsRetinaNet() # 模型的类型设置为 RetinaNet
detector.setModelPath(path) # 将模型路径设置为 RetinaNet 模型的路径
detector.loadModel() # 模型加载到的目标检测类
# 调用目标检测函数,解析输入的和输出的图像路径。
detections = detector.detectObjectsFromImage(input_image=imgArray,
input_type='array',output_type='array')
return detections # data = plt.imread('./imgData/avenue.jpg')
# model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')
# imgInfo = targetDetection(data,model_path)
# plt.imshow(imgInfo[0])
# plt.show() if __name__=='__main__':
# 获取摄像头0表示第一个摄像头
model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')
cap = cv2.VideoCapture(0)
while (True): # 逐帧显示
ret, img = cap.read() # 强调img是ndarray类型的。
imgData=targetDetection(img,model_path)
cv2.imshow('image',imgData[0])
if cv2.waitKey(1) & 0xFF == ord(' '):
break
cap.release() # 释放摄像头
cv2.destroyAllWindows() # 释放窗口资源 打开本地摄像头进行实时检测

  

最新文章

  1. WPF中通过代码设置控件的坐标
  2. (转)ant 使用指南
  3. hdu 2034人见人爱A-B
  4. is_user_logged_in()
  5. Jquery元素追加和删除
  6. nginx配置(解释)
  7. asp.net sql无限极分类实例程序
  8. 关于js的replace替换
  9. poj2912 Rochambeau
  10. D - DZY Loves Hash CodeForces - 447A
  11. Android中的WeakReference 弱引用
  12. 注解 - Excel 校验工具
  13. shell下获取系统时间
  14. Redis管道
  15. [APP] Android 开发笔记 004-Android常用基本控件使用说明
  16. 1.phpcms的安装和卸载文件
  17. ssh & sftp & MacOS
  18. jQuery网格插件 ParamQuery
  19. FFmpeg再学习 -- 硬件加速编解码
  20. 基于 CodeIgniter 的各类开源项目大全

热门文章

  1. c++ list_iterator demo
  2. 《数据结构与算法分析:C语言描述》读书笔记
  3. 调度器&负载均衡调度算法整理
  4. const char and static const char
  5. [leetcode-635-Design Log Storage System]
  6. CodeForces Round #521 (Div.3) E. Thematic Contests
  7. Apache服务器的Options 的 Indexes FollowSymLinks详解
  8. Delphi GetCurrentDir 获取当前文件夹
  9. [BZOJ4920][Lydsy六月月赛]薄饼切割
  10. 洛谷 P2480 [SDOI2010]古代猪文 解题报告