user18055852
user18055852

Reputation:

AttributeError: 'DataFrame' object has no attribute '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.

Upvotes: 0

Views: 2142

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

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.

Upvotes: 1

Related Questions