def create_pbtxt_compatible_with_opencv(pb_path, pbtxt_path):
    import tensorflow as tf

    # Read the graph.
    with tf.gfile.FastGFile(pb_path, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # Remove Const nodes.
    for i in reversed(range(len(graph_def.node))):
        if graph_def.node[i].op == 'Const':
            del graph_def.node[i]
        for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
                     'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
                     'Tpaddings']:
            if attr in graph_def.node[i].attr:
                del graph_def.node[i].attr[attr]

    # Save as text.
    tf.train.write_graph(graph_def, "", pbtxt_path, as_text=True)

def create_pbtxt_compatible_with_opencv(pb_path, pbtxt_path):
    import tensorflow as tf

    # Read the graph.
    with tf.gfile.FastGFile(pb_path, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # Remove Const nodes.
    for i in reversed(range(len(graph_def.node))):
        if graph_def.node[i].op == 'Const':
            del graph_def.node[i]
        for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
                     'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
                     'Tpaddings']:
            if attr in graph_def.node[i].attr:
                del graph_def.node[i].attr[attr]

    # Save as text.
    tf.train.write_graph(graph_def, "", pbtxt_path, as_text=True)

# Read the graph. # Remove Const nodes. # Save as text.