• 利用maskrcnn来实现目标检测与追踪


    首先下载源代码仓库,链接地址如下:

    maskrcnn

    能够实现的效果如图所示:

    该存储库包括:

    • 基于FPN和ResNet101构建的Mask R-CNN的源代码。
    • MS COCO 的训练代码
    • MS COCO 的预训练砝码
    • Jupyter 笔记本,用于可视化每一步的检测管道
    • 用于多 GPU 训练的并行模型类
    • 对 MS COCO 指标 (AP) 的评估
    • 在自己的数据集上进行训练的示例

    下载代码仓库,进行解压后的目录如下:

    可以使用下面:

    pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

    也可以使用

    python setup.py install

    来安装相关的依赖包,安装完成后,还需要下载模型文件,

    下载链接地址如下:

    mask_rcnn_balloon.h5

    测试代码如下所示:

    1. import os
    2. import sys
    3. import random
    4. import math
    5. import numpy as np
    6. import skimage.io
    7. import matplotlib
    8. import matplotlib.pyplot as plt
    9. # Root directory of the project
    10. ROOT_DIR = os.path.abspath("../")
    11. # Import Mask RCNN
    12. sys.path.append(ROOT_DIR) # To find local version of the library
    13. from mrcnn import utils
    14. import mrcnn.model as modellib
    15. from mrcnn import visualize
    16. # Import COCO config
    17. sys.path.append(os.path.join(ROOT_DIR, "samples/coco/")) # To find local version
    18. import coco
    19. %matplotlib inline
    20. # Directory to save logs and trained model
    21. MODEL_DIR = os.path.join(ROOT_DIR, "logs")
    22. # Local path to trained weights file
    23. COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
    24. # Download COCO trained weights from Releases if needed
    25. if not os.path.exists(COCO_MODEL_PATH):
    26. utils.download_trained_weights(COCO_MODEL_PATH)
    27. # Directory of images to run detection on
    28. IMAGE_DIR = os.path.join(ROOT_DIR, "images")
    29. class InferenceConfig(coco.CocoConfig):
    30. # Set batch size to 1 since we'll be running inference on
    31. # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    32. GPU_COUNT = 1
    33. IMAGES_PER_GPU = 1
    34. config = InferenceConfig()
    35. config.display()
    36. # Create model object in inference mode.
    37. model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
    38. # Load weights trained on MS-COCO
    39. model.load_weights(COCO_MODEL_PATH, by_name=True)
    40. # COCO Class names
    41. # Index of the class in the list is its ID. For example, to get ID of
    42. # the teddy bear class, use: class_names.index('teddy bear')
    43. class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
    44. 'bus', 'train', 'truck', 'boat', 'traffic light',
    45. 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
    46. 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
    47. 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
    48. 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
    49. 'kite', 'baseball bat', 'baseball glove', 'skateboard',
    50. 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
    51. 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
    52. 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
    53. 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
    54. 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
    55. 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
    56. 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
    57. 'teddy bear', 'hair drier', 'toothbrush']
    58. # Load a random image from the images folder
    59. file_names = next(os.walk(IMAGE_DIR))[2]
    60. image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))
    61. # Run detection
    62. results = model.detect([image], verbose=1)
    63. # Visualize results
    64. r = results[0]
    65. visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
    66. class_names, r['scores'])

  • 相关阅读:
    echarts案例之仪表盘如何单独设置指针颜色?
    大数据扫黄,是真的吗?
    OCR文字识别方法综述
    【Java技术专题】「原理分析系列」Lambda表达式实现原理分析
    【AD9361 数字接口CMOS &LVDS&SPI】B 并行数据之CMOS
    小红书种草推广步骤是怎样的,小红书种草效果好吗?
    Redis 数据迁移篇之move、dump、migrate、redis-rdb-tools和redis-dump工具使用手册
    Servlet学习(七):Cookie
    基于增强蛇优化算法求解单目标优化问题附matlab代码
    Git 名词简单学习
  • 原文地址:https://blog.csdn.net/Helloorld_1/article/details/133382323