AttributeError: 'function' object has no attribute 'GFile'

sonx1354: <p>Traceback (most recent call last):<br>
File “/home/pi/tensorflow1/models/research/object_detection/Object_detection_webcam.py”, line 57, in <br>
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)<br>
File “/home/pi/tensorflow1/models/research/object_detection/utils/label_map_util.py”, line 137, in load_labelmap<br>
with tf.gfile.GFile(path, ‘r’) as fid:</p>
<p>Code:</p>
<p>######## Webcam Object Detection Using Tensorflow-trained Classifier #########</p>
<h1></h1>
<h1>Author: Evan Juras</h1>
<h1>Date: 1/20/18</h1>
<h1>Description:</h1>
<h1>This program uses a TensorFlow-trained classifier to perform object detection.</h1>
<h1>It loads the classifier uses it to perform object detection on a webcam feed.</h1>
<h1>It draws boxes and scores around the objects of interest in each frame from</h1>
<h1>the webcam.</h1>
<h2>Some of the code is copied from Google’s example at</h2>
<h2><a href="https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb" rel="nofollow noopener">https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb</a></h2>
<h2>and some is copied from Dat Tran’s example at</h2>
<h2><a href="https://github.com/datitran/object_detector_app/blob/master/object_detection_app.py" rel="nofollow noopener">https://github.com/datitran/object_detector_app/blob/master/object_detection_app.py</a></h2>
<h2>but I changed it to make it more understandable to me.</h2>
<h1>Import packages</h1>
<p>import cv2<br>
import numpy as np<br>
import tensorflow as tf<br>
import sys<br>
import os</p>
<h1>This is needed since the notebook is stored in the object_detection folder.</h1>
<p>sys.path.append("…")</p>
<h1>Import utilites</h1>
<p>from utils import label_map_util<br>
from utils import visualization_utils as vis_util</p>
<h1>Name of the directory containing the object detection module we’re using</h1>
<p>MODEL_NAME = ‘inference_graph’</p>
<h1>Grab path to current working directory</h1>
<p>CWD_PATH = os.getcwd()</p>
<h1>Path to frozen detection graph .pb file, which contains the model that is used</h1>
<h1>for object detection.</h1>
<p>PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,‘frozen_inference_graph.pb’)</p>
<h1>Path to label map file</h1>
<p>PATH_TO_LABELS = os.path.join(CWD_PATH,‘training’,‘labelmap.pbtxt’)</p>
<h1>Number of classes the object detector can identify</h1>
<p>NUM_CLASSES = 6</p>
<h2>Load the label map.</h2>
<h1>Label maps map indices to category names, so that when our convolution</h1>
<h1>network predicts <code>5</code>, we know that this corresponds to <code>king</code>.</h1>
<h1>Here we use internal utility functions, but anything that returns a</h1>
<h1>dictionary mapping integers to appropriate string labels would be fine</h1>
<p>label_map = label_map_util.load_labelmap(PATH_TO_LABELS)<br>
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)<br>
category_index = label_map_util.create_category_index(categories)</p>
<h1>Load the Tensorflow model into memory.</h1>
<p>detection_graph = tf.Graph()<br>
with detection_graph.as_default():<br>
od_graph_def = tf.GraphDef()<br>
with tf.gfile.GFile(PATH_TO_CKPT, ‘rb’) as fid:<br>
serialized_graph = fid.read()<br>
od_graph_def.ParseFromString(serialized_graph)<br>
tf.import_graph_def(od_graph_def, name=’’)</p>
<pre><code>sess = tf.Session(graph=detection_graph)
</code></pre>
<h1>Define input and output tensors (i.e. data) for the object detection classifier</h1>
<h1>Input tensor is the image</h1>
<p>image_tensor = detection_graph.get_tensor_by_name(‘image_tensor:0’)</p>
<h1>Output tensors are the detection boxes, scores, and classes</h1>
<h1>Each box represents a part of the image where a particular object was detected</h1>
<p>detection_boxes = detection_graph.get_tensor_by_name(‘detection_boxes:0’)</p>
<h1>Each score represents level of confidence for each of the objects.</h1>
<h1>The score is shown on the result image, together with the class label.</h1>
<p>detection_scores = detection_graph.get_tensor_by_name(‘detection_scores:0’)<br>
detection_classes = detection_graph.get_tensor_by_name(‘detection_classes:0’)</p>
<h1>Number of objects detected</h1>
<p>num_detections = detection_graph.get_tensor_by_name(‘num_detections:0’)</p>
<h1>Initialize webcam feed</h1>
<p>video = cv2.VideoCapture(0)</p>
<p>while(True):</p>
<pre><code># Acquire frame and expand frame 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
ret, frame = video.read()
frame_expanded = np.expand_dims(frame, axis=0)

# Perform the actual detection by running the model with the image as input
(boxes, scores, classes, num) = sess.run(
    [detection_boxes, detection_scores, detection_classes, num_detections],
    feed_dict={image_tensor: frame_expanded})

objects = []
threshold = 0.89
for index, value in enumerate(classes[0]):
    object_dict = {}
    if scores[0,index] &gt; threshold:
        object_dict[category_index.get(value).get('name')] = scores[0,index]
        avbn = (category_index.get(value).get('name'))
        print(avbn)


# Draw the results of the detection (aka 'visulaize the results')
vis_util.visualize_boxes_and_labels_on_image_array(
    frame,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=8,
    min_score_thresh=0.85)

# All the results have been drawn on the frame, so it's time to display it.
cv2.imshow('Object detector', frame)


# Press 'q' to quit
if cv2.waitKey(1) == ord('q'):
    break
</code></pre>
<h1>Clean up</h1>
<p>video.release()<br>
cv2.destroyAllWindows()</p>

ptrblck: <p>Your issue seems to be related to TensorFlow, so I doubt many users can help you in this PyTorch discussion board. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"><br>
I would recommend to ask a question on StackOverflow.</p>