2017-08-03 61 views
0

我有以下錯誤...錯誤:需要對類字節對象,而不是「STR」(cPickle的,Python)的

error

其被稱作的代碼是這樣的:

from hog import HOG 
import dataset 
import argparse 
import _pickle as cPickle 
import mahotas 
import cv2 


ap = argparse.ArgumentParser() 
ap.add_argument("-m", "--model", required = True, 
    help = "path to where the model will be stored") 
ap.add_argument("-i", "--image", required = True, 
    help = "path to the image file") 
args = vars(ap.parse_args()) 

model = open(args["model"]).read() 
model = cPickle.loads(model) 

hog = HOG(orientations = 18, pixelsPerCell = (10, 10), 
cellsPerBlock = (1, 1), normalize = True) 

image = cv2.imread(args["image"]) 
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 

blurred = cv2.GaussianBlur(gray, (5, 5), 0) 
edged = cv2.Canny(blurred, 30, 150) 
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 

cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts], key =lambda x: x[1]) 

for (c, _) in cnts: 
    (x, y, w, h) = cv2.boundingRect(c) 
    if w >= 7 and h >= 20: 
     roi = gray[y:y + h, x:x + w] 
     thresh = roi.copy() 
     T = mahotas.thresholding.otsu(roi) 
     thresh[thresh > T] = 255 
     thresh = cv2.bitwise_not(thresh) 
     thresh = dataset.deskew(thresh, 20) 
     thresh = dataset.center_extent(thresh, (20, 20)) 
     cv2.imshow("thresh", thresh) 

     hist = hog.describe(thresh) 
     digit = model.predict(hist)[0] 
     print 
     "I think that number is: %d" % (digit) 
     cv2.rectangle(image, (x, y), (x + w, y + h), 
      (0, 255, 0), 1) 
     cv2.putText(image, str(digit), (x - 10, y - 10), 
      cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 2) 
     cv2.imshow("image", image) 
     cv2.waitKey(0) 

這一個是 「連接」 到另一部件(在同一封裝中的另一個.py文件...),其是這樣的:

from sklearn.svm import LinearSVC 
from hog import HOG 
import dataset 
import argparse 
import _pickle as cPickle 


ap = argparse.ArgumentParser() 
ap.add_argument("-d", "--dataset", required = True, 
    help = "path to the dataset file") 
ap.add_argument("-m", "--model", required = True, 
    help = "path to where the model will be stored") 
args = vars(ap.parse_args()) 

(digits, target) = dataset.load_digits(args["dataset"]) 
data = [] 

hog = HOG(orientations = 18, pixelsPerCell = (10, 10), 
    cellsPerBlock = (1, 1), normalize = True) 

for image in digits: 
    image = dataset.deskew(image, 20) 
    image = dataset.center_extent(image, (20, 20)) 

    hist = hog.describe(image) 
    data.append(hist) 

model = LinearSVC(random_state = 42) 
model.fit(data, target) 
f = open(args["model"], "w") 
f.write(str(cPickle.dumps(model))) 
f.close() 

注意:在這一行--->f.write(str(cPickle.dumps(model)))我將它自己鑄造爲str,因爲當我試圖執行最後一個塊時它給出了錯誤。

順便說一句,有人知道爲什麼它給出了在CMD中顯示的錯誤?

謝謝

+0

'cPickle'有一個'dump'功能,讓您通過以二進制模式打開的文件。直接使用。 –

+0

編輯答案。我現在第一次使用cPickle ... – Link

+0

根據您的「編輯」標記爲重複。 –

回答

1

嘗試以字節模式打開您的文件。所以更換

f = open(args["model"], "w") 

f = open(args["model"], "wb") 
+0

像你說的那樣。刪除str()投射。主要的代碼似乎工作。但現在我有另一個錯誤...更新回答.. – Link

+0

你的新錯誤是什麼? – MLavrentyev

+0

是這樣的:https://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character 但我不知道如何應用解決方案我的代碼.. – Link

1

你需要寫和讀你的泡菜以字節爲單位,而不是文本:

# Reading 
with open(args['model'], 'b') as f: 
    model = cPickle.load(f) 

... 

# Writing 
with open(args['model'], 'wb') as f: 
    cPickle.dump(model, f, 2) 
+0

這可以直接應用於我的代碼? – Link

相關問題