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的内建对象,稍微归类了一下,多 ...
随机推荐
- 手把手教你玩转CSS3 3D技术
手把手教你玩转 CSS3 3D 技术 要玩转css3的3d,就必须了解几个词汇,便是透视(perspective).旋转(rotate)和移动(translate).透视即是以现实的视角来看屏幕上 ...
- ios下 active 演示激活
document.body.addEventListener('touchstart', function () { });
- GraphSAGE 代码解析(三) - aggregators.py
原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(一) - unsupervised_train.py GraphSAGE 代码解析(二) - layers.py ...
- 第一周 Introduction
欢迎 欢迎来到这门关于机器学习的免费网络课程,机器学习是近年来最激动人心的技术之一,在这门课中,你不仅可以了解机器学习的原理,更有机会进行实践操作,并且亲自运用所学的算法. 每天你都可能在不知不觉中使 ...
- edgeboxes proposal 和dpm 连接
经过多天阅读文献和代码,我对edgeboxes 和 dpm的有了一定了解. 现在决定把它们简单地连接起来 也就是edgeboxes proposal 推荐窗口 然后 dpm去判断 但按照我理解的DPM ...
- 一个简单的NetCore项目:2 - 登录
1-UI,登陆界面布局 PS:使用的是metronic 框架,没有用过的可以自行百度. 1.1 metronic 放在wwwroot文件夹下面 1.2 metronic 中的 open sans 使 ...
- Alpha冲刺(7/10
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 学会了POSTMAN的使用,对后端已经完成的接口进行了收发消息正确性的验证 推 ...
- exec族
在之前我们已经知道用fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个程序.当进程调用一种exec函数时,该进程的用户空间代码和 ...
- Ext.net中TreePanel动态生成
这个问题可以参考官网例子:http://examples2.ext.net/#/TreePanel/Basic/Built_in_CodeBehind/ 贴一段本人程序中用到的动态生成核心代码: Ex ...
- Flink源码解读之状态管理
一.从何说起 State要能发挥作用,就需要持久化到可靠存储中,flink中持久化的动作就是checkpointing,那么从TM中执行的Task的基类StreamTask的checkpoint逻辑说 ...