python imageai 对象检测、对象识别
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() # 释放窗口资源 打开本地摄像头进行实时检测
python imageai 对象检测、对象识别的更多相关文章
- 计算机视觉中的对象检测,Python用几段代码就能实现
目前计算机视觉(CV)与自然语言处理(NLP)及语音识别并列为人工智能三大热点方向,而计算机视觉中的对象检测(objectdetection)应用非常广泛,比如自动驾驶.视频监控.工业质检.医疗诊断等 ...
- 人脸检测及识别python实现系列(5)——利用keras库训练人脸识别模型
人脸检测及识别python实现系列(5)——利用keras库训练人脸识别模型 经过前面稍显罗嗦的准备工作,现在,我们终于可以尝试训练我们自己的卷积神经网络模型了.CNN擅长图像处理,keras库的te ...
- [object_detect]使用MobileNetSSD进行对象检测
使用MobileNetSSD进行对象检测 1.单帧图片识别 object_detection.py # 导入必要的包 import numpy as np import argparse import ...
- 斯坦福新深度学习系统 NoScope:视频对象检测快1000倍
以作备份,来源http://jiasuhui.com/archives/178954 本文由“新智元”(微信ID:AI_era)编译,来源:dawn.cs.stanford.edu,编译:刘小芹 斯坦 ...
- Python笔记day20-面向对象
目录 面向对象 1 装饰器 1.1 装饰器是什么? 1.2 装饰器 2 面向对象 (Object Oriented) 简称OO 2.1 面向对象相关术语 2.2 类和对象 2.3 类和对象的实现和书写 ...
- 《python解释器源码剖析》第4章--python中的list对象
4.0 序 python中的list对象,底层对应的则是PyListObject.如果你熟悉C++,那么会很容易和C++中的list联系起来.但实际上,这个C++中的list大相径庭,反而和STL中的 ...
- python基础--面向对象基础(类与对象、对象之间的交互和组合、面向对象的命名空间、面向对象的三大特性等)
python基础--面向对象 (1)面向过程VS面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. ...
- python基础之面对对象
Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触 ...
- Python内建的对象列表
Python内建的对象列表 刚写Python肯定会遇到这样的情况,想写些什么,但又不知从何写起... 在我看来问题在于我们不知道有什么东东可以拿来玩,这里列出Python的内建对象,稍微归类了一下,多 ...
随机推荐
- (2)分布式下的爬虫Scrapy应该如何做-关于对Scrapy的反思和核心对象的介绍
本篇主要介绍对于一个爬虫框架的思考和,核心部件的介绍,以及常规的思考方法: 一,猜想 我们说的爬虫,一般至少要包含几个基本要素: 1.请求发送对象(sender,对于request的封装,防止被封) ...
- 【个人训练】(POJ3279)Fliptile
最近在刷kuangbin神犇的各种套题....感觉自己好弱啊.....还是要多多训练,跟上大神的脚步.最近的这十几题都比较水,记下来这一条我比较印象深刻.也比较难的题目吧(之后应该不会再有水题写了,珍 ...
- weblogic中配置自定义filter和servlet
情景:最近公司产品要接入其它厂商的单点服务器,本来我是在Tomcat上进行测试,使用的是spring boot 的注解方式@webFilter和@webServlet注解写过滤器和servlet类,启 ...
- Ruby中数组的&操作
最近在忙一个项目,好久没有写日志了,项目终于接近尾声,可以适当放松一下,所以记一下在这个项目中发现的有趣事情: 数组的 与 操作 一直以为两个数组A和B相与,谁前谁后都一样,不过这次在项目中突然想试一 ...
- devstack环境搭建
1. devstack部署 参考Quick Start,推荐使用ubuntu16.04进行安装 1.1 配置ubuntu国内源 修改/etc/apt/sources.list内容为 deb http: ...
- MySQL训练营03
[任务四] #任务时间# 请于4月6日22:00前完成,在[打卡表格]处打卡.逾期尚未打卡的会被清退. 4.1 MySQL 实战 #学习内容# 数据导入导出 将之前创建的任意一张MySQL表导出,且是 ...
- ORM选型对比
ORM框架选型 ORM框架选型 jian A YEAR AGO (2017-04-10) orm, database 选型标准:实现O/R mapping,基于promise,支持原生SQL语句,支持 ...
- Week3 Teamework from Z.XML-团队分工及贡献分分配办法
引言:团队项目即将开展,本文将就团队分工,以及分数分配办法进行阐述 一.团队分工 本周我们团队进行了初步的分工,结果如下: PM: 李孟 Dev:毛宇 薛亚杰 肖俊鹏 罗凡 Test:周敏轩 马辰 李 ...
- 简明Python3教程 1.介绍
Python是少有的几种既强大又简单的编程语言.你将惊喜地发现通过使用Python即可轻松专注于解决问题而非和你所用的语言格式与结构. 下面是Python的官方介绍: Python is an eas ...
- 学习MVC中出现的一个BUG
BUG描述:No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.S ...