0
我有一個正確運行並打印值的opencv python程序。但是,當我嘗試將打印的值寫入csv文件時,出現錯誤。 以下是代碼:Python將多個數組寫入爲csv
for testingPath in paths.list_images(args["testing"]):
# load the image and make predictions
image = cv2.imread(testingPath)
boxes = detector(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# loop over the bounding boxes and draw them
for b in boxes:
(x, y, w, h) = (b.left(), b.top(), b.right(), b.bottom())
cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
#print(basename(testingPath),"CX:"+str(x),"CY:"+str(y),"Width:"+str(w),"Height:"+str(h),brandname,"Number of brands detected: {}".format(len(boxes))) -----this prints all the required values without problem on the console
我試圖這樣做:
我添加的參數之前,對循環開始:
ap.add_argument("-i", "--index", required=True, help="Path to directory of output")
output = open(args["index"], "w")
和所用的環如下:
for testingPath in paths.list_images(args["testing"]):
# load the image and make predictions
image = cv2.imread(testingPath)
#filename = testingPath[testingPath.rfind("/") + 1:]
boxes = detector(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#print(basename(testingPath), brandname,"Number of brands detected: {}".format(len(boxes)))
# loop over the bounding boxes and draw them
for b in boxes:
(x, y, w, h) = (b.left(), b.top(), b.right(), b.bottom())
cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
#print(basename(testingPath),"CX:"+str(x),"CY:"+str(y),"Width:"+str(w),"Height:"+str(h),brandname,"Number of brands detected: {}".format(len(boxes)))
dat = str([x, y, w, h, brandname, len(boxes)])
output.write("{},{}\n".format(testingPath, "".join(dat)))
上面的代碼打印值如下:
/home/mycomp/VideoExtract/28157.jpg,[83, 349, 164, 383, 'Pirelli', 1]
我想擺脫[]括號。所需的操作是將打印的值寫入csv /文本文件。
此示例代碼不打算提出建議,以改善更換您的最後兩行
代碼質量和/或可讀性。 –
謝謝....這個改變奏效了。 – Apricot