属性错误:"数据帧"对象没有属性"编写器"



这个程序是通过点击并拖动图像来绘制边界框并打印边界框坐标,我得到这个错误说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文件并得到此错误。我使用csv writer来存储这些值,而不是打印语句。

您没有向我们展示导致错误的部分,但我可以猜到您做了什么。

你有一个import csv,你没有给我们看,但你也进口熊猫,在某个时候你做了:

csv = pd.read_csv(...)

擦除导入的模块,并将名称csv绑定到DataFrame。因此,当您去使用csv模块时,它不在那里。

为您的csv数据框架使用不同的名称,一切都会很好。

最新更新