'How to convert road lane coordinates to tensors in Python?

I am trying to develop a Lane Detector using PyTorch. Basically, I'm reading the video frame by frame using cv2, then finding edges using Canny Edge Detector and then I'm using average lane algorithm to get lane's coordinates.

These coordinates look like [[x1, y1, x2, y2], [x3, y3, x4, y4]] and my .csv dataset is looks like:

image-0.jpg,"[[449, 576, 353, 600], [696, 576, 722, 600]]"
image-1.jpg,"[[165, 951, 468, 1654], [465, 654, 416, 654]]"
...

*(I'm saving images frame by frame from the video to specify the road lanes because I will train my model on these images and coordinates. That's why my database has names of every frame. image-0.jpg, image-1.jpg, image-2, etc.)

*(([x1, y1, x2, y2] is left road lane, [x3, y3, x4, y4] is right road lane))

But in the __getitem__ method of my dataset class, I'm getting this error:

    y_label = torch.tensor(int(self.annotations.iloc[index, 1]))
ValueError: invalid literal for int() with base 10: '[[449, 576, 353, 600], [696, 576, 722, 600]]'

How can I fix this code? I want these coordinates to be readable by PyTorch so I can train my model on these coordinates and road images that I save. I tried to convert coordinates' list to tensor list but I couldn't do that.

My full database code:

import os
import pandas as pd
import torch
from torch.utils.data import Dataset
from torchvision import transforms
from skimage import io

class RoadLanesDataset(Dataset):

    def __init__(self, csv_file, root_dir, transform=None):
        self.annotations = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform

    def __len__(self):
        return len(self.annotations)

    def __getitem__(self, index):
        img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])
        image = io.imread(img_path)
        y_label = torch.tensor(int(self.annotations.iloc[index, 1]))

        if self.transform:
            image = self.transform(image)

        return (image, y_label)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source