attributeError:' dataframe'对象没有属性'作者'
该程序是通过单击并拖动图像绘制边界框,并打印边界框坐标,然后我收到此错误。 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上并没有向我们展示导致错误的零件,但我可以猜测您做了什么。
您有一个
导入的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:That ERASES the imported module, and binds the name
csv
to your DataFrame. Thus, when you go to use thecsv
module, it isn't there.Use a different name for your
csv
dataframe and all will be well.