Skip to content

Commit 40e9bed

Browse files
Add files via upload
1 parent 7c236a2 commit 40e9bed

File tree

4 files changed

+451
-0
lines changed

4 files changed

+451
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
item {
2+
id: 1
3+
name: 'c'
4+
}
5+
6+
item {
7+
id: 2
8+
name: 'ch'
9+
}
10+
item {
11+
id: 3
12+
name: 't'
13+
}
14+
15+
item {
16+
id: 4
17+
name: 'th'
18+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Welcome to the object detection tutorial !
2+
3+
# # Imports
4+
import time
5+
import cv2
6+
import mss
7+
import numpy as np
8+
import os
9+
import sys
10+
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
11+
import tensorflow as tf
12+
from distutils.version import StrictVersion
13+
from collections import defaultdict
14+
from io import StringIO
15+
16+
17+
# title of our window
18+
title = "FPS benchmark"
19+
# set start time to current time
20+
start_time = time.time()
21+
# displays the frame rate every 2 second
22+
display_time = 2
23+
# Set primarry FPS to 0
24+
fps = 0
25+
# Load mss library as sct
26+
sct = mss.mss()
27+
# Set monitor size to capture to MSS
28+
monitor = {"top": 80, "left": 0, "width": 800, "height": 640}
29+
30+
31+
32+
# ## Env setup
33+
from object_detection.utils import ops as utils_ops
34+
from object_detection.utils import label_map_util
35+
from object_detection.utils import visualization_utils as vis_util
36+
37+
38+
# # Model preparation
39+
PATH_TO_FROZEN_GRAPH = 'CSGO_frozen_inference_graph.pb'
40+
# List of the strings that is used to add correct label for each box.
41+
PATH_TO_LABELS = 'CSGO_labelmap.pbtxt'
42+
NUM_CLASSES = 4
43+
44+
45+
# ## Load a (frozen) Tensorflow model into memory.
46+
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
47+
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
48+
category_index = label_map_util.create_category_index(categories)
49+
50+
detection_graph = tf.Graph()
51+
with detection_graph.as_default():
52+
od_graph_def = tf.GraphDef()
53+
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
54+
serialized_graph = fid.read()
55+
od_graph_def.ParseFromString(serialized_graph)
56+
tf.import_graph_def(od_graph_def, name='')
57+
58+
59+
# # Detection
60+
with detection_graph.as_default():
61+
with tf.Session(graph=detection_graph) as sess:
62+
while True:
63+
# Get raw pixels from the screen, save it to a Numpy array
64+
image_np = np.array(sct.grab(monitor))
65+
# To get real color we do this:
66+
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
67+
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
68+
image_np_expanded = np.expand_dims(image_np, axis=0)
69+
# Actual detection.
70+
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
71+
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
72+
scores = detection_graph.get_tensor_by_name('detection_scores:0')
73+
classes = detection_graph.get_tensor_by_name('detection_classes:0')
74+
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
75+
# Visualization of the results of a detection.
76+
(boxes, scores, classes, num_detections) = sess.run(
77+
[boxes, scores, classes, num_detections],
78+
feed_dict={image_tensor: image_np_expanded})
79+
vis_util.visualize_boxes_and_labels_on_image_array(
80+
image_np,
81+
np.squeeze(boxes),
82+
np.squeeze(classes).astype(np.int32),
83+
np.squeeze(scores),
84+
category_index,
85+
use_normalized_coordinates=True,
86+
line_thickness=2)
87+
# Show image with detection
88+
cv2.imshow(title, cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB))
89+
# Bellow we calculate our FPS
90+
fps+=1
91+
TIME = time.time() - start_time
92+
if (TIME) >= display_time :
93+
print("FPS: ", fps / (TIME))
94+
fps = 0
95+
start_time = time.time()
96+
# Press "q" to quit
97+
if cv2.waitKey(25) & 0xFF == ord("q"):
98+
cv2.destroyAllWindows()
99+
break
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
16+
r"""Tool to export an object detection model for inference.
17+
18+
Prepares an object detection tensorflow graph for inference using model
19+
configuration and a trained checkpoint. Outputs inference
20+
graph, associated checkpoint files, a frozen inference graph and a
21+
SavedModel (https://tensorflow.github.io/serving/serving_basic.html).
22+
23+
The inference graph contains one of three input nodes depending on the user
24+
specified option.
25+
* `image_tensor`: Accepts a uint8 4-D tensor of shape [None, None, None, 3]
26+
* `encoded_image_string_tensor`: Accepts a 1-D string tensor of shape [None]
27+
containing encoded PNG or JPEG images. Image resolutions are expected to be
28+
the same if more than 1 image is provided.
29+
* `tf_example`: Accepts a 1-D string tensor of shape [None] containing
30+
serialized TFExample protos. Image resolutions are expected to be the same
31+
if more than 1 image is provided.
32+
33+
and the following output nodes returned by the model.postprocess(..):
34+
* `num_detections`: Outputs float32 tensors of the form [batch]
35+
that specifies the number of valid boxes per image in the batch.
36+
* `detection_boxes`: Outputs float32 tensors of the form
37+
[batch, num_boxes, 4] containing detected boxes.
38+
* `detection_scores`: Outputs float32 tensors of the form
39+
[batch, num_boxes] containing class scores for the detections.
40+
* `detection_classes`: Outputs float32 tensors of the form
41+
[batch, num_boxes] containing classes for the detections.
42+
* `detection_masks`: Outputs float32 tensors of the form
43+
[batch, num_boxes, mask_height, mask_width] containing predicted instance
44+
masks for each box if its present in the dictionary of postprocessed
45+
tensors returned by the model.
46+
47+
Notes:
48+
* This tool uses `use_moving_averages` from eval_config to decide which
49+
weights to freeze.
50+
51+
Example Usage:
52+
--------------
53+
python export_inference_graph \
54+
--input_type image_tensor \
55+
--pipeline_config_path path/to/ssd_inception_v2.config \
56+
--trained_checkpoint_prefix path/to/model.ckpt \
57+
--output_directory path/to/exported_model_directory
58+
59+
The expected output would be in the directory
60+
path/to/exported_model_directory (which is created if it does not exist)
61+
with contents:
62+
- inference_graph.pbtxt
63+
- model.ckpt.data-00000-of-00001
64+
- model.ckpt.info
65+
- model.ckpt.meta
66+
- frozen_inference_graph.pb
67+
+ saved_model (a directory)
68+
69+
Config overrides (see the `config_override` flag) are text protobufs
70+
(also of type pipeline_pb2.TrainEvalPipelineConfig) which are used to override
71+
certain fields in the provided pipeline_config_path. These are useful for
72+
making small changes to the inference graph that differ from the training or
73+
eval config.
74+
75+
Example Usage (in which we change the second stage post-processing score
76+
threshold to be 0.5):
77+
78+
python export_inference_graph \
79+
--input_type image_tensor \
80+
--pipeline_config_path path/to/ssd_inception_v2.config \
81+
--trained_checkpoint_prefix path/to/model.ckpt \
82+
--output_directory path/to/exported_model_directory \
83+
--config_override " \
84+
model{ \
85+
faster_rcnn { \
86+
second_stage_post_processing { \
87+
batch_non_max_suppression { \
88+
score_threshold: 0.5 \
89+
} \
90+
} \
91+
} \
92+
}"
93+
"""
94+
import tensorflow as tf
95+
from google.protobuf import text_format
96+
from object_detection import exporter
97+
from object_detection.protos import pipeline_pb2
98+
99+
slim = tf.contrib.slim
100+
flags = tf.app.flags
101+
102+
flags.DEFINE_string('input_type', 'image_tensor', 'Type of input node. Can be '
103+
'one of [`image_tensor`, `encoded_image_string_tensor`, '
104+
'`tf_example`]')
105+
flags.DEFINE_string('input_shape', None,
106+
'If input_type is `image_tensor`, this can explicitly set '
107+
'the shape of this input tensor to a fixed size. The '
108+
'dimensions are to be provided as a comma-separated list '
109+
'of integers. A value of -1 can be used for unknown '
110+
'dimensions. If not specified, for an `image_tensor, the '
111+
'default shape will be partially specified as '
112+
'`[None, None, None, 3]`.')
113+
flags.DEFINE_string('pipeline_config_path', None,
114+
'Path to a pipeline_pb2.TrainEvalPipelineConfig config '
115+
'file.')
116+
flags.DEFINE_string('trained_checkpoint_prefix', None,
117+
'Path to trained checkpoint, typically of the form '
118+
'path/to/model.ckpt')
119+
flags.DEFINE_string('output_directory', None, 'Path to write outputs.')
120+
flags.DEFINE_string('config_override', '',
121+
'pipeline_pb2.TrainEvalPipelineConfig '
122+
'text proto to override pipeline_config_path.')
123+
flags.DEFINE_boolean('write_inference_graph', False,
124+
'If true, writes inference graph to disk.')
125+
tf.app.flags.mark_flag_as_required('pipeline_config_path')
126+
tf.app.flags.mark_flag_as_required('trained_checkpoint_prefix')
127+
tf.app.flags.mark_flag_as_required('output_directory')
128+
FLAGS = flags.FLAGS
129+
130+
131+
def main(_):
132+
pipeline_config = pipeline_pb2.TrainEvalPipelineConfig()
133+
with tf.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f:
134+
text_format.Merge(f.read(), pipeline_config)
135+
text_format.Merge(FLAGS.config_override, pipeline_config)
136+
if FLAGS.input_shape:
137+
input_shape = [
138+
int(dim) if dim != '-1' else None
139+
for dim in FLAGS.input_shape.split(',')
140+
]
141+
else:
142+
input_shape = None
143+
exporter.export_inference_graph(
144+
FLAGS.input_type, pipeline_config, FLAGS.trained_checkpoint_prefix,
145+
FLAGS.output_directory, input_shape=input_shape,
146+
write_inference_graph=FLAGS.write_inference_graph)
147+
148+
149+
if __name__ == '__main__':
150+
tf.app.run()

0 commit comments

Comments
 (0)