简体   繁体   中英

Using pandas to save descriptors into csv

I have problem with my code. I want to save HOG descriptors into csv file.

Code:

import cv2
import pandas as pd

def getHogFromImage(im):
   hog = cv2.HOGDescriptor()
   h = hog.compute(cv2.imread(im))
   return h

h1 = getHogFromImage("apple.jpg")
h2 = getHogFromImage("peach.jpg")

apple = pd.DataFrame("1," + h1) #any idea how do it?
peach = pd.DataFrame("0," + h2)

apple.to_csv('foo.csv', index=False, header=None, sep=",", line_terminator="\n")
peach.to_csv('foo.csv', index=False, header=None, sep=",", line_terminator="\n")

My foo.csv file

0.15200253
0.16865349
0.0909358
0.13702433
0.15322956
0.14372425
0.18107016
0.12029551
0.065096505
0.20796815
0.28659236
0.15984383
0.12754892
0.15886153
.
.
.

But I need save the descriptors into file in this format

1, 0.15200253, 0.16865349, 0.0909358, 0.13702433 .....
0, 0.16865349, 0.0909358, 0.13702433

So all descriptor data will be in one line separated by ",". But when image will be apple the first value will be "1" and when image will be peach the first value will be 0.

假设您的hog值是浮点数列表,您可以通过将h值放在[]的内部来创建数据框构造函数中的列表列表

df = pd.DataFrame([h1,h2]).to_csv('foo.csv', index=False, header=None, sep=",", line_terminator="\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM