forked from sahibdhanjal/Mask-RCNN-Pedestrian-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainLive.py
205 lines (160 loc) · 7.5 KB
/
mainLive.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
# RCNN Dependencies
import utils
import coco
import utils
import model as modellib
import visualize
# Video Detection Dependenices
import cv2
import time
import urllib.request as urllib2
# particle filter dependencies
import particle
import particleFilter as pf
if __name__ == '__main__':
# Root directory of the project
ROOT_DIR = os.getcwd()
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
class InferenceConfig(coco.CocoConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
config = InferenceConfig()
config.display()
# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)
# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush']
##################################################
# Real Time Detection
##################################################
plt.ion()
size = (16, 16)
fig, ax = plt.subplots(1, figsize = size)
# Block for IP Streaming
url = "35.3.71.126:8080"
link = 'http://' + url + '/video'
print('Streaming from: ' + link)
ctr = 0
stream = urllib2.urlopen(link)
bytes = bytes()
# Read until video is completed
while True:
bytes += stream.read(1024)
a = bytes.find(b'\xff\xd8')
b = bytes.find(b'\xff\xd9')
if a != -1 and b != -1:
start = time.time()
ctr += 1
jpg = bytes[a:b+2]
bytes = bytes[b+2:]
frame0 = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
frame0 = cv2.cvtColor(frame0, cv2.COLOR_BGR2RGB)
prevFrame = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
##################################################
# Particle Filter Initialization
##################################################
numParticles = 100
particles = [particle.Particle(0,0,0)]*numParticles
for i in range(numParticles):
particles[i] = particle.Particle(random.randint(0,frame0.shape[0]-1), random.randint(0,frame0.shape[1]-1), 1/numParticles)
if ctr%10==0:
##################################################
# Calculate Optical Flow
##################################################
frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
nextFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prevFrame, nextFrame, None, 0.5, 3, 15, 3, 5, 1.2, 0)
dx , dy = flow[...,0], flow[...,1]
prevFrame = nextFrame
##################################################
# Mask R-CNN Detection
##################################################
results = model.detect([frame], verbose=1)
##################################################
# Particle FIltering to track
##################################################
r = results[0]
boxes = r['rois']
N = boxes.shape[0]
idx = []
for k in range(N):
if r['class_ids'][k] == 1:
idx.append(k)
idS = 0; maxE = 0
for k in idx:
if r['scores'][k] > maxE:
maxE = r['scores'][k]
idS = k
for i in range(N):
if r['class_ids'][i] == 1 and i==idS:
# mean and covariance in the particle filter
y1, x1, y2, x2 = boxes[i]
# optical flow center and covariance
meanX = dx[x1:x2, y1:y2].mean()
meanY = dy[x1:x2, y1:y2].mean()
covX = (np.cov(dx[x1:x2, y1:y2])).mean()
covY = (np.cov(dy[x1:x2, y1:y2])).mean()
# bounding box center
boxX = (x1+x2)/2
boxY = (y1+y2)/2
covBox = 1 - r['scores'][i]
# Assign random mean / covariance which needs to be tuned
# currently using 0.3, 5
if math.isnan(meanX) :
meanX = 0.3
covX = 5
if math.isnan(meanY) :
meanY = 0.3
covY = 5
sumX = np.sum(dx[x1:x2, y1:y2])
sumY = np.sum(dy[x1:x2, y1:y2])
particles = pf.actionModel(particles, numParticles, boxX, boxY, meanX, meanY, covX, covY)
particles = pf.sensorModel(particles, numParticles, boxX, boxY)
for j in range(numParticles):
ax.scatter([particles[j].x], [particles[j].y])
break
##################################################
# Image Plotting
##################################################
visualize.display_instances(frame, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'], "Real Time Detection", size, ax, fig)
# Press esc on keyboard to exit
if cv2.waitKey(25) & 0xFF == 27:
exit(0)
print(time.time() - start)