Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request - expose prometheus metrics #16

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'numpy',
'pillow',
'opencv-python',
'prometheus_client',
'scipy',
'scikit-image',
'tensorflow'
Expand Down
4 changes: 2 additions & 2 deletions umt/deep_sort/generate_detections.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def __init__(self, checkpoint_filename, input_name="images",
graph_def.ParseFromString(file_handle.read())
tf.import_graph_def(graph_def, name="net")
self.input_var = tf.get_default_graph().get_tensor_by_name(
"%s:0" % input_name)
"net/%s:0" % input_name)
self.output_var = tf.get_default_graph().get_tensor_by_name(
"%s:0" % output_name)
"net/%s:0" % output_name)

assert len(self.output_var.get_shape()) == 2
assert len(self.input_var.get_shape()) == 4
Expand Down
42 changes: 38 additions & 4 deletions umt/umt_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from umt.umt_utils import initialize_img_source
from umt.umt_utils import generate_detections

from prometheus_client import start_http_server, Summary, Counter, Gauge

#--- CONSTANTS ----------------------------------------------------------------+

LABEL_PATH = "models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29/labelmap.txt"
Expand All @@ -44,6 +46,10 @@ def main():
parser.add_argument('-nframes', dest='nframes', type=int, required=False, default=10, help='specify nunber of frames to process')
parser.add_argument('-display', dest='live_view', required=False, default=False, action='store_true', help='add this flag to view a live display. note, that this will greatly slow down the fps rate.')
parser.add_argument('-save', dest='save_frames', required=False, default=False, action='store_true', help='add this flag if you want to persist the image output. note, that this will greatly slow down the fps rate.')
parser.add_argument('-metrics', dest='metrics', required=False, default=False, action='store_true', help='add this flag to enable prometheus metrics')
parser.add_argument('-metricport', dest='metric_port', type=int, required=False, default=8000, help='specify the prometheus metrics port (default 8000)')
parser.add_argument('-initlabelcounters', dest='init_object_counters', required=False, default=False, action='store_true', help='add this flag to return 0 for all possible object counter metrics available in the model')
parser.add_argument('-nolog', dest='nolog', required=False, default=False, action='store_true', help='add this flag to disable logging to object_paths.csv. note, file is still created, just not written to.')
args = parser.parse_args()

# basic checks
Expand All @@ -61,6 +67,25 @@ def main():
# initialize detector
interpreter = initialize_detector(args)

if args.metrics:
# initialize counters for metrics
frames = Counter('umt_frame_counter', 'Number of frames processed', ['result'])
frames.labels(result='no_detection')
frames.labels(result='detection')
frames.labels(result='error')

object_counter = Counter ('umt_object_counter', 'Number of each object counted', ['type'])
if args.init_object_counters:
for label in labels.values():
object_counter.labels(type=label)

track_count_hwm = 0 # Track id high water mark
track_count = Gauge('umt_tracked_objects', 'Number of objects that have been tracked')

print(' > METRIC PORT',args.metric_port)
print(' > STARTING METRIC SERVER')
start_http_server(args.metric_port)

# create output directory
if not os.path.exists('output') and args.save_frames: os.makedirs('output')

Expand Down Expand Up @@ -93,9 +118,13 @@ def main():
detections = generate_detections(pil_img, interpreter, args.threshold)

# proceed to updating state
if len(detections) == 0: print(' > no detections...')
if len(detections) == 0:
print(' > no detections...')
if args.metrics: frames.labels(result='no_detection').inc()
else:

# update metric
if args.metrics: frames.labels(result='detection').inc()

# update tracker
tracker.predict()
tracker.update(detections)
Expand All @@ -110,7 +139,12 @@ def main():
f'{int(track.time_since_update)},{str(track.hits)},'
f'{int(bbox[0])},{int(bbox[1])},'
f'{int(bbox[2])},{int(bbox[3])}')
print(row, file=out_file)
if not args.nolog: print(row, file=out_file)
if args.metrics: # update the metrics
if track.track_id > track_count_hwm: # new thing being tracked
track_count_hwm = track.track_id
track_count.set(track.track_id)
object_counter.labels(type=class_name).inc()

# only for live display
if args.live_view or args.save_frames:
Expand Down Expand Up @@ -147,5 +181,5 @@ def main():

if __name__ == '__main__':
main()

#--- END ----------------------------------------------------------------------+