ValueError: could not convert string to float: 'registration'

593 views Asked by At

I am doing vehicle registration plate detection using YOLOv4 in colab. When I ran !python \convert_annotations.py file I got following error

Currently in subdirectory: validation
Converting annotations for class:  Vehicle registration plate
  0% 0/30 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "convert_annotations.py", line 63, in <module>
    coords = np.asarray([float(labels[1]), float(labels[2]), float(labels[3]), float(labels[4])])
ValueError: could not convert string to float: 'registration'

convert_annotations.py file is as follows.

import os
import cv2
import numpy as np
from tqdm import tqdm
import argparse
import fileinput

# function that turns XMin, YMin, XMax, YMax coordinates to normalized yolo format
def convert(filename_str, coords):
    os.chdir("..")
    image = cv2.imread(filename_str + ".jpg")
    coords[2] -= coords[0]
    coords[3] -= coords[1]
    x_diff = int(coords[2]/2)
    y_diff = int(coords[3]/2)
    coords[0] = coords[0]+x_diff
    coords[1] = coords[1]+y_diff
    coords[0] /= int(image.shape[1])
    coords[1] /= int(image.shape[0])
    coords[2] /= int(image.shape[1])
    coords[3] /= int(image.shape[0])
    os.chdir("Label")
    return coords

ROOT_DIR = os.getcwd()

# create dict to map class names to numbers for yolo
classes = {}
with open("classes.txt", "r") as myFile:
    for num, line in enumerate(myFile, 0):
        line = line.rstrip("\n")
        classes[line] = num
    myFile.close()
# step into dataset directory
os.chdir(os.path.join("OID", "Dataset"))
DIRS = os.listdir(os.getcwd())

# for all train, validation and test folders
for DIR in DIRS:
    if os.path.isdir(DIR):
        os.chdir(DIR)
        print("Currently in subdirectory:", DIR)
        
        CLASS_DIRS = os.listdir(os.getcwd())
        # for all class folders step into directory to change annotations
        for CLASS_DIR in CLASS_DIRS:
            if os.path.isdir(CLASS_DIR):
                os.chdir(CLASS_DIR)
                print("Converting annotations for class: ", CLASS_DIR)
                
                # Step into Label folder where annotations are generated
                os.chdir("Label")

                for filename in tqdm(os.listdir(os.getcwd())):
                    filename_str = str.split(filename, ".")[0]
                    if filename.endswith(".txt"):
                        annotations = []
                        with open(filename) as f:
                            for line in f:
                                for class_type in classes:
                                    line = line.replace(class_type, str(classes.get(class_type)))
                                labels = line.split()
                                coords = np.asarray([float(labels[1]), float(labels[2]), float(labels[3]), float(labels[4])])
                                coords = convert(filename_str, coords)
                                labels[1], labels[2], labels[3], labels[4] = coords[0], coords[1], coords[2], coords[3]
                                newline = str(labels[0]) + " " + str(labels[1]) + " " + str(labels[2]) + " " + str(labels[3]) + " " + str(labels[4])
                                line = line.replace(line, newline)
                                annotations.append(line)
                            f.close()
                        os.chdir("..")
                        with open(filename, "w") as outfile:
                            for line in annotations:
                                outfile.write(line)
                                outfile.write("\n")
                            outfile.close()
                        os.chdir("Label")
                os.chdir("..")
                os.chdir("..")
        os.chdir("..")

I am doing this by seeing youtube videos so in video he didn't get error but I am getting error. What could be the problem, please help?

2

There are 2 answers

0
FeX On

Brian, the solution to your problem :

With your terminal, go to your directory, for example :

yolov4/OIDv4_ToolKit

To get 15 images for the training data:

python3 main.py downloader --classes Vehicle_registration_plate --type_csv train --limit 15

To get 3 images for the validation:

python3 main.py downloader --classes Vehicle_registration_plate --type_csv validation --limit 3

(If you want, you can set higher numbers, like 1500 images for the training data and 300 for the validation data)

Within the root OIDv4_ToolKit folder open the file classes.txt and edit it to have the classes you just downloaded and type exactly:

Vehicle registration plate

Finally, the command to convert all labels to YOLOv4 format:

python3 convert_annotations.py

Normally, this solution works like a charm.

1
Zahra Hajalioghli On

remove apostrophes in 'Vehicle registration plate' and run it. if the issue is not solved, then try running pip install -r requirements.txt before running this command