-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.py
152 lines (124 loc) · 5.12 KB
/
demo.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'''
test for video
'''
import os
import argparse
from time import time
import numpy
import torch
from torchvision import transforms
from PIL import Image
import cv2
from models.get_segmentation_model import get_segmentation_model
from dataload.utils import get_pred
def parse_args():
parser = argparse.ArgumentParser(
description='Predict segmentation result from a given image')
parser.add_argument('--model', type=str, default='bisenetv',
help='model name (default: fcn32_vgg16)')
parser.add_argument('--backbone', type=str, default='resnet34')
parser.add_argument('--aux', type=bool, default=False)
parser.add_argument('--dataset', type=str, default='cityscapes',
choices=['pascal_voc, pascal_aug, ade20k, citys'],
help='dataset name (default: pascal_voc)')
parser.add_argument('--num_class', type=int, default=19)
parser.add_argument('--pretrained_model', type=str,
default=r'D:\Tramac\result\bisenetv_resnet34_cityscapes\models\best_model.pth')
parser.add_argument('--save_if', type=bool, default=True)
parser.add_argument('--base_size', type=int, default=700)
parser.add_argument('--save_dir', type=str, default=r'E:\OBS\demoOut')
parser.add_argument('--input_obj', type=str, default='D:/datasets/229575561-1-208.mp4',
help='the vedio path split with / or use camara with a number')
# parser.add_argument('--input_obj', type=int, default=0,
# help='the vedio path split with / or use camara with a number')
return parser.parse_args()
class Preder():
def __init__(self, args) -> None:
self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu")
self.dataset = args.dataset
# image transform
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
self.model = get_segmentation_model(
args.model,
dataset=args.dataset,
backbone=args.backbone,
aux=args.aux,
num_class=args.num_class
).to(self.device)
pretrained_model_state_dict = torch.load(
args.pretrained_model, self.device)
model_state_dict = self.model.state_dict()
state_dict_buffer = {k: v for k, v in pretrained_model_state_dict.items(
) if k in model_state_dict.keys()}
self.model.load_state_dict(state_dict_buffer)
self.model.eval()
def pred(self, img):
with torch.no_grad():
# img is a array
image = Image.fromarray(img).convert('RGB')
images = self.transform(image).unsqueeze(0).to(self.device)
output = self.model(images)
pred = torch.argmax(output[0], 1).squeeze(0).cpu().data.numpy()
mask = get_pred(pred, self.dataset)
return mask
if __name__ == '__main__':
args = parse_args()
originWriter = None
maskWriter = None
fourcc = None
if args.save_if:
if os.path.exists(args.save_dir):
assert os.path.isdir(args.save_dir), '%s not a dir' % args.save_dir
else:
os.makedirs(args.save_dir)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# input your camera(such as 0) or a video path
cap = cv2.VideoCapture(args.input_obj)
model = Preder(args)
while cap.isOpened():
_, img = cap.read()
# 图像读取错误
if not _:
break
# resize
oh, ow, ch = img.shape
if args.base_size != None:
img = Image.fromarray(img).convert('RGB')
if ow > oh:
h = int(oh * args.base_size/ow)
w = args.base_size
else:
w = int(ow * args.base_size/oh)
h = args.base_size
img = img.resize((w, h), Image.BILINEAR)
ow, oh = w, h
img = numpy.array(img)
mask = numpy.array(model.pred(img).convert("RGB"))
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
cv2.imshow('origin', img)
cv2.imshow('pred', mask)
if args.save_if:
if originWriter == None or maskWriter == None:
_t=os.path.join(
args.save_dir, '{}_{}_{}'.format(args.model,args.backbone,args.dataset))
if os.path.exists(_t):
assert os.path.isdir(_t), '{} is a file you need delete it first'.format(_t)
else :
os.makedirs(_t)
_s=args.input_obj.split('/')[-1].split('.')[0]
originWriter = cv2.VideoWriter(_t+'/'+_s+'_origin.avi', fourcc, 30, (ow, oh))
maskWriter = cv2.VideoWriter(_t+'/'+_s+'_mask.avi', fourcc, 30, (ow, oh))
originWriter.write(img)
maskWriter.write(mask)
# if cv2.waitKey(1) == 27:
# cv2.destroyAllWindows()
# break
cap.release()
if maskWriter != None:
maskWriter.release()
if originWriter != None:
originWriter.release()