Ask Your Question

Gfile error Tensorflow in ROS

asked 2021-03-22 05:35:15 -0600

Alessandro Melino gravatar image


I am trying to create a node for image classification using ROS and Tensorflow.

If I run this script with python, everything works perfectly with some differents networks:

import pyrealsense2 as rs
import numpy as np
import cv2
import tensorflow as tf

# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(, 640, 480, rs.format.bgr8, 30)

print("[INFO] Starting streaming...")
print("[INFO] Camera ready.")

# download model from:
print("[INFO] Loading model...")
PATH_TO_CKPT = "frozen_inference_graph.pb"

# Load the Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.compat.v1.GraphDef()
    with tf.compat.v1.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph =
        tf.compat.v1.import_graph_def(od_graph_def, name='')
    sess = tf.compat.v1.Session(graph=detection_graph)

# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Output tensors are the detection boxes, scores, and classes
# Each box represents a part of the image where a particular object was detected
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represents level of confidence for each of the objects.
# The score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
# Number of objects detected
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# code source of tensorflow model loading:
print("[INFO] Model loaded.")
colors_hash = {}
while True:
    frames = pipeline.wait_for_frames()
    color_frame = frames.get_color_frame()

    # Convert images to numpy arrays
    color_image = np.asanyarray(color_frame.get_data())
    scaled_size = (color_frame.width, color_frame.height)
    # expand image dimensions to have shape: [1, None, None, 3]
    # i.e. a single-column array, where each item in the column has the pixel RGB value
    image_expanded = np.expand_dims(color_image, axis=0)
    # Perform the actual detection by running the model with the image as input
    (boxes, scores, classes, num) =[detection_boxes, detection_scores, detection_classes, num_detections],
                                                feed_dict={image_tensor: image_expanded})

    boxes = np.squeeze(boxes)
    classes = np.squeeze(classes).astype(np.int32)
    scores = np.squeeze(scores)

    for idx in range(int(num)):
        class_ = classes[idx]
        score = scores[idx]
        box = boxes[idx]

        if class_ not in colors_hash:
            colors_hash[class_] = tuple(np.random.choice(range(256), size=3))

        if score > 0.6:
            left = int(box[1] * color_frame.width)
            top = int(box[0] * color_frame.height)
            right = int(box[3] * color_frame.width)
            bottom = int(box[2] * color_frame.height)

            p1 = (left, top)
            p2 = (right, bottom)
            # draw box
            r, g, b = colors_hash[class_]

            cv2.rectangle(color_image, p1, p2, (int(r), int(g), int(b)), 2, 1)
            cv2.putText(color_image, str(class_)+" "+str(score), p1, cv2.FONT_HERSHEY_SIMPLEX, 0.75, (int(r), int(g), int(b)))

    cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
    cv2.imshow('RealSense', color_image)

print("[INFO] stop streaming ...")

In the other hand, I have created a ROS package where I launch the following script:

#!/usr/bin/env python3

#Creador: AMC 2021
#Funcion: Nodo de clasificacion de imagen con TensorFlow
#Recibe: RealSense [Image(/camera/color/image_raw)]
#Envia: Realsense [Image(/image_classified)]
import rospy
from sensor_msgs.msg ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-03-22 09:56:45 -0600

gvdhoorn gravatar image

Actually the frozen_inference_graph.pb file is in the same directory of the script, so I don't know what is different respect the python execution.

I believe this is similar/a duplicate of #q235337.

Summarising: the CWD (current working directory) when running a node with rosrun or roslaunch is not the same as when you start a script yourself.

The CWD for all nodes is $HOME/.ros. So unless there is a file called frozen_inference_graph.pb in $HOME/.ros, the error you mention is expected.

edit flag offensive delete link more


Yes, that was the problem, I must use the whole path of the file to reference it. Thank you.

Alessandro Melino gravatar image Alessandro Melino  ( 2021-03-23 02:34:42 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-03-22 05:35:15 -0600

Seen: 103 times

Last updated: Mar 22