2017-09-29 48 views
-2

這裏是我的代碼:如何在python中保存單詞數據包?

sift=cv2.xfeatures2d.SIFT_create() 
descriptors_unclustered=[] 
dictionarysize=800 
BOW=cv2.BOWKmeansTrainer(dictionarysize) 
for p in training-paths : 
    kp,dsc=sift.detectAndCompute(image,None) 
    BOW.add(dsc) 

dictionary=BOW.cluster() 
bowdiction=cv2.BOWImgDescriptorExtractor(sift, cv2.BFMatcher(cv2.NORM_L2)) 
bowdiction.setvocabulary(dictionary) 

我要救這個bowdiction數據以後使用它。我不想每次都等待這些計算,那麼如何保存這些數據?

回答

1

使用pickle

  1. 保存低頭泡菜:

    import pickle 
    
    sift=cv2.xfeatures2d.SIFT_create() 
    descriptors_unclustered=[] 
    dictionarysize=800 
    BOW=cv2.BOWKmeansTrainer(dictionarysize) 
    for p in training-paths : 
        kp,dsc=sift.detectAndCompute(image,None) 
        BOW.add(dsc) 
    
    with open('bow_pickle.pickle', 'wb') as f: 
    pickle.dump(f) 
    
  2. 返回的從泡菜數據:

    import pickle  
    with open('bow_pickle.pickle', 'rb') as f: 
        BOW = pickle.load(f)  
    dictionary=BOW.cluster() 
    bowdiction=cv2.BOWImgDescriptorExtractor(sift,cv2.BFMatcher(cv2.NORM_L2)) 
    bowdiction.setvocabulary(dictionary) 
    
+0

我能保存詞典在同方式?因爲特別是「字典= BOW.cluster()」需要很多時間 – ali

+0

是的,你可以使用 –

相關問題