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()  # 释放窗口资源

打开本地摄像头进行实时检测

  

原文地址:https://www.cnblogs.com/wuzaipei/p/9696601.html