-
Notifications
You must be signed in to change notification settings - Fork 21
/
video_processing_yolact.py
121 lines (98 loc) · 3.35 KB
/
video_processing_yolact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import cv2
import numpy as np
import sys
import argparse
# import dlib
from datetime import datetime
import os
# Yolact instance segmentation
# https://github.com/dbolya/yolact
#
# See Google Colab notebook for setup instruction
transform='yolact' #object segmentation
# counter = 0
skip_frames = 30*4
previous_grey = None
hsv = None
hsv_roi = None
roi_hist = None
term_criteria = None
x = 200
y = 350
w = 150
h = 150
def create_blank(width, height, rgb_color=(0, 0, 0)):
"""Create new image(numpy array) filled with certain color in RGB"""
# Create black blank image
image = np.zeros((height, width, 3), np.uint8)
# Since OpenCV uses BGR, convert the color first
color = tuple(reversed(rgb_color))
# Fill image with color
image[:] = color
return image
black = (0, 0, 0)
pathToProject='../yolact/'
sys.path.insert(0, pathToProject)
os.chdir(pathToProject)
from eval_colab import *
from data import config, COCODetection, get_label_map, MEANS, COLORS
from yolact import Yolact
from utils.augmentations import BaseTransform, FastBaseTransform, Resize
from utils.functions import MovingAverage, ProgressBar
from layers.box_utils import jaccard, center_size, mask_iou
from utils import timer
from utils.functions import SavePath
from layers.output_utils import postprocess, undo_image_transformation
import pycocotools
from data import cfg, set_cfg, set_dataset
def init_model(transform):
args = parse_args()
if args.config is not None:
print(args.config)
set_cfg(args.config)
cfg.mask_proto_debug = False
if args.trained_model == 'interrupt':
args.trained_model = SavePath.get_interrupt('weights/')
elif args.trained_model == 'latest':
args.trained_model = SavePath.get_latest('weights/', cfg.name)
if args.config is None:
model_path = SavePath.from_str(args.trained_model)
# TODO: Bad practice? Probably want to do a name lookup instead.
args.config = model_path.model_name + '_config'
print('Config not specified. Parsed %s from the file name.\n' % args.config)
set_cfg(args.config)
if args.detect:
cfg.eval_mask_branch = False
if args.dataset is not None:
set_dataset(args.dataset)
with torch.no_grad():
if args.cuda:
cudnn.fastest = True
torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
torch.set_default_tensor_type('torch.FloatTensor')
print('Loading model...', end='')
net = Yolact()
net.load_weights(args.trained_model)
net.eval()
print(' Done.')
net = net.cuda()
net = CustomDataParallel(net).cuda()
transform = torch.nn.DataParallel(FastBaseTransform()).cuda()
return net, args
def process_image(transform,processing_model,img):
global previous_grey, hsv, skip_frames,hsv_roi,roi_hist, term_criteria,x, y, w, h
tracks = []
try:
with torch.no_grad():
net = processing_model
frame = torch.from_numpy(img).cuda().float()
batch = FastBaseTransform()(frame.unsqueeze(0))
preds = net(batch)
# print("display predictions",preds)
img_numpy = prep_display(preds, frame, None, None, undo_transform=False)
img = img_numpy
tracks = preds
except:
pass
return tracks,img