如何将道路车道坐标转换为Python的张量?

发布于 2025-01-24 14:51:21 字数 1798 浏览 0 评论 0原文

我正在尝试使用Pytorch开发车道探测器。基本上,我使用cv2逐帧读取视频框架,然后使用Cancy> Canny Edge检测器查找边缘,然后我使用平均车道算法来获得Lane的坐标。

这些坐标看起来像[[X1,Y1,X2,Y2],[X3,Y3,X4,Y4]]和我的.csv数据集看起来像:

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

*(我保存了图像框架。从视频中框架来指定道路车道,因为我将在这些图像和坐标上训练我的模型。

*(([[X1,Y1,X2,Y2]离开道路车道,[X3,Y3,X4,Y4]是正确的Road Lane))

)我的数据集类方法,我会遇到此错误:

    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]]'

如何修复此代码?我希望这些坐标可以通过Pytorch读取,因此我可以在保存的这些坐标和道路图像上训练模型。我试图将坐标列表转换为张量列表,但我做不到。

我的完整数据库代码:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面犯桃花 2025-01-31 14:51:21

我认为这是因为您正在尝试将列表列表转换为INT。

尝试使用dtype torch.long这样的张量。

y_label = torch.tensor(self.annotations.iloc[index, 1], dtype=torch.long)

I think it is because you are trying to convert a list of lists to an int.

Try making the tensor like this with the dtype torch.long.

y_label = torch.tensor(self.annotations.iloc[index, 1], dtype=torch.long)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文