当前位置: 首页 > news >正文

【python】OpenCV—Mask RCNN for Object Detection and Instance Segmentation

在这里插入图片描述

文章目录

  • 1、任务描述
  • 2、MASR RCNN 网络结构
  • 3、方法实现
  • 4、结果展示
  • 5、涉及到的库
    • getPerfProfile
  • 6、参考

1、任务描述

在这里插入图片描述

利用 mask rcnn 网络,进行图片和视频的目标检测和实例分割

2、MASR RCNN 网络结构

在这里插入图片描述

3、方法实现

# Copyright (C) 2018-2019, BigVision LLC (LearnOpenCV.com), All Rights Reserved.
# Author : Sunita Nayak
# Article : https://www.learnopencv.com/deep-learning-based-object-detection-and-instance-segmentation-using-mask-r-cnn-in-opencv-python-c/
# License: BSD-3-Clause-Attribution (Please read the license file.)
# This work is based on OpenCV samples code (https://opencv.org/license.html)import cv2 as cv
import argparse
import numpy as np
import os.path
import sys
import random# Initialize the parameters
confThreshold = 0.5  # Confidence threshold
maskThreshold = 0.3  # Mask thresholdparser = argparse.ArgumentParser(description='Use this script to run Mask-RCNN object detection and segmentation')
parser.add_argument('--image', help='Path to image file.')
parser.add_argument('--video', help='Path to video file.', default="cars.mp4")
parser.add_argument("--device", default="gpu", help="Device to inference on")
args = parser.parse_args()"""
python mask_rcnn.py --image ./images/person.jpg --device cpu
python mask_rcnn.py --video ./cars.mp4 --device cpu
"""# Draw the predicted bounding box, colorize and show the mask on the image
def drawBox(frame, classId, conf, left, top, right, bottom, classMask):# Draw a bounding box.cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)# Print a label of class.label = '%.2f' % confif classes:assert (classId < len(classes))label = '%s:%s' % (classes[classId], label)  # 'person:1.00'# Display the label at the top of the bounding boxlabelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)top = max(top, labelSize[1])cv.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine),(255, 255, 255), cv.FILLED)cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)# Resize the mask, threshold, color and apply it on the imageclassMask = cv.resize(classMask, (right - left + 1, bottom - top + 1))mask = (classMask > maskThreshold)roi = frame[top:bottom + 1, left:right + 1][mask]# color = colors[classId%len(colors)]# Comment the above line and uncomment the two lines below to generate different instance colorscolorIndex = random.randint(0, len(colors) - 1)color = colors[colorIndex]frame[top:bottom + 1, left:right + 1][mask] = ([0.3 * color[0], 0.3 * color[1], 0.3 * color[2]] + 0.7 * roi).astype(np.uint8)# Draw the contours on the imagemask = mask.astype(np.uint8)contours, hierarchy = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)cv.drawContours(frame[top:bottom + 1, left:right + 1], contours, -1, color, 3, cv.LINE_8, hierarchy, 100)# For each frame, extract the bounding box and mask for each detected object
def postprocess(boxes, masks):# Output size of masks is NxCxHxW where# N - number of detected boxes# C - number of classes (excluding background)# HxW - segmentation shapenumClasses = masks.shape[1]  # 90numDetections = boxes.shape[2]  # 100frameH = frame.shape[0]  # 531frameW = frame.shape[1]  # 800for i in range(numDetections):  # traverse top 100 ROIbox = boxes[0, 0, i]  # (1, 1, 100, 7) -> (7,)# array([0.        , 0.        , 0.99842095, 0.7533724 , 0.152397  , 0.92448074, 0.9131955 ], dtype=float32)mask = masks[i]  # (100, 90, 15, 15) -> (90, 15, 15)score = box[2]  # 0.99842095if score > confThreshold:classId = int(box[1])# Extract the bounding boxleft = int(frameW * box[3])top = int(frameH * box[4])right = int(frameW * box[5])bottom = int(frameH * box[6])left = max(0, min(left, frameW - 1))top = max(0, min(top, frameH - 1))right = max(0, min(right, frameW - 1))bottom = max(0, min(bottom, frameH - 1))# Extract the mask for the objectclassMask = mask[classId]# Draw bounding box, colorize and show the mask on the imagedrawBox(frame, classId, score, left, top, right, bottom, classMask)# Load names of classes
classesFile = "mscoco_labels.names"
classes = None"""
person
bicycle
car
motorcycle
airplane
bus
train
truck
boat
traffic light
fire hydrantstop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffebackpack
umbrellahandbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottlewine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
couch
potted plant
beddining tabletoilettv
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigeratorbook
clock
vase
scissors
teddy bear
hair drier
toothbrush
"""with open(classesFile, 'rt') as f:classes = f.read().rstrip('\n').split('\n')# Give the textGraph and weight files for the model
textGraph = "./mask_rcnn_inception_v2_coco_2018_01_28.pbtxt"
modelWeights = "./mask_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb"# Load the network
net = cv.dnn.readNetFromTensorflow(modelWeights, textGraph)if args.device == "cpu":net.setPreferableBackend(cv.dnn.DNN_TARGET_CPU)print("Using CPU device")
elif args.device == "gpu":net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA)print("Using GPU device")# Load the classes
colorsFile = "colors.txt"
with open(colorsFile, 'rt') as f:colorsStr = f.read().rstrip('\n').split('\n')# ['0 255 0', '0 0 255', '255 0 0', '0 255 255', '255 255 0', '255 0 255', '80 70 180',# '250 80 190', '245 145 50', '70 150 250', '50 190 190']colors = []  # [0,0,0]
for i in range(len(colorsStr)):rgb = colorsStr[i].split(' ')color = np.array([float(rgb[0]), float(rgb[1]), float(rgb[2])])colors.append(color)"""
[array([  0., 255.,   0.]), array([  0.,   0., 255.]), array([255.,   0.,   0.]), array([  0., 255., 255.]), 
array([255., 255.,   0.]), array([255.,   0., 255.]), array([ 80.,  70., 180.]), array([250.,  80., 190.]), 
array([245., 145.,  50.]), array([ 70., 150., 250.]), array([ 50., 190., 190.])]
"""winName = 'Mask-RCNN Object detection and Segmentation in OpenCV'
cv.namedWindow(winName, cv.WINDOW_NORMAL)outputFile = "mask_rcnn_out_py.avi"
if (args.image):# Open the image fileif not os.path.isfile(args.image):print("Input image file ", args.image, " doesn't exist")sys.exit(1)cap = cv.VideoCapture(args.image)outputFile = args.image[:-4] + '_mask_rcnn_out_py.jpg'
elif (args.video):# Open the video fileif not os.path.isfile(args.video):print("Input video file ", args.video, " doesn't exist")sys.exit(1)cap = cv.VideoCapture(args.video)outputFile = args.video[:-4] + '_mask_rcnn_out_py.avi'
else:# Webcam inputcap = cv.VideoCapture(0)# Get the video writer initialized to save the output video
if (not args.image):vid_writer = cv.VideoWriter(outputFile, cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 28,(round(cap.get(cv.CAP_PROP_FRAME_WIDTH)), round(cap.get(cv.CAP_PROP_FRAME_HEIGHT))))while cv.waitKey(1) < 0:# Get frame from the videohasFrame, frame = cap.read()# Stop the program if reached end of videoif not hasFrame:print("Done processing !!!")print("Output file is stored as ", outputFile)cv.waitKey(3000)break# Create a 4D blob from a frame.blob = cv.dnn.blobFromImage(frame, swapRB=True, crop=False)  # (1, 3, 531, 800)# Set the input to the networknet.setInput(blob)# Run the forward pass to get output from the output layersboxes, masks = net.forward(['detection_out_final', 'detection_masks'])"""(1, 1, 100, 7)  top 100 RoI, (0, classid, score, x0, y0, x1, y1)(100, 90, 15, 15) 100 RoI, 90 classes, 15*15 feature maps size"""# Extract the bounding box and mask for each of the detected objectspostprocess(boxes, masks)# Put efficiency information.t, _ = net.getPerfProfile()label = 'Mask-RCNN Inference time for a frame : %0.0f ms' % abs(t * 1000.0 / cv.getTickFrequency())cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))# Write the frame with the detection boxesif (args.image):cv.imwrite(outputFile, frame.astype(np.uint8));else:vid_writer.write(frame.astype(np.uint8))cv.imshow(winName, frame)

根据 bbox 的类别,取 mask 输出特征图对应类别的通道特征 classMask = mask[classId]

画 mask 的时候,先 resize 到 bbox 的大小,再借助了 cv2.findContoursdrawContours 绘制出轮廓

4、结果展示

输入图片

在这里插入图片描述

输出结果

在这里插入图片描述

输入图片

在这里插入图片描述

输出结果

在这里插入图片描述

输入图片

在这里插入图片描述

输出结果

在这里插入图片描述

输入图片

在这里插入图片描述

输出结果

在这里插入图片描述

输入图片

在这里插入图片描述

输出结果

在这里插入图片描述

输入图片

在这里插入图片描述

输出结果

在这里插入图片描述

输入图片

在这里插入图片描述

输出结果

在这里插入图片描述


看看视频的结果

cars_mask_rcnn_out

5、涉及到的库

getPerfProfile

getPerfProfile 是 OpenCV 库中用于获取深度学习模型推理性能概况的一个函数。该函数主要用于分析模型中各层的执行时间,帮助开发者了解模型性能瓶颈和优化方向。

一、功能

getPerfProfile 函数返回一个包含模型各层执行时间的向量(或类似结构),单位通常为毫秒或秒,具体取决于函数实现和调用方式。

通过这个函数,开发者可以获取到模型推理过程中每一层所消耗的时间,进而分析哪些层是性能瓶颈,需要进一步优化。
使用场景

在使用 OpenCV 进行深度学习模型推理时,尤其是在对实时性要求较高的应用场景中,如视频处理、实时监控系统等,使用 getPerfProfile 函数可以帮助开发者评估和优化模型性能。

二、示例代码

import cv2  # 加载预训练模型  
net = cv2.dnn.readNet("model.xml", "model.bin")  # 假设有一个输入图像 blob  
blob = cv2.dnn.blobFromImage(...)  # 设置输入并进行推理  
net.setInput(blob)  
outputs = net.forward()  # 获取性能概况  
t, _ = net.getPerfProfile()  # 假设 t 是以秒为单位的时间,转换为毫秒并打印  
print("Inference time: %.2f ms" % (t * 1000.0))  # 如果需要更详细的每层时间,可以遍历 t  
# 注意:这里的 t 可能是一个向量,包含了多层的执行时间  
for layer_idx, layer_time in enumerate(t):  print(f"Layer {layer_idx}: {layer_time * 1000.0} ms")

三、注意事项

getPerfProfile 函数的返回值和单位可能因 OpenCV 的不同版本或不同的深度学习后端(如 DNN 模块支持的 TensorFlow、Caffe、PyTorch 等)而有所不同。

在使用该函数时,请确保您的 OpenCV 版本支持该功能,并仔细阅读相关文档以了解其具体用法和注意事项。

由于深度学习模型的复杂性和多样性,getPerfProfile 函数提供的性能数据仅供参考,实际的优化工作还需要结合模型的具体结构和应用场景进行。

6、参考

  • 论文解读
    【Mask RCNN】《Mask R-CNN》

  • tensorflow 代码解读
    Mask RCNN without Mask

  • OpenCV进阶(7)在 OpenCV中使用 Mask RCNN实现对象检测和实例分割

相关文章:

【python】OpenCV—Mask RCNN for Object Detection and Instance Segmentation

文章目录 1、任务描述2、MASR RCNN 网络结构3、方法实现4、结果展示5、涉及到的库getPerfProfile 6、参考 1、任务描述 利用 mask rcnn 网络&#xff0c;进行图片和视频的目标检测和实例分割 2、MASR RCNN 网络结构 3、方法实现 # Copyright (C) 2018-2019, BigVision LLC (L…...

通过 Python 使用 Pexels图片库 API 打造个性化壁纸应用

在数字时代&#xff0c;照片不仅仅是回忆的载体&#xff0c;它们还是我们生活的美丽装饰品。想象一下&#xff0c;如果你能轻松地将世界上最美的免费图片应用到你的应用程序中&#xff0c;岂不是让你的程序立刻闪亮起来&#xff1f;好消息是&#xff0c;这不仅仅是一个梦想。今…...

多线程篇(其它容器- CopyOnWriteArrayList)(持续更新迭代)

一、CopyOnWriteArrayList&#xff08;一&#xff09; 1. 简介 并发包中的并发List只有CopyOnWriteArrayList。 CopyOnWriteArrayList是一个线程安全的ArrayList&#xff0c;对其进行的修改操作都是在底层的一个复制的数 组&#xff08;快照&#xff09;上进行的&#xff0…...

OPENAIGC开发者大赛高校组金奖 | 知洞—基于大模型的智慧题库

在第二届拯救者杯OPENAIGC开发者大赛中&#xff0c;涌现出一批技术突出、创意卓越的作品。为了让这些优秀项目被更多人看到&#xff0c;我们特意开设了优秀作品报道专栏&#xff0c;旨在展示其独特之处和开发者的精彩故事。 无论您是技术专家还是爱好者&#xff0c;希望能带给您…...

java服务CPU使用率高排查

第一步 使用top命令进行查看 如果是死锁&#xff0c;cpu使用率不会很高&#xff0c;但不会响应&#xff0c;这时这样排查。 第二步 使用jps查看到进程id,再使用jstack pid查看线程堆栈信息 jstack pid会出现如图所示的信息&#xff0c;表示发生死锁&#xff0c;然后去排查这…...

聚焦:clicOH 借助 NVIDIA cuOpt 实现最后一英里交付速度 20 倍提升

受消费者行为转变和疫情影响&#xff0c;电子商务继续呈爆炸式增长和转型。因此&#xff0c;物流和运输公司发现自己处于包裹配送革命的前沿。这新的现实情况在最后一英里配送中尤为明显&#xff0c;而后者现在已经成为供应链物流中成本最高的要素&#xff0c;占从零售到制造等…...

从头开始嵌入式第三十八天(数据结构 双向链表)

目录 双向链表 一、结构特点 二、操作优势 三、应用场景 1.创建链表 2.头插数据 3.打印数据 4.查找数据 5.删除数据 6.更改数据 7.清空数据 8.尾插数据 9.按位插入 10.获取长度 11.是否为空 双向链表 双向链表是一种链表结构。 一、结构特点 1. 每个节点包含两个…...

chapter14-集合——(List-HashSet)——day18

目录 519-HashSet全面说明 520-数组链表模拟 521-HashSet扩容机制 重要 522-HashSet源码解读1 526-HashSet最佳实践 527-hashSet思考题 519-HashSet全面说明 题一、 两个tom都可以添加成功是因为这是两个对象 看源码做分析&#xff1a;不是直接指向常量池的吗&#xff1f;…...

企业会议室预约管理系统

基于springbootvuemysql实现的企业会议室预约管理系统&#xff08;源码数据库部署视频&#xff09; ### 主要技术 SpringBoot、Vue、MySQL ### 系统角色 员工、管理员 ### 系统功能 1&#xff09;管理员&#xff1a;数据统计&#xff08;会议室使用统计-柱状图、设备状态统计…...

安全API

提到安全&#xff0c;大部分在学习和调试编程语言时并不太在意&#xff0c;真正爆发问题是在用户端。真正的安全漏洞是很可怕的&#xff0c;获取系统最高权限&#xff0c;获得敏感资讯&#xff0c;伤心不已&#xff0c;泪流满面。 字符串 C语言简洁自由的风格&#xff0c;产生…...

【论文阅读】视觉分割新SOTA: Segment Anything(SAM)

导言 随着基于对比文本—图像对的预训练&#xff08;CLIP&#xff09;方法或者模型、聊天生成预训练转换器&#xff08;ChatGPT&#xff09;、生成预训练转换器-4&#xff08;GPT-4&#xff09;等基础大模型的出现&#xff0c;通用人工智能&#xff08; AGI&#xff09;的研究…...

redis之list核心命令演示与细节探索

redis之list核心命令演示与细节探索 BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout summary: Pop an element from a list, push it to another list and return it; or block until one is available since: 6.2.0 BLPOP key [key …] timeout summary: Remove …...

[数据集][目标检测]智慧农业草莓叶子病虫害检测数据集VOC+YOLO格式4040张9类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;4040 标注数量(xml文件个数)&#xff1a;4040 标注数量(txt文件个数)&#xff1a;4040 标注…...

Lua 与 C#交互

Lua 与C#交互 前提 Lua是一种嵌入式脚本语言&#xff0c;Lua的解释器是用C编写的&#xff0c;因此可以方便的与C/C进行相互调用。轻量级 Lua语言的官方版本只包括一个精简的核心和最基本的库&#xff0c;这使得Lua体积小、启动速度快&#xff0c;也适合嵌入在别的程序里。 交…...

【办公类】大组工会学习(文心一言+Python批量)

背景需求&#xff1a; 每学期要写一份工会的大组政治学习读后感&#xff08;9月-1月&#xff0c;共5次&#xff09; 学习内容 9月、10月、11月、12月、1月的学习内容文字稿 在班级里&#xff0c;我擅长电脑工作&#xff0c;所以这种写的工作都包了。 中2班三位老师一共写3篇&…...

项目管理工作流是什么?项目管理工作流管理实战技巧!

项目管理工作流是指在协作过程中通过限制任务状态的流转进行流程控制的一种方式。项目从启动到完成所经历的一系列有序、可控的步骤和流程&#xff0c;它详细描述了项目执行过程中各项任务和活动的顺序、依赖关系、责任人以及完成标准等&#xff0c;是项目成功执行的重要保障。…...

leveldb源码剖析(二)——LSM Tree

LSM Tree LSM Tree&#xff1a;Log-Structured Merge Tree&#xff0c;日志结构合并树。是一种频繁写性能很高的数据结构。 LSM Tree将写入操作与合并操作分离&#xff0c;数据首先写入磁盘中的日志文件&#xff08;WAL&#xff09;&#xff0c;随后写入内存缓存&#xff0c;…...

三十六、Gin注册功能-检查账号是否存在

一、初始化 1、在cms.go中添加数据库连接方法 func connDB(app *CmsApp) {mysqlDB, err : gorm.Open(mysql.Open("root:rootroottcp(localhost:3306)/?charsetutf8mb4&parseTimeTrue&locLocal"))if err ! nil {panic(err)}db, err : mysqlDB.DB()if err !…...

什么是期权对冲?

今天期权懂带你了解什么是期权对冲&#xff1f;期权对冲的选择取决于投资者的市场预期和风险承受能力&#xff0c;通过合理使用期权对冲策略&#xff0c;可以有效减少风险并优化投资组合的表现。 期权对冲是什么&#xff1f; 期权是一种支持双向交易的投资产品&#xff0c;期…...

什么是数据库课程设计?

文章目录 前言一、课程设计目的二、课程设计流程三、设计要点四、示例项目总结 前言 数据库课程设计是一个综合性的实践过程&#xff0c;旨在通过实际项目的设计与实现&#xff0c;加深学生对数据库理论知识的理解和应用能力。 以下是一个关于数据库课程设计的基本框架和要点&…...

走进低代码报表开发(二):高效报表设计新利器

在前面的文章中&#xff0c;我们已经详细介绍了勤研低代码开发平台的报表数据源可视化设计&#xff0c;接下来&#xff0c;让我们一起来继续了解勤研低代码平台的报表设计&#xff0c;在当今数字化快速发展的时代&#xff0c;高效便捷的开发工具对于企业和开发者来说至关重要。…...

校园水电费管理|基于java的校园水电费管理小程序系统 (源码+数据库+文档)

校园水电费管理 目录 基于java的校园水电费管理小程序系统 一、前言 二、系统设计 三、系统功能设计 小程序端 后台功能模块 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#xff1a;✌️大厂码农|毕…...

java设计模式 桥接模式

桥接模式&#xff08;Bridge Pattern&#xff09;是一种结构型设计模式&#xff0c;旨在将抽象部分与其实现部分分离&#xff0c;使它们都可以独立地变化。桥接模式通过将继承改为组合&#xff0c;实现了在不修改现有类的情况下&#xff0c;动态地切换和扩展抽象类与其具体实现…...

如何利用大数据技术来识别和预防网络赌博行为?

1.构建赌博账户识别模型&#xff1a;通过大数据分析和机器学习技术&#xff0c;建立智能风险防控体系&#xff0c;对账户进行全生命周期管理&#xff0c;精准打击和切断不法分子的资金链条 。 2.分析资金流动&#xff1a;利用大数据技术监测和分析异常资金流动&#xff0c;识别…...

N-152基于java贪吃蛇游戏5

开发工具eclipse,jdk1.8 文档截图&#xff1a; N-152基于java贪吃蛇游戏5...

从线段中搜寻提取闭合轮廓(三)

1.前言 做底层和数据的调试问题也是个麻烦事&#xff0c;如果没有方便的可视化工具辅助&#xff0c;那将令人感到痛苦&#xff0c;借助可视化的工具可以让我们高效、省心&#xff0c;进而心情舒畅&#xff0c;重要的是可以提高调试效率。 当然可视化工具也分不同层次的…...

最全面的递归算法详解,一篇足矣(高手必备)

在编程中&#xff0c;递归和循环是两种常用的控制结构&#xff0c;各有其独特的优缺点。理解这两者的特点和应用场景&#xff0c;对于编写高效、可读的代码至关重要。 什么是递归&#xff1f; 递归是一种强大的编程技术&#xff0c;允许函数在其定义中调用自身。递归通常涉及…...

数据结构(2)单向链表排序和双向链表操作

一单向链表的插入排序 void insertion_sort_link(link_t* plink) { // 如果链表头为空&#xff0c;直接返回 if(NULL plink->phead) { return; } // 初始化指针&#xff0c;p指向当前已排序部分的最后一个节点 node_t* p plink->phead; // ptemp指向待插入的…...

OpenCV结构分析与形状描述符(14)拟合直线函数fitLine()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 拟合一条直线到2D或3D点集。 fitLine 函数通过最小化 ∑ i ρ ( r i ) \sum_i \rho(r_i) ∑i​ρ(ri​)来拟合一条直线到2D或3D点集&#xff0c…...

Mysql基础练习题 1757.可回收且低脂的产品(力扣)

编写解决方案找出既是低脂又是可回收的产品编号。 题目链接&#xff1a; https://leetcode.cn/problems/recyclable-and-low-fat-products/description/ 建表插入数据&#xff1a; Create table If Not Exists Products (product_id int, low_fats ENUM(Y, N), recyclable …...