def image_recognition(cv_image): #cv2.imshow('image',cv_image) # cv2.waitKey(1000) image_data = cv2.imencode('.jpg', cv_image)[1].tostring() # Creates graph from saved GraphDef. softmax_tensor = session.graph.get_tensor_by_name('softmax:0') predictions = 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()[use_top_k:][::-1] strings = [] scores = [] result_num = 0 for node_id in top_k: human_string = node_lookup.id_to_string(node_id) score = predictions[node_id] if score > score_threshold: strings.append(human_string) scores.append(float(score)) result_num += 1 return strings, scores, result_num
def callback(self, image_msg): cv_image = self._cv_bridge.imgmsg_to_cv2(image_msg, "bgr8") # copy from # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/imagenet/classify_image.py 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') # Creates node ID --> English string lookup. node_lookup = classify_image.NodeLookup() top_k = predictions.argsort( )[-self.use_top_number_of_predictions:][::-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)
def callback(self, image_msg): # convert image from ROS image msg type to opencv format cv_image = self._cv_bridge.imgmsg_to_cv2(image_msg, "bgr8") image_data = cv2.imencode('.jpg', cv_image)[1].tostring() 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) # create nodeID -- english string lookup node_lookup = classify_image.NodeLookup() top_k = predictions.argsort()[-self.use_top_k:][::-1] # get top k 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)
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))
def _classify_cb(self, image): try: cv_img = self._BRIDGE.imgmsg_to_cv2(image) except CvBridgeError as e: rospy.logerr("cv_bridge exception: {}".format(e)) img_data = cv2.imencode('.jpg', cv_img)[1].tostring() softmax = self._sess.graph.get_tensor_by_name('softmax:0') pred = self._sess.run(softmax, {'DecodeJpeg/contents:0': img_data}) pred = np.squeeze(pred) lookup = classify_image.NodeLookup() top_k = pred.argsort()[-self._k:][::-1] ss = [] for id in top_k: ret_str = lookup.id_to_string(id) score = pred[id] if score > self._score_threshold: ss.append("[{:0.4f} - {}]\n".format(score, ret_str)) self._string_pub.publish(','.join(ss)) self._show_image(cv_img, ss[0][:-1])