参考:https://www.youtube.com/watch?v=lLM8oAsi32g

import cv2
    import numpy as np
     
     
    def random_colors(N):
        np.random.seed(1)
        colors=[tuple(255*np.random.rand(3)) for _ in range(N)]
        return colors
     
    def apply_mask(image, mask, color, alpha=0.5):
        """Apply the given mask to the image.
        """
        for n, c in enumerate(color):
            image[:, :, n] = np.where(
                mask == 1,
                image[:, :, n] *(1 - alpha) + alpha * c,
                image[:, :, n]
            )
        return image
     
    def display_instances(image,boxes,masks,ids,names,scores):
        n_instances=boxes.shape[0]
        if not n_instances:
            print('No instances to display')
        else:
            assert boxes.shape[0] == masks.shape[-1] == ids.shape[0]
        
        colors=random_colors(n_instances)
        height, width = image.shape[:2]
        
        for i,color in enumerate(colors):
            if not np.any(boxes[i]):
                continue
            
            y1,x1,y2,x2=boxes[i]
            mask=masks[:,:,i]
            image=apply_mask(image,mask,color)
            image=cv2.rectangle(image,(x1,y1),(x2,y2),color,2)
            
            label=names[ids[i]]
            score=scores[i] if scores is not None else None
            
            caption='{}{:.2f}'.format(label,score) if score else label
            image=cv2.putText(
                image,caption,(x1,y1),cv2.FONT_HERSHEY_COMPLEX,0.7,color,2
            )
            
        return image
     
    if __name__=='__main__':
        import os
        import sys
        import random
        import math
        import skimage.io
        import time
        import utils
        #import model as modellib
        
        
        ROOT_DIR = os.path.abspath("../")
        sys.path.append(ROOT_DIR)
        from mrcnn import utils
        import mrcnn.model as modellib
     
     
        sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
        import coco
        
     
        MODEL_DIR = os.path.join(ROOT_DIR, "logs")
        COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
        if not os.path.exists(COCO_MODEL_PATH):
            print('cannot find coco_model')
            
        class InferenceConfig(coco.CocoConfig):
            GPU_COUNT = 1
            IMAGES_PER_GPU = 1
     
        config = InferenceConfig()
        config.display()
        
        model = modellib.MaskRCNN(
            mode="inference", model_dir=MODEL_DIR, config=config
        )
     
        # Load weights trained on MS-COCO
        model.load_weights(COCO_MODEL_PATH, by_name=True)
        class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
                   'bus', 'train', 'truck', 'boat', 'traffic light',
                   'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
                   'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
                   'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
                   'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
                   'kite', 'baseball bat', 'baseball glove', 'skateboard',
                   'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
                   'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
                   'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
                   'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
                   'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
                   'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
                   'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
                   'teddy bear', 'hair drier', 'toothbrush']
        
        
        capture=cv2.VideoCapture(0)
        capture.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
        capture.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
        
        while True:
            ret,frame=capture.read()
            results=model.detect([frame],verbose=0)
            r=results[0]
            
            
            frame=display_instances(
                  frame,r['rois'], r['masks'], r['class_ids'],
                                class_names, r['scores']
            )
            
            cv2.imshow('frame',frame)
            if cv2.waitKey(1)&0xFF==ord('q'):
                break
           
        capture.release()
        cv2.destroyAllWindows()

Tensorflow Mask-RCNN(三)——实时 检测视频的更多相关文章

  1. Mask R-CNN用于目标检测和分割代码实现

    Mask R-CNN用于目标检测和分割代码实现 Mask R-CNN for object detection and instance segmentation on Keras and Tenso ...

  2. 终极指南:构建用于检测汽车损坏的Mask R-CNN模型(附Python演练)

    介绍 计算机视觉领域的应用继续令人惊叹着.从检测视频中的目标到计算人群中的人数,计算机视觉似乎没有无法克服的挑战. 这篇文章的目的是建立一个自定义Mask R-CNN模型,可以检测汽车上的损坏区域(参 ...

  3. [Network Architecture]Mask R-CNN论文解析(转)

    前言 最近有一个idea需要去验证,比较忙,看完Mask R-CNN论文了,最近会去研究Mask R-CNN的代码,论文解析转载网上的两篇博客 技术挖掘者 remanented 文章1 论文题目:Ma ...

  4. 手把手教你使用LabVIEW实现Mask R-CNN图像实例分割

    前言 前面给大家介绍了使用LabVIEW工具包实现图像分类,目标检测,今天我们来看一下如何使用LabVIEW实现Mask R-CNN图像实例分割. 一.什么是图像实例分割? 图像实例分割(Instan ...

  5. 论文笔记:Mask R-CNN

    之前在一次组会上,师弟诉苦说他用 UNet 处理一个病灶分割的任务,但效果极差,我看了他的数据后发现,那些病灶区域比起整张图而言非常的小,而 UNet 采用的损失函数通常是逐像素的分类损失,如此一来, ...

  6. Tensorflow实现Mask R-CNN实例分割通用框架,检测,分割和特征点定位一次搞定(多图)

    Mask R-CNN实例分割通用框架,检测,分割和特征点定位一次搞定(多图)   导语:Mask R-CNN是Faster R-CNN的扩展形式,能够有效地检测图像中的目标,同时还能为每个实例生成一个 ...

  7. 物体检测之FPN及Mask R-CNN

    对比目前科研届普遍喜欢把问题搞复杂,通过复杂的算法尽量把审稿人搞蒙从而提高论文的接受率的思想,无论是著名的残差网络还是这篇Mask R-CNN,大神的论文尽量遵循著名的奥卡姆剃刀原理:即在所有能解决问 ...

  8. 物体检测丨从R-CNN到Mask R-CNN

    这篇blog是我刚入目标检测方向,导师发给我的文献导读,深入浅出总结了object detection two-stage流派Faster R-CNN的发展史,读起来非常有趣.我一直想翻译这篇博客,在 ...

  9. 论文阅读笔记三十六:Mask R-CNN(CVPR2017)

    论文源址:https://arxiv.org/pdf/1703.06870.pdf 开源代码:https://github.com/matterport/Mask_RCNN 摘要 Mask R-CNN ...

随机推荐

  1. PHP代码审计之入门实战

    系统介绍 CMS名称:新秀企业网站系统PHP版 官网:www.sinsiu.com 版本:这里国光用的1.0 正式版 (官网最新的版本有毒,网站安装的时候居然默认使用远程数据库???迷之操作 那站长的 ...

  2. docker安装并设置开机启动(Linux)

    docker 开机启动: systemctl enable docker 使用的linux系统为CentOS7.2 docker分为CE和EE版本,EE版本收费,一般我们使用CE版本就满足要求了 do ...

  3. ACM-小偷的背包

    题目描述:小偷的背包   设有一个背包可以放入的物品重量为S,现有n件物品,重量分别是w1,w2,w3,...,wn.问能否从这n件物品中选择若干件放入背包中,使得放入的重量之和正好为S.如果有满足条 ...

  4. 学校算法作业X——(日期问题)

    最近一直在忙项目,难得有时间写一下作业,所以断了,现在赶紧续上 题目如下: 日历问题 问题描述 在我们现在使用的日历中, 闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外, ...

  5. Express - 采用bcryptjs进行密码加密

    1.安装bcryptjs模块 npm install bcryptjs --save 2.在需要加密的模块中引入bcryptjs库    require('bcryptjs');   实战 : /** ...

  6. apache启动错误:Could not reliably determine the server's fully qualified domain name

    启动apache遇到错误:httpd: Could not reliably determine the server's fully qualified domain name [root@serv ...

  7. (转)zookeeper理解

    分布式服务框架 Zookeeper -- 管理分布式环境中的数据 Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题 ...

  8. tools.eclipse.内存配置

    环境:jdk1.7+eclipse luna 选择:Run ->Run Configurations, 在弹出框右侧中选择Arguments, 在VM arguments最后加入 -Xms256 ...

  9. servlet中urlpatterns注意事项

    在servlet中, @WebServlet(urlPatterns="/newsAdd")接收 resp.sendRedirect("/wedding/houtai/N ...

  10. 一百一十一、SAP的OO-ALV之五,显示ALV表格

    一.在屏幕里面有2部分,(PROCESS BEFORE OUTPUT 用于显示, PROCESS AFTER INPUT用于数据处理).我们创建的display_alv函数, 二.display_al ...