之前已经训练出自己的MASK-RCNN模型,并在单张图片上经行了测试,那么如何在摄像头中实时检测呢,今天就实现这个功能。其实方法很简单,主要就是获取摄像头的frame,然后对这个frame进行检测就行了。

import cv2
import numpy as np
from mrcnn.config import Configclass MouseConfig(Config):"""Configuration for training on the toy  dataset.Derives from the base Config class and overrides some values."""# Give the configuration a recognizable nameNAME = "mouse"# We use a GPU with 12GB memory, which can fit two images.# Adjust down if you use a smaller GPU.IMAGES_PER_GPU = 1# Number of classes (including background)NUM_CLASSES = 1 + 1  # Background + balloon# Number of training steps per epochSTEPS_PER_EPOCH = 100# Skip detections with < 90% confidenceDETECTION_MIN_CONFIDENCE = 0.9def random_colors(N):np.random.seed(1)colors = [tuple(255 * np.random.rand(3)) for _ in range(N)]return colorsdef apply_mask(image, mask, color, alpha=0.5):for n, c in enumerate(color):image[:, :, n] = np.where(mask == 1,image[:, :, n] * (1 - alpha) + alpha * c,image[:, :, n])return imagedef 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]):continuey1, 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 Nonecaption = '{}{:.2f}'.format(label, score) if score else labelimage = cv2.putText(image, caption, (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.7, color, 2)return imageif __name__ == '__main__':import osimport sysimport randomimport utilsROOT_DIR = os.path.abspath("../")sys.path.append(ROOT_DIR)from mrcnn import utilsimport mrcnn.model as modellibsys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local versionMODEL_DIR = os.path.join(ROOT_DIR, "logs")COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_mouse_0030.h5")print('COCO_MODEL_PATH: ',COCO_MODEL_PATH)if not os.path.exists(COCO_MODEL_PATH):print('cannot find coco_model')class InferenceConfig(MouseConfig):GPU_COUNT = 1IMAGES_PER_GPU = 1config = InferenceConfig()config.display()model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)# Load weights trained on MS-COCOmodel.load_weights(COCO_MODEL_PATH, by_name=True)class_names = ['BG', 'mouse', '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(1)# 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('camera1', frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakcapture.release()cv2.destroyAllWindows()

摄像头实时显示的结果如下:

参考:https://blog.csdn.net/eereere/article/details/80178595

将自己训练的MASK-RCNN模型用于摄像头实时检测相关推荐

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

    阅读时间将近11分钟 介绍 计算机视觉领域的应用继续令人惊叹着.从检测视频中的目标到计算人群中的人数,计算机视觉似乎没有无法克服的挑战. 这篇文章的目的是建立一个自定义Mask R-CNN模型,可以检 ...

  2. 【CV】Mask R-CNN:用于目标实例分割的通用框架

    论文名称:Mask R-CNN 论文下载:https://arxiv.org/abs/1703.06870 论文年份:2017 论文被引:18354(2022/05/03) Abstract We p ...

  3. 【笔记】基于 Mask R-CNN 的玉米田间杂草检测方法

    <基于 Mask R-CNN 的玉米田间杂草检测方法> 单位:山东农业大学信息科学与工程学院 作者:姜红花 数据获取 相机:Intel RealSense Depth CameraD435 ...

  4. 2021极术通讯-CSL-YOLO | 超越Tiny-YOLO V4,全新设计轻量化YOLO模型实现边缘实时检测

    首发极术社区 如对Arm相关技术感兴趣,欢迎私信aijishu20加入技术微信群. 导读:极术社区与E-learning平台联合推出极术通讯,引入行业媒体和技术社区.咨询机构优质内容,定期分享产业技术 ...

  5. Mask R-CNN 模型

    数据准备 要训练 Mask R-CNN 实例分割模型,我们首先要准备图像的掩模(mask),使用标注工具 labelme(支持 Windows 和 Ubuntu,使用 (sudo) pip insta ...

  6. 【Pytorch神经网络理论篇】 33 基于图片内容处理的机器视觉:目标检测+图片分割+非极大值抑制+Mask R-CNN模型

    基于图片内容的处理任务,主要包括目标检测.图片分割两大任务. 1 目标检测 目标检测任务的精度相对较高,主要是以检测框的方式,找出图片中目标物体所在的位置.目标检测任务的模型运算量相对较小,速度相对较 ...

  7. Mask R-CNN:实例分割与检测算法

    摘要 目的:对象实例分割(区别出不同的对象) 提出:Mask R-CNN(扩展的FasterR-CNN)--通过在每个RoI处添加用于预测分割的mask(小的FCN),与用于分类和边界框回归的Fast ...

  8. CVPR 2023 | 白翔团队提出:将CLIP模型用于场景文本检测

    点击下方卡片,关注"CVer"公众号 AI/CV重磅干货,第一时间送达 点击进入->[计算机视觉]微信技术交流群 转载自:CSIG文档图像分析与识别专委会  本文简要介绍CV ...

  9. HRNet-v1模型,用于人体形态检测

    源码参考:GitHub - HRNet/HRNet-Human-Pose-Estimation: This repo is copied from https://github.com/leoxiao ...

最新文章

  1. notepad 查找php函数,Notepad++中常用的技巧总结
  2. {面试题2: 实现 Singleton 模式}
  3. 2013\Province_Java_A\2.振兴中华
  4. 你当真了解count(*)count(id)count(1)吗?
  5. JSON学习笔记(三)- 数组
  6. 设置ComboBox控件的提示内容.
  7. 【SCOI2005】【BZOJ1083】繁忙的都市(MST模板)
  8. NodeJS写模块和引入模块的例子
  9. ubuntu server 14.04/16.x 开启 root ssh 登录
  10. pythonexcel怎么读_python怎么读excel
  11. NBIOT 关键术语
  12. python多个if_Python之条件判断/if嵌套/如何写嵌套代码
  13. 编码中关于二义性的解释
  14. 宏碁(Acer)蜂鸟Fun 2020新款 14英寸轻薄商务笔记本使用真实评测
  15. 1520 - 骑士的金币(coin)
  16. pinyin4j:拼音与汉字的转换实例
  17. html5四季特点,美国一年四季天气特点介绍
  18. 【MySQL】主从复制
  19. 曾遭作者“删库”的faker.js,现被社区接手;Apache Ambari 项目被弃用;FFmpeg 5.0 发布 | 开源日报
  20. 链表---给定一个排序链表,删除所有重复的元素每个元素只留下一个

热门文章

  1. DHCP+DHCP中继
  2. java根据word模板导出_java根据模板生成,导出word和pdf(aspose.words实现word转换pdf)...
  3. 教新手了解怎么从网络中赚钱
  4. Office2016 64位安装包+只安装3件套
  5. 《安富莱嵌入式周报》第295期:世界杯球员和足球实时跟踪,开源手持矢量网络分析仪,自制柔性电容式传感器,IAR加强对VSCode支持、索尼早期PSX的光驱模拟器
  6. office 2016输入超过4阶矩阵
  7. 实现简单计算器 两个数字的加减乘除计算
  8. css 更换图片颜色
  9. x265常用编码参数
  10. 对象存储、文件存储、块存储区别介绍