zoukankan      html  css  js  c++  java
  • Object detection with deep learning and OpenCV


      这篇文章只是基于OpenCV使用SSD算法执行目标检测;不涉及到SSD的理论原理、不涉及训练过程;也就是说仅仅使用训练好的模型文件基于OpenCV做测试;包括图片和视频;

      只用作笔记,原教程地址:Object detection with deep learning and OpenCV


    Single Shot Detectors for Object Detection

      当提到基于深度学习的目标检测算法,大家都多多少少的听说过这三种算法:

     当然了,现在已经是19年了,上面三种算法也已经更新换代了;那之所以还列举出来,想要表达的是这三类算法是相当good,...(完了,装不下去了....)

    R-CNN系列检测算法,精确度高,速度慢;

    YOLO系列检测算法,速度快,精确度有些欠缺;

    SSD取了两者的优点吧。。。。

    Deep learning-based object detection with OpenCV

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # @Time    : 19-4-24 下午3:52
    # @Author  : chen
    
    """
    利用MobileNet SSD + OpenCV中的dnn执行目标检测
    
    python deep_learning_object_detection_cz.py --image images/face_1.jpg 
    --prototxt MobileNetSSD_deploy.prototxt.txt 
     --model MobileNetSSD_deploy.caffemodel
    """
    # 依赖包
    import numpy as np
    import argparse
    import cv2
    import time
    import pdb
    
    # 解析命令行参数
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True, help="path to input image")
    ap.add_argument("-p", "--prototxt", required=True, help="path to Caffe 'deploy' prototxt file")
    ap.add_argument("-m", "--model", required=True, help="path to Caffe pre-trained model")
    ap.add_argument("-c", "--confidence", type=float, default=0.2, help="minimum probability to filter weak detections")
    args = vars(ap.parse_args())
    
    # 初始化类标签,然后为每一个类别设置一个颜色值
    # 该颜色值是为了在图像中画出矩形框的时候使用
    CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
               "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
               "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
               "sofa", "train", "tvmonitor"]
    COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
    
    # 加载训练好的Caffe模型
    # OpenCV的dnn方法中,可以加载由Caffe,TensorFLow,Darknet,Torch训练得到的模型文件的方法
    print("[INFO] loading model...")
    net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
    
    # 加载测试图片,并转换成blob(OpenCV需要这样做)
    image = cv2.imread(args["image"])
    (h, w) = image.shape[:2]
    # cv2.dnn.blobFromImage返回一个四维的blob
    # 可以对image执行缩放,剪切,交换RB通道,减均值操作
    blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5)
    
    # 输入到网络中,执行Inference
    print("[INFO] computing object detections...")
    net.setInput(blob)
    start = time.time()
    detections = net.forward()
    end = time.time()
    print("[INFO] SSD took {:6} seconds.".format(end - start))
    # pdb.set_trace()
    
    for i in range(detections.shape[2]):
        # 类别概率
        confidence = detections[0, 0, i, 2]
    
        # 过滤掉confidence小于人为设定的阈值的detection
        if confidence > args["confidence"]:
            idx = int(detections[0, 0, i, 1])  # 类别索引
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")  # SSD的输出直接就是框的左上角和右下角的点的坐标位置
    
            # 在图片展示检测的object
            label = "{}: {:.2f}%".format(CLASSES[idx], confidence*100)
            print("[INFO] {}".format(label))
            cv2.rectangle(image, (startX, startY), (endX, endY), COLORS[idx], 2)
            y = startY -15 if startY - 15 > 15 else startY + 15
            cv2.putText(image, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
    
    # 显示
    cv2.imshow("Object Detection", image)
    cv2.waitKey(0)
    cv2.imwrite("lab_2_ssd.jpg", image)
    

     这样有没有用?????用处不大

      还是需要看论文的。。。。。

  • 相关阅读:
    索引优化策略
    mysql列类型选择
    redis安装
    redis相关面试题
    aop动态代理源码分析
    JVM运行时数据区
    redis应用场景
    JPA相关知识
    技术栈
    linux上安装mysql5.6
  • 原文地址:https://www.cnblogs.com/chenzhen0530/p/10763381.html
Copyright © 2011-2022 走看看