Exemplo n.º 1
0
    def __init__(self):
        classify_image.maybe_download_and_extract()  # what does it do?
        self._session = tf.Session()
        classify_image.create_graph()
        self._cv_bridge = CvBridge()  # what does this do?

        self._sub = rospy.Subscriber('image', Image, self.callback, queue_size=1)  # callback is the argument if we callback or # NOTE:
        self._pub = rospy.Publisher('result', String, queue_size=1)
        self.score_threshold = rospy.get_param('~score_threshold', 0.1)  # ~ means private name
        self.use_top_k = rospy.get_param('~use_top_k', 5)
    def __init__(self):
        classify_image.maybe_download_and_extract()
        self._session = tf.Session()
        classify_image.create_graph()
        self._cv_bridge = CvBridge()

        self._sub = rospy.Subscriber('image', Image, self.callback, queue_size=1)
        self._pub = rospy.Publisher('result', String, queue_size=1)
        self.score_threshold = rospy.get_param('~score_threshold', 0.1)
        self.use_top_k = rospy.get_param('~use_top_k', 5)
Exemplo n.º 3
0
    def __init__(self):
        classify_image.maybe_download_and_extract()
        self._session = tf.Session()
        classify_image.create_graph()
        self._cv_bridge = CvBridge()

        self._sub = rospy.Subscriber('image',
                                     Image,
                                     self.callback,
                                     queue_size=1)
        self._pub = rospy.Publisher('result', String, queue_size=1)
        self.score_threshold = rospy.get_param('~score_threshold', 0.1)
        self.use_top_k = rospy.get_param('~use_top_k', 5)
Exemplo n.º 4
0
    def __init__(self):
        #构造函数有个API可从tensorflow.org下载训练好的inception-v3
        classify_image.maybe_download_and_extract()
        #创建一个session对象
        self._session = tf.Session()
        classify_image.create_graph()
        #为opencv图像转换创建一个cv_bridge
        self._cv_bridge = CvBridge()

        #节点的发布者和订阅着
        self._sub = rospy.Subscriber('image',
                                     Image,
                                     self.callback,
                                     queue_size=1)
        self._pub = rospy.Publisher('result', String, queue_size=1)
        self.score_threshold = rospy.get_param('~score_threshold', 0.1)
        self.use_top_k = rospy.get_param('~use_top_k', 5)
Exemplo n.º 5
0
def main(argv=None):
    classify_image.maybe_download_and_extract()
    classify_image.create_graph()
    basedir = os.path.dirname(__file__)
    with tf.Session() as sess:
        data = np.loadtxt('embedding.tsv')
        embedding_var = tf.Variable(data, name='embedding_var')
        # prepare projector config
        config = projector.ProjectorConfig()
        embedding = config.embeddings.add()
        embedding.tensor_name = embedding_var.name
        summary_writer = tf.train.SummaryWriter(os.path.join(
            basedir, 'logdir'))
        # link metadata
        #metadata_path = os.path.join(basedir, 'logdir', 'metadata.tsv')
        #with open(metadata_path, 'w') as f:
        #    for name in files:
        #        f.write('%s\n' % name)
        #embedding.metadata_path = metadata_path

        # write to sprite image file
        image_path = os.path.join(basedir, 'logdir', 'sprite.jpg')
        #size = int(math.sqrt(len(images))) + 1
        #while len(images) < size * size:
        #    images.append(np.zeros((100, 100, 3), dtype=np.uint8))
        #rows = []
        #for i in range(size):
        #    rows.append(tf.concat(1, images[i*size:(i+1)*size]))
        #jpeg = tf.image.encode_jpeg(tf.concat(0, rows))
        #with open(image_path, 'wb') as f:
        #    f.write(sess.run(jpeg))
        embedding.sprite.image_path = image_path
        embedding.sprite.single_image_dim.extend([64, 64])
        # save embedding_var
        projector.visualize_embeddings(summary_writer, config)
        sess.run(tf.variables_initializer([embedding_var]))
        saver = tf.train.Saver([embedding_var])
        saver.save(sess, os.path.join(basedir, 'logdir', 'model.ckpt'))
def run_top_k_predictions_on_image(image):

  if not tf.gfile.Exists(image):
    tf.logging.fatal('File does not exist %s', image)
  image_data = tf.gfile.FastGFile(image, 'rb').read()

  # Download the incepion-v3 model
  ic.maybe_download_and_extract()

  # Creates graph from saved GraphDef.
  ic.create_graph()

  with tf.Session() as sess:
    # Convert the PNG image to a height x width x 3 (channels) Numpy array, 
    # for example using PIL, then feed the 'DecodeJpeg:0' tensor:
    image_content = Image.open(image)
    image_array = np.array(image_content)[:, :, 0:3]  # Select RGB channels only.

    # Some useful tensors:
    # 'softmax:0': A tensor containing the normalized prediction across
    #   1000 labels.
    # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
    #   float description of the image.
    # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
    #   encoding of the image.
    # Runs the softmax tensor by feeding the image_data as input to the graph.
    softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
    predictions = sess.run(softmax_tensor, {'DecodeJpeg:0': image_array})
    predictions = np.squeeze(predictions)

    # Creates node ID --> English string lookup.
    node_lookup = ic.NodeLookup()
    top_k = predictions.argsort()[-ic.FLAGS.num_top_predictions:][::-1]
    for node_id in top_k:
      human_string = node_lookup.id_to_string(node_id)
      score = predictions[node_id]
      print('%s (score = %.5f)' % (human_string, score))
Exemplo n.º 7
0
    def __init__(self,
                 model=None,
                 type_='segmentation',
                 img_shape=None,
                 input_tensor=None):
        classify_image.maybe_download_and_extract()
        self._session = tf.Session()
        self.tensor_2_run = model
        init = tf.initialize_all_variables()
        self._session.run(init)
        #classify_image.create_graph()
        self._cv_bridge = CvBridge()
        self.input_tensor = input_tensor
        self.img_shape = img_shape
        self.type_ = type_

        self._sub = rospy.Subscriber('image',
                                     Image,
                                     self.callback,
                                     queue_size=1)
        print('found camera')
        self._pub = rospy.Publisher('result', String, queue_size=1)
        print('setup publishing')
        self.count = 0
Exemplo n.º 8
0
        print("len : " + str(len(recv_data)) + "   total_len : " + str(length))
        if len(recv_data) == length:
            break
    print("end while")

    arr = np.array(recv_data)
    bytearr = arr.tobytes()

    print("recv_data length : " + str(len(recv_data)))
    image = Image.open(io.BytesIO(bytearr))
    image.save('python.jpg')


#server code

classify_image.maybe_download_and_extract()
session = tf.Session()
classify_image.create_graph()

host = "192.168.0.2"
port = 12345
buffer_size = 1024
score_threshold = 0.1
use_top_k = 5

s = socket.socket()

try:
    s.bind((host, port))

    s.listen(5)
def main(argv=None):
    classify_image.maybe_download_and_extract()
    classify_image.create_graph()
    basedir = os.path.dirname(__file__)
    with tf.Session() as sess:
        pool3 = sess.graph.get_tensor_by_name('pool_3:0')
        jpeg_data = tf.placeholder(tf.string)
        thumbnail = tf.cast(
            tf.image.resize_images(tf.image.decode_jpeg(jpeg_data),
                                   [100, 100]), tf.uint8)
        outputs = []
        files = []
        images = []
        for filename in os.listdir('images'):
            if not filename.endswith('.JPG'):
                continue
            print('process %s...' % filename)
            files.append(filename)
            with open(os.path.join(basedir, 'images', filename), 'rb') as f:
                data = f.read()
                results = sess.run([pool3, thumbnail], {
                    'DecodeJpeg/contents:0': data,
                    jpeg_data: data
                })
                outputs.append(results[0])
                images.append(results[1])

        embedding_var = tf.Variable(tf.pack([tf.squeeze(x) for x in outputs],
                                            axis=0),
                                    trainable=False,
                                    name='pool3')
        # prepare projector config
        config = projector.ProjectorConfig()
        embedding = config.embeddings.add()
        embedding.tensor_name = embedding_var.name
        summary_writer = tf.train.SummaryWriter(os.path.join(
            basedir, 'logdir'))
        # link metadata
        metadata_path = os.path.join(basedir, 'logdir', 'metadata.tsv')
        with open(metadata_path, 'w') as f:
            for name in files:
                f.write('%s\n' % name)
        embedding.metadata_path = metadata_path
        # write to sprite image file
        image_path = os.path.join(basedir, 'logdir', 'sprite.jpg')
        size = int(math.sqrt(len(images))) + 1
        while len(images) < size * size:
            images.append(np.zeros((100, 100, 3), dtype=np.uint8))
        rows = []
        for i in range(size):
            rows.append(tf.concat(1, images[i * size:(i + 1) * size]))
        jpeg = tf.image.encode_jpeg(tf.concat(0, rows))
        with open(image_path, 'wb') as f:
            f.write(sess.run(jpeg))
        embedding.sprite.image_path = image_path
        embedding.sprite.single_image_dim.extend([100, 100])
        # save embedding_var
        projector.visualize_embeddings(summary_writer, config)
        sess.run(tf.variables_initializer([embedding_var]))
        saver = tf.train.Saver([embedding_var])
        saver.save(sess, os.path.join(basedir, 'logdir', 'model.ckpt'))
 def _load_graph(self):
     classify_image.maybe_download_and_extract()
     classify_image.create_graph()