-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdetect.py
187 lines (151 loc) · 6.12 KB
/
detect.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
import os
import sys
import numpy as np
import cv2
# Root directory of the project
ROOT_DIR = os.path.abspath("/content")
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn.config import Config
import mrcnn.model as modellib
# model directory and path
# Root directory of the project
ROOT_DIR = os.path.abspath("../../")
# Directory to save logs and trained model
LOGS_DIR = os.path.join(ROOT_DIR, "logs")
# Device to load the neural network on.
# Useful if you're training a model on the same
# machine, in which case use CPU and leave the
# GPU for training.
DEVICE = "/gpu:0" # /cpu:0 or /gpu:0
# Inspect the model in training or inference modes
# values: 'inference' or 'training'
# TODO: code for 'training' test mode not ready yet
TEST_MODE = "inference"
class ADConfig(Config):
"""Configuration for training on the toy shapes dataset.
Derives from the base Config class and overrides values specific
to the toy shapes dataset.
"""
# Give the configuration a recognizable name
NAME = "ad"
BACKBONE = "resnet101"
# Train on 1 GPU and 8 images per GPU. We can put multiple images on each
# GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
GPU_COUNT = 1
IMAGES_PER_GPU = 1
# Number of classes (including background)
NUM_CLASSES = 1 + 1 # background + 3 shapes
# Use small images for faster training. Set the limits of the small side
# the large side, and that determines the image shape.
IMAGE_RESIZE_MODE = "square"
IMAGE_MIN_DIM = 1024
IMAGE_MAX_DIM = 1024
DETECTION_MIN_CONFIDENCE = 0
RPN_ANCHOR_RATIOS = [0.5, 1, 2]
RPN_NMS_THRESHOLD = 0.9
RPN_TRAIN_ANCHORS_PER_IMAGE = 64
TRAIN_ROIS_PER_IMAGE = 128
MAX_GT_INSTANCES = 200
DETECTION_MAX_INSTANCES = 400
# Use smaller anchors because our image and objects are small
# anchor side in pixels
# Reduce training ROIs per image because the images are small and have
# few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
# Use a small epoch since the data is simple
STEPS_PER_EPOCH = 500
# use small validation steps since the epoch is small
VALIDATION_STEPS = 50
import datetime
def ad_blur(image, mask):
"""Apply color splash effect.
image: RGB image [height, width, 3]
mask: instance segmentation mask [height, width, instance count]
Returns result image.
"""
# Make a grayscale copy of the image. The grayscale copy still
# has 3 RGB channels, though.
blur = cv2.blur(image,(33,33))
# Copy color pixels from the original color image where mask is set
if mask.shape[-1] > 0:
# We're treating all instances as one, so collapse the mask into one layer
mask = (np.sum(mask, -1, keepdims=True) >= 1)
splash = np.where(mask, blur, image).astype(np.uint8)
else:
splash = image.astype(np.uint8)
return splash
def detect_and_ad_blur(model, image_path=None, video_path=None):
assert image_path or video_path
# Image or video?
if image_path:
# Run model detection and generate the color splash effect
# Read image
image = skimage.io.imread(image_path)
# Detect objects
r = model.detect([image], verbose=1)[0]
# Color splash
splash = ad_blur(image, r['masks'])
# Save output
file_name = "ad_{:%Y%m%dT%H%M%S}.png".format(datetime.datetime.now())
skimage.io.imsave(file_name, splash)
elif video_path:
import cv2
# Video capture
vcapture = cv2.VideoCapture(video_path)
width = int(vcapture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(vcapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = vcapture.get(cv2.CAP_PROP_FPS)
# Define codec and create video writer
file_name = "ad_{:%Y%m%dT%H%M%S}.avi".format(datetime.datetime.now())
vwriter = cv2.VideoWriter(file_name,
cv2.VideoWriter_fourcc(*'MJPG'),
fps, (width, height))
count = 0
success = True
while success:
# Read next image
success, image = vcapture.read()
if success:
# OpenCV returns images as BGR, convert to RGB
image = image[..., ::-1]
# Detect objects
r = model.detect([image], verbose=0)[0]
# Color splash
splash = ad_blur(image, r['masks'])
# RGB -> BGR to save image to video
splash = splash[..., ::-1]
# Add image to video writer
vwriter.write(splash)
count += 1
vwriter.release()
print("Saved to ", file_name)
if __name__ == '__main__':
import argparse
# Parse command line arguments
parser = argparse.ArgumentParser(
description='Detect AD')
parser.add_argument('--model', required=True,
metavar="/path/to/weights.h5",
help="Path to weights .h5 file")
parser.add_argument('--image_path', required=True,
metavar="/path/to/image.jpg",
help="Path to image")
parser.add_argument('--video_path', required=True,
metavar="/path/to/video.mp4",
help="Path to video")
args = parser.parse_args()
# Configurations
config = ADConfig()
# Create model in inference mode
with tf.device(DEVICE):
model = modellib.MaskRCNN(mode=TEST_MODE, model_dir=LOGS_DIR,
config=config)
model_path = args.model
# Load weights
print("Loading weights ", model_path)
model.load_weights(model_path, by_name=True)
# image and video path
path_img = args.image_path
video_path = args.video_path
# detect and blur
detect_and_ad_blur(model, path_img, video_path)