Skip to content

Instantly share code, notes, and snippets.

@plusangel
Created December 3, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plusangel/661ff6bf145d112646ea60d62fff8e41 to your computer and use it in GitHub Desktop.
Save plusangel/661ff6bf145d112646ea60d62fff8e41 to your computer and use it in GitHub Desktop.
ROS camera streaming to TensorFlow
def callback(self, image_msg):
# convert a ROS image message into an cv::Mat using module cv_bridge
cv_image = self._cv_bridge.imgmsg_to_cv2(image_msg, "bgr8")
# copy from
# classify_image.py
# convert (encode) the image format into streaming data and assign it to
# memory cache. It is mainly used for compressing image data format to facilitate
# network transmission
image_data = cv2.imencode('.jpg', cv_image)[1].tostring()
# Creates graph from saved GraphDef.
softmax_tensor = self._session.graph.get_tensor_by_name('softmax:0')
predictions = self._session.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
# Creates node ID --> English string lookup.
node_lookup = classify_image.NodeLookup()
top_k = predictions.argsort()[-self.use_top_k:][::-1]
for node_id in top_k:
human_string = node_lookup.id_to_string(node_id)
score = predictions[node_id]
if score > self.score_threshold:
rospy.loginfo('%s (score = %.5f)' % (human_string, score))
self._pub.publish(human_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment