forked from seth814/Semantic-Shapes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.py
136 lines (112 loc) · 4.37 KB
/
stream.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
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
import os
import time
import contextlib
import numpy as np
import cv2
from utils import VideoStream
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import to_categorical
from models import preprocess_input, dice
from config import imshape, model_name, n_classes
from utils import add_masks, crf
with contextlib.redirect_stdout(None):
import pygame
RUN = True
MODE = 'softmax'
CALC_CRF = False
BACKGROUND = False
frame_shape = (640, 480)
target_shape = imshape[:2]
d_width = target_shape[0] // 2
d_height = target_shape[1] // 2
x0 = frame_shape[1] // 2 - d_width
y0 = frame_shape[0] // 2 - d_height
x1 = frame_shape[1] // 2 + d_width
y1 = frame_shape[0] // 2 + d_height
model = load_model(os.path.join('models', model_name+'.model'),
custom_objects={'dice': dice})
pygame.init()
screen = pygame.display.set_mode(frame_shape)
vs = VideoStream(device=0).start()
time.sleep(0.1)
prev = time.time()
while RUN:
current = time.time()
# camera stream
if vs.check_queue():
delta = current - prev
prev = current
screen.fill([0,0,0])
frame = vs.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
im = frame.copy()
roi = im[x0:x1, y0:y1]
tmp = np.expand_dims(roi, axis=0)
roi_pred = model.predict(tmp)
if MODE == 'argmax':
if n_classes == 1:
roi_pred = roi_pred.squeeze()
roi_softmax = np.stack([1-roi_pred, roi_pred], axis=2)
roi_max = np.argmax(roi_softmax, axis=2)
roi_pred = np.array(roi_max, dtype=np.float32)
elif n_classes > 1:
roi_max = np.argmax(roi_pred.squeeze(), axis=2)
roi_pred = to_categorical(roi_max)
if CALC_CRF:
if n_classes == 1:
roi_pred = roi_pred.squeeze()
roi_softmax = np.stack([1-roi_pred, roi_pred], axis=2)
roi_mask = crf(roi_softmax, roi)
roi_mask = np.array(roi_mask, dtype=np.float32)
roi_mask = cv2.cvtColor(roi_mask, cv2.COLOR_GRAY2RGB)
elif n_classes > 1:
roi_mask = crf(roi_pred.squeeze(), roi)
else:
if n_classes == 1:
roi_mask = roi_pred.squeeze()*255.0
roi_mask = cv2.cvtColor(roi_mask, cv2.COLOR_GRAY2RGB)
elif n_classes > 1:
roi_mask = add_masks(roi_pred.squeeze()*255.0)
if BACKGROUND:
roi_mask = np.array(roi_mask, dtype=np.uint8)
roi_mask = cv2.addWeighted(roi, 1.0, roi_mask, 1.0, 0)
frame[x0:x1, y0:y1] = roi_mask
cv2.rectangle(frame, (y0, x0), (y1, x1), (0,0,255), 2)
cv2.putText(frame, 'FPS: '+str(np.round(1/delta, 1)), (10,30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 1, cv2.LINE_AA)
cv2.putText(frame, 'MODE: '+str(MODE), (10,70),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 1, cv2.LINE_AA)
cv2.putText(frame, 'CRF: '+str(CALC_CRF), (10,110),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 1, cv2.LINE_AA)
frame = cv2.flip(frame, 0)
frame = np.rot90(frame, k=3)
frame = pygame.surfarray.make_surface(frame)
screen.blit(frame, (0,0))
pygame.display.update()
for event in pygame.event.get():
keys = pygame.key.get_pressed()
# Close window by pressing X in top right
if event.type == pygame.QUIT:
RUN = False
# Press Q to close window
elif event.type == pygame.KEYDOWN and keys[pygame.K_q]:
RUN = False
# C turns on Conditional Random Field Processing
elif event.type == pygame.KEYDOWN and keys[pygame.K_c]:
CALC_CRF = not(CALC_CRF)
# M switches from probability to argmax output
elif event.type == pygame.KEYDOWN and keys[pygame.K_m]:
if MODE == 'softmax':
MODE = 'argmax'
else:
MODE = 'softmax'
# B toggles background on/ off
elif event.type == pygame.KEYDOWN and keys[pygame.K_b]:
if BACKGROUND == False:
BACKGROUND = True
else:
BACKGROUND = False
cv2.destroyAllWindows()
vs.stop()