attributeError:' dataframe'对象没有属性'作者'

发布于 2025-01-21 18:14:02 字数 2357 浏览 2 评论 0原文

该程序是通过单击并拖动图像绘制边界框,并打印边界框坐标,然后我收到此错误。 attributeError:'dataframe'对象没有属性'writer'

    import cv2
    import csv
    
    class BoundingBoxWidget(object):
        def __init__(self):
            self.original_image = cv2.imread('data/colorpic3.jpg')
            self.clone = self.original_image.copy()
    
            cv2.namedWindow('image')
            cv2.setMouseCallback('image', self.extract_coordinates)
    
            # Bounding box reference points
            self.image_coordinates = []
    
        def extract_coordinates(self, event, x, y, flags, parameters):
            # Record starting (x,y) coordinates on left mouse button click
            if event == cv2.EVENT_LBUTTONDOWN:
                self.image_coordinates = [(x,y)]
    
            # Record ending (x,y) coordintes on left mouse button release
            elif event == cv2.EVENT_LBUTTONUP:
                self.image_coordinates.append((x,y))
              
           with open('results.csv', 'w') as csvfile:
                 writer= csv.writer(csvfile)

                 writer.writerow(['top left: {}, bottom right: {}'.format(self.image_coordinates[0], self.image_coordinates[1])])
                 writer.writerow(['x,y,w,h : ({}, {}, {}, {})'.format(self.image_coordinates[0][0], self.image_coordinates[0][1], self.image_coordinates[1][0] - self.image_coordinates[0][0], self.image_coordinates[1][1] - self.image_coordinates[0][1])])     
                
                # Draw rectangle 
                cv2.rectangle(self.clone, self.image_coordinates[0], self.image_coordinates[1], (0,255,0), 2)
                cv2.imshow("image", self.clone) 
    
            # Clear drawing boxes on right mouse button click
            elif event == cv2.EVENT_RBUTTONDOWN:
                self.clone = self.original_image.copy()
    
    
    
    
                
    
        def show_image(self):
            return self.clone
    
    if __name__ == '__main__':
        boundingbox_widget = BoundingBoxWidget()
        while True:
            cv2.imshow('image', boundingbox_widget.show_image())
            key = cv2.waitKey(1)
    
            # Close program with keyboard 'q'
            if key == ord('q'):
                cv2.destroyAllWindows()
                exit(1)

我试图将边界框坐标直接存储到CSV文件并获取此错误。INTENT STEAD我使用CSV Writer来存储这些值。

This program is to draw bounding box by click and drag on the image and prints the bounding box coordinates and I.m getting this error saying
AttributeError: 'DataFrame' object has no attribute 'writer'

    import cv2
    import csv
    
    class BoundingBoxWidget(object):
        def __init__(self):
            self.original_image = cv2.imread('data/colorpic3.jpg')
            self.clone = self.original_image.copy()
    
            cv2.namedWindow('image')
            cv2.setMouseCallback('image', self.extract_coordinates)
    
            # Bounding box reference points
            self.image_coordinates = []
    
        def extract_coordinates(self, event, x, y, flags, parameters):
            # Record starting (x,y) coordinates on left mouse button click
            if event == cv2.EVENT_LBUTTONDOWN:
                self.image_coordinates = [(x,y)]
    
            # Record ending (x,y) coordintes on left mouse button release
            elif event == cv2.EVENT_LBUTTONUP:
                self.image_coordinates.append((x,y))
              
           with open('results.csv', 'w') as csvfile:
                 writer= csv.writer(csvfile)

                 writer.writerow(['top left: {}, bottom right: {}'.format(self.image_coordinates[0], self.image_coordinates[1])])
                 writer.writerow(['x,y,w,h : ({}, {}, {}, {})'.format(self.image_coordinates[0][0], self.image_coordinates[0][1], self.image_coordinates[1][0] - self.image_coordinates[0][0], self.image_coordinates[1][1] - self.image_coordinates[0][1])])     
                
                # Draw rectangle 
                cv2.rectangle(self.clone, self.image_coordinates[0], self.image_coordinates[1], (0,255,0), 2)
                cv2.imshow("image", self.clone) 
    
            # Clear drawing boxes on right mouse button click
            elif event == cv2.EVENT_RBUTTONDOWN:
                self.clone = self.original_image.copy()
    
    
    
    
                
    
        def show_image(self):
            return self.clone
    
    if __name__ == '__main__':
        boundingbox_widget = BoundingBoxWidget()
        while True:
            cv2.imshow('image', boundingbox_widget.show_image())
            key = cv2.waitKey(1)
    
            # Close program with keyboard 'q'
            if key == ord('q'):
                cv2.destroyAllWindows()
                exit(1)

Here I'm trying to store bounding box coordinates directly to csv file and getting this error.Instead of print statement I'm using csv writer to store those values.

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

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

发布评论

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

评论(1

柒夜笙歌凉 2025-01-28 18:14:02

您实际上并没有向我们展示导致错误的零件,但我可以猜测您做了什么。

您有一个导入的CSV,您没有向我们展示,但是您也会导入大熊猫,在某个时候您确实这样做:

csv = pd.read_csv(...)

擦除导入的模块,并绑定名称csv到您的数据框。因此,当您使用csv模块时,它不存在。

为您的csv dataframe使用其他名称,一切都会很好。

You don't actually show us the parts that caused the error, but I can guess what you did.

You have an import csv, which you did not show us, but you also import pandas, and at some point you did:

csv = pd.read_csv(...)

That ERASES the imported module, and binds the name csv to your DataFrame. Thus, when you go to use the csv module, it isn't there.

Use a different name for your csv dataframe and all will be well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文