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() # 模型的类型设置为 RetinaNet 12 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: 图片数据,类型为ndarray 13 :param model_path: retinanet模型路径 14 :return: 15 """ 16 path = os.path.abspath(model_path) 17 detector = ObjectDetection() # 定义了目标检测类 18 detector.setModelTypeAsRetinaNet() # 模型的类型设置为 RetinaNet 19 detector.setModelPath(path) # 将模型路径设置为 RetinaNet 模型的路径 20 detector.loadModel() # 模型加载到的目标检测类 21 # 调用目标检测函数,解析输入的和输出的图像路径。 22 detections = detector.detectObjectsFromImage(input_image=imgArray, 23 input_type='array',output_type='array') 24 return detections 25 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: 图片数据,类型为ndarray 13 :param model_path: retinanet模型路径 14 :return: 15 """ 16 path = os.path.abspath(model_path) 17 detector = ObjectDetection() # 定义了目标检测类 18 detector.setModelTypeAsRetinaNet() # 模型的类型设置为 RetinaNet 19 detector.setModelPath(path) # 将模型路径设置为 RetinaNet 模型的路径 20 detector.loadModel() # 模型加载到的目标检测类 21 # 调用目标检测函数,解析输入的和输出的图像路径。 22 detections = detector.detectObjectsFromImage(input_image=imgArray, 23 input_type='array',output_type='array') 24 return detections 25 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 break 43 cap.release() # 释放摄像头 44 cv2.destroyAllWindows() # 释放窗口资源