资源简介
通过opencv-python调用摄像头, 传入YOLACT模型进行实时在线的目标检测和MASK标记.
详见博客 https://blog.csdn.net/Augurlee/article/details/103574125
详见博客 https://blog.csdn.net/Augurlee/article/details/103574125
代码片段和文件信息
import torch
import torch.backends.cudnn as cudnn
from data import COLORS
from data import cfg set_cfg
from yolact import Yolact
from utils.augmentations import FastbaseTransform
from utils import timer
from utils.functions import SavePath
from layers.output_utils import postprocess undo_image_transformation
import cv2 as cv
from collections import defaultdict
# 参数配置
score_threshold = 0.15
top_k = 15
display_masks = True
display_text = True
display_bboxes = True
display_scores = True
# 全局变量
iou_thresholds = [x / 100 for x in range(50 100 5)]
coco_cats = {} # Call prep_coco_cats to fill this
coco_cats_inv = {}
color_cache = defaultdict(lambda: {})
def prep_display(dets_out img h w undo_transform=True class_color=False mask_alpha=0.45 fps_str=‘‘):
“““
Note: If undo_transform=False then im_h and im_w are allowed to be None.
“““
if undo_transform:
img_numpy = undo_image_transformation(img w h)
img_gpu = torch.Tensor(img_numpy).cuda()
else:
img_gpu = img / 255.0
h w _ = img.shape
with timer.env(‘Postprocess‘):
t = postprocess(dets_out w h visualize_lincomb=False
crop_masks=True
score_threshold=score_threshold)
torch.cuda.synchronize()
with timer.env(‘Copy‘):
if cfg.eval_mask_branch:
# Masks are drawn on the GPU so don‘t copy
masks = t[3][:top_k]
classes scores boxes = [x[:top_k].cpu().numpy() for x in t[:3]]
num_dets_to_consider = min(top_k classes.shape[0])
for j in range(num_dets_to_consider):
if scores[j] < score_threshold:
num_dets_to_consider = j
break
# Quick and dirty lambda for selecting the color for a particular index
# Also keeps track of a per-gpu color cache for maximum speed
def get_color(j on_gpu=None):
global color_cache
color_idx = (classes[j] * 5 if class_color else j * 5) % len(COLORS)
if on_gpu is not None and color_idx in color_cache[on_gpu]:
return color_cache[on_gpu][color_idx]
else:
color = COLORS[color_idx]
if not undo_transform:
# The image might come in as RGB or BRG depending
color = (color[2] color[1] color[0])
if on_gpu is not None:
color = torch.Tensor(color).to(on_gpu).float() / 255.
color_cache[on_gpu][color_idx] = color
return color
# First draw the masks on the GPU where we can do it really fast
# Beware: very fast but possibly unintelligible mask-drawing code ahead
# I wish I had access to OpenGL or Vulkan but alas I guess Pytorch tensor operations will have to suffice
# After this mask is of size [num_dets h w 1]
if display_masks and cfg.eval_mask_branch and num_dets_to_consider > 0:
masks = masks[:
- 上一篇:Python多线程子域名扫描自带字典
- 下一篇:基于python的深度信念网络
相关资源
- opencv_tensorflow
- python调用opencv进行人脸检测
- opencv-python的dll
- 使用Python操作摄像头[Windows]
- Xtion_pro_live.py
- 汉字书法图像骨架提取
- Python - 截取指定帧数间隔指定大小的
- Graph Cut图像分割算法——Python+Opencv实
- 《OpenCV视觉之眼》专栏图像处理总体
- opencv实现石头剪刀布代码
- 使用单目摄像头测量距离
- 基于PYTHON+OPENCV的SIFT SURF图像特征匹配
- pycharm工程python调用OpenCV实现USB摄像头
- 基于opencv绘制图片的三维空间显示图
- 全景图像拼接python+opencv
- python 利用OpenCV 图像黑白化
- python-opencv 机器视觉 质心,形心 坐标
- 安装步骤。提取码也在里面
- BM3D去噪python代码
- python+opencv鼠标选择目标自动跟踪,
- 工训物流小车颜色及二维码识别
- Python+OpenCv实现AI人脸识别身份认证系
- 智能小车视觉巡线python代码
- python简易jpeg编码
- 树莓派利用python、opencv、PyALPR识别车
- python摄像头视频显示到TK窗口
- 人脸识别UI Pythone+pyq5+opencv 多线程模式
- 基于Python的双路视频传输及双显示系
- python调用opencv实现人脸识别的简单D
- 树莓派小车物体追踪
评论
共有 条评论