Click here to Skip to main content
14,870,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm following this link, https://colab.research.google.com/drive/11ko0DBnI1QLxVoJQR8gt9b4JDcvbCrtU#scrollTo=A_tyvKnBP6qD to build my object detector. I am using Google collab. My workspace structure is exactly as followed in this link. Everything was going fine until this block of code:

Python
from object_detection.utils import dataset_util
%cd /content/drive/MyDrive/Gun_Detection/models

data_base_url = '/content/drive/MyDrive/Gun_Detection/data'
image_dir = data_base_url + 'images/'

def class_text_to_int(row_label):
        if row_label == 'pistol':
                return 1
        else:
                None


def split(df, group):
        data = namedtuple('data', ['filename', 'object']) #we wanna group by
        gb = df.groupby(group) #split data into group data by splitting, applying n combining
        return [data(filename, gb.get_group(x)) 
  for filename, x in zip(gb.groups.keys(), gb.groups)] #add group keys to index to identify pieces.

def create_tf_example(group, path):
        with tf.io.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
                encoded_jpg = fid.read()
        encoded_jpg_io = io.BytesIO(encoded_jpg)
        image = Image.open(encoded_jpg_io)
        width, height = image.size

        filename = group.filename.encode('utf8')
        image_format = b'jpg'
        xmins = []
        xmaxs = []
        ymins = []
        ymaxs = []
        classes_text = []
        classes = []

        for index, row in group.object.iterrows():
                xmins.append(row['xmin'] / width)
                xmaxs.append(row['xmax'] / width)
                ymins.append(row['ymin'] / height)
                ymaxs.append(row['ymax'] / height)
                classes_text.append(row['class'].encode('utf8'))
                classes.append(class_text_to_int(row['class']))

        tf_example = tf.train.Example(features=tf.train.Features(feature={
                'image/height': dataset_util.int64_feature(height),
                'image/width': dataset_util.int64_feature(width),
                'image/filename': dataset_util.bytes_feature(filename),
                'image/source_id': dataset_util.bytes_feature(filename),
                'image/encoded': dataset_util.bytes_feature(encoded_jpg),
                'image/format': dataset_util.bytes_feature(image_format),
                'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
                'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
                'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
                'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
                'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
                'image/object/class/label': dataset_util.int64_list_feature(classes),
        }))
        return tf_example

for csv in ['train_labels', 'test_labels']:
  writer = tf.io.TFRecordWriter(data_base_url + csv + '.record')
  path = os.path.join(image_dir)
  examples = pd.read_csv(data_base_url + csv + '.csv')
  grouped = split(examples, 'filename')
  for group in grouped:
      tf_example = create_tf_example(group, path)
      writer.write(tf_example.SerializeToString())
    
  writer.close()
  output_path = os.path.join(os.getcwd(), data_base_url + csv + '.record')
  print('Successfully created the TFRecords: {}'.format(data_base_url + csv + '.record'))


After this error, datatrain_label.record got generated in my Gun Detection folder in my drive. I am confused 😕 I can't proceed further. Please help!

N.B: I am not pro with python and am still learning. Trying hard to understand the code but I honestly don't.

This is the error message I'm getting:
Python
<pre>/content/drive/MyDrive/Gun_Detection/models

---------------------------------------------------------------------------

FileNotFoundError                         Traceback (most recent call last)

<ipython-input-33-b566ecc28dcc> in <module>()
     61   writer = tf.io.TFRecordWriter(data_base_url + csv + '.record')
     62   path = os.path.join(image_dir)
---> 63   examples = pd.read_csv(data_base_url + csv + '.csv')
     64   grouped = split(examples, 'filename')
     65   for group in grouped:

4 frames

/usr/local/lib/python3.7/dist-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
   2008         kwds["usecols"] = self.usecols
   2009 
-> 2010         self._reader = parsers.TextReader(src, **kwds)
   2011         self.unnamed_cols = self._reader.unnamed_cols
   2012 

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Gun_Detection/datatrain_labels.csv'


What I have tried:

I have googled alot on this error but all in vain.
Posted
Updated 30-Mar-21 19:23pm
v4
Comments
Richard MacCutchan 30-Mar-21 9:21am
   
Which line of the program produces the error? It is clearly telling you that a file or directory you are trying to access to does not exist.
maverick maiden 31-Mar-21 1:28am
   
I have updated the error message in this question. Thank you for replying. Upon running this particular portion of code, it produces this attached error message. It says this: FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Gun_Detection/datatrain_labels.csv'.
I don't know how, but after running this code, it is automatically generated a file named "datatrain_labels.csv" in my "/content/drive/MyDrive/Gun_Detection" folder. and it is there but the error says it couldn't find this particular file. Thats irony!
Richard MacCutchan 31-Mar-21 3:27am
   
Look at the path in the error message. Do you have a root directory named content on your system?
maverick maiden 1-Apr-21 2:51am
   
Yes @Richard MacCutchan. I have the copied the google drive path and pasted. There is Content. Thanks!
Richard MacCutchan 1-Apr-21 3:07am
   
Well that does not alter the fact that some part of that path is not valid. But the only person who can check it is you, as we have no way of getting access to your system.
maverick maiden 1-Apr-21 3:31am
   
That is exactly what I am not able to figure out.
Richard MacCutchan 1-Apr-21 3:33am
   
Look in the directory, list the file to see that the path is valid, check that you have read and write permissions to it, and also to all parts of its path. There are plenty of tools in Linux that will verify the details.
Gerry Schmitz 30-Mar-21 14:19pm
   
As Richard pointed out, when you "copy" sample code, you often have to adjust file drive letters, etc. to match your (default) software project setup.
maverick maiden 1-Apr-21 2:53am
   
I have carefully read and changed the directories path and file paths, according to that link. Maybe, I've missed something here but I can't figure out where I have gone wrong. Thanks for your time @Gerry Schmitz

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900