資源簡介
通過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]?????????????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的深度信念網絡
評論
共有 條評論