def gen_sim_matrix(x_files, y_files): # Get a combined list of the skeleton files chosen earlier combined_files = x_files + y_files # Initializing the similarity matrix to be used for final comparison s_matrix = np.zeros(len(combined_files)**2).reshape( len(combined_files), len(combined_files)) # Populating the similarity matrix with distance outputs from DTW for i, file_i in enumerate(combined_files): sk_i = Skeleton(file_i) sk_i.load() sk_i.normalize('spine-base') for j, file_j in enumerate(combined_files): sk_j = Skeleton(file_j) sk_j.load() sk_j.normalize('spine-base') dist, cost, acc, path = dtw( sk_i.data, sk_j.data, dist=lambda a, b: np.linalg.norm(a - b)) s_matrix[i, j] = dist return s_matrix
def main(): service_name = 'GestureRecognizer.Recognition' log = Logger(name=service_name) op = load_options() channel = Channel(op.broker_uri) log.info('Connected to broker {}', op.broker_uri) exporter = create_exporter(service_name=service_name, uri=op.zipkin_uri) subscription = Subscription(channel=channel, name=service_name) for group_id in list(op.group_ids): subscription.subscribe( 'SkeletonsGrouper.{}.Localization'.format(group_id)) model = GestureRecognizer("model_gesture1_72.00.pth") log.info('Initialize the model') unc = Gauge('uncertainty_total', "Uncertainty about predict") unc.set(0.0) start_http_server(8000) buffer = list() predict_flag = False mean = lambda x: (sum(x) / len(x)) while True: msg = channel.consume() tracer = Tracer(exporter, span_context=msg.extract_tracing()) span = tracer.start_span(name='detection_and_info') annotations = msg.unpack(ObjectAnnotations) skeleton = select_skeletons(annotations=annotations, min_keypoints=op.skeletons.min_keypoints, x_range=op.skeletons.x_range, y_range=op.skeletons.y_range) if skeleton is None: tracer.end_span() continue skl = Skeleton(skeleton) skl_normalized = skl.normalize() pred, prob, uncertainty = model.predict(skl_normalized) if pred == 0 and predict_flag is False: pass elif pred != 0 and predict_flag is False: initial_time = time.time() predict_flag = True buffer.append(uncertainty) elif pred != 0 and predict_flag is True: buffer.append(uncertainty) elif pred == 0 and predict_flag is True: predict_flag = False exec_time = time.time() - initial_time if exec_time >= op.exec_time: unc.set(mean(buffer)) log.info("execution_ms: {}, buffer_mean: {}", (exec_time * 1000), mean(buffer)) buffer = [] tracer.end_span() info = { 'prediction': pred, 'probability': prob, 'uncertainty': uncertainty, 'took_ms': { 'service': round(span_duration_ms(span), 2) } } log.info('{}', str(info).replace("'", '"'))