打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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/

下面提供两种调用方式。

  第一文件流调用:

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

  第二种numpy数据类型调用:

 1 # coding:utf-8 2 #  imageai下载地址:https://github.com/OlafenwaMoses/ImageAI 3 #  resnet50_coco_best_v2.1.0.h5 模型下载地址:https://github.com/fizyr/keras-retinanet/releases/ 4 from imageai.Detection import ObjectDetection  # 导入了 ImageAI 目标检测类 5 import cv2 6 import os 7 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 8 import matplotlib.pyplot as plt 9 10 def targetDetection(imgArray,model_path):11     """12     :param imgArray: 图片数据,类型为ndarray13     :param model_path: retinanet模型路径14     :return:15     """16     path = os.path.abspath(model_path)17     detector = ObjectDetection()  # 定义了目标检测类18     detector.setModelTypeAsRetinaNet()  # 模型的类型设置为 RetinaNet19     detector.setModelPath(path)  # 将模型路径设置为 RetinaNet 模型的路径20     detector.loadModel()  # 模型加载到的目标检测类21     # 调用目标检测函数,解析输入的和输出的图像路径。22     detections = detector.detectObjectsFromImage(input_image=imgArray,23                                                  input_type='array',output_type='array')24     return detections25 26 data = plt.imread('./imgData/avenue.jpg')27 model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')28 imgInfo = targetDetection(data,model_path)29 plt.imshow(imgInfo[0])30 plt.show()

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

 1 # coding:utf-8 2 #  imageai下载地址:https://github.com/OlafenwaMoses/ImageAI 3 #  resnet50_coco_best_v2.1.0.h5 模型下载地址:https://github.com/fizyr/keras-retinanet/releases/ 4 from imageai.Detection import ObjectDetection  # 导入了 ImageAI 目标检测类 5 import cv2 6 import os 7 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 8 import matplotlib.pyplot as plt 9 10 def targetDetection(imgArray,model_path):11     """12     :param imgArray: 图片数据,类型为ndarray13     :param model_path: retinanet模型路径14     :return:15     """16     path = os.path.abspath(model_path)17     detector = ObjectDetection()  # 定义了目标检测类18     detector.setModelTypeAsRetinaNet()  # 模型的类型设置为 RetinaNet19     detector.setModelPath(path)  # 将模型路径设置为 RetinaNet 模型的路径20     detector.loadModel()  # 模型加载到的目标检测类21     # 调用目标检测函数,解析输入的和输出的图像路径。22     detections = detector.detectObjectsFromImage(input_image=imgArray,23                                                  input_type='array',output_type='array')24     return detections25 26 # data = plt.imread('./imgData/avenue.jpg')27 # model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')28 # imgInfo = targetDetection(data,model_path)29 # plt.imshow(imgInfo[0])30 # plt.show()31 32 33 if __name__=='__main__':34     # 获取摄像头0表示第一个摄像头35     model_path = ('./imgData/resnet50_coco_best_v2.0.1.h5')36     cap = cv2.VideoCapture(0)37     while (True):  # 逐帧显示38         ret, img = cap.read() # 强调img是ndarray类型的。39         imgData=targetDetection(img,model_path)40         cv2.imshow('image',imgData[0])41         if cv2.waitKey(1) & 0xFF == ord(' '):42             break43     cap.release()  # 释放摄像头44     cv2.destroyAllWindows()  # 释放窗口资源

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
目标检测很难?Python10行代码就能写出来,而且还非常好用
TFOD:基于TFOD API的官方模型案例对图片进行目标检测
Python 20行代码检测人脸是否佩戴口罩
厉害了!用不到20行的Python代码构建一个对象检测模型
谷歌更新TensorFlow目标检测API
使用Tensorflow OpenCV构建会玩石头剪刀布的AI
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服