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的内建对象,稍微归类了一下,多 ...
随机推荐
- oracle12c 新建表空间
第1步:创建临时表空间 create temporary tablespace jeeplus_temp tempfile 'D:\app\Administrator\virtual\product\ ...
- Linux 文件属性及修改权限
输入 ll 或 ls -l 命令显示当前目录中文件的属性及文件所属的用户和组 root@user:/home/www# ll test total 880 drwxr-xr-x 2 root root ...
- (干货分享)mac python+appium环境搭建
因为mac本自带python2.x(不建议卸载,因为本本本身有很多依赖与此),所以装python3的过程极其坎坷,勉强装好后也总是各种报错.这次装appium环境,直接把原来的python3卸了,用h ...
- Lambda表达式在Kotlin中怎样工作的:setOnClickListener的转换(KAD 18)
作者:Antonio Leiva 时间:Mar 28, 2017 原文链接:https://antonioleiva.com/lambdas-kotlin-android/ 虽然,我在其它文章讲过一点 ...
- python学习笔记-list的用法
1.list的定义 list = [] list = [1,2,'a','b'](list中的元素不一定是一个类型) 2.list的操作 1)list.append(value) 2)list.ins ...
- lintcode-119-编辑距离
119-编辑距离 给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 插入一个字符 删除一个字符 替换一个字符 样例 给出 work1=&q ...
- vue里的this
vue中methods对象里的函数, this指向的都是当前实例或者组件.
- vue2.0中改变了数组值不能实时反映到页面
页面中点击事件checkContent,改变row数组中的row[99]的值,如果注释更改,那么页面是不能实时获取的,如图更改,则可以 具体原理:http://blog.csdn.net/websof ...
- [剑指Offer] 27.字符串的排列
[思路]从第一位开始,判断每一位字符的所有可能性,依此递归. class Solution { public: void PermutationHelp(vector<string> &a ...
- hdu 1787 GCD Again (欧拉函数)
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...