酸洗cv2.Knearest对象不起作用

时间:2017-04-03 11:40:34

标签: python pickle cv2

我一直在尝试将cv2.knearest训练过的模型保存到文件中。对于下面的代码(python 2)

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30

clock = pygame.time.Clock()

# RGB values for red
red = (255, 0 ,0)

# Your three button rects :
simulator_rect = pygame.Rect(225, 125, 30, 30)
quiz_rect = pygame.Rect(600, 125, 30, 30)
quit_rect = pygame.Rect(375, 425, 30, 30)
# These represent your three option rects
option_rects = [simulator_rect, quiz_rect, quit_rect]

# Your blue selector rect
selector_rect = pygame.Rect(50, 50, 60, 60)
# The 50, 50 xy coords are temporary

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP] and y > 0: y -= 5
    if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
    if pressed[pygame.K_LEFT] and x > 0: x -= 5
    if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5

    # Set the slector rect's coords to x/y
    selector_rect.x, selector_rect.y = x, y

    screen.fill((0, 0, 0))

    color = (0, 128, 255)
    pygame.draw.rect(screen, color, selector_rect)

    myfont = pygame.font.SysFont("monospace", 15)

    label = myfont.render("Start the experiment simulator", 1, (255,255,255))
    screen.blit(label, (100, 100))

    label2 = myfont.render("Start the quiz", 1, (255,255,255))
    screen.blit(label2, (550, 100))

    label3 = myfont.render("Quit game", 1, (255,255,255))
    screen.blit(label3, (350, 400))

    # Use our created rects
    pygame.draw.rect(screen, red, simulator_rect)
    pygame.draw.rect(screen, red, quiz_rect)
    pygame.draw.rect(screen, red, quit_rect)

    # Check to see if the user presses the enter key
    if pressed[pygame.K_RETURN]:
        # Check to see if the selection rect 
        # collides with any other rect
        for rect in option_rects:
        # Add rects as needed
            if selector_rect.colliderect(rect):
                if rect == simulator_rect:
                    # Do simulations stuff!
                    print('Simulating!')
                elif rect == quiz_rect:
                    # Do quizzing stuff!
                    print('Quizzing!')
                elif rect == quit_rect:
                    # Quit!
                    done = True




    pygame.display.flip()
    clock.tick(60)

我得到了

import numpy as np 
import cv2
from sklearn.datasets import load_digits
import pickle
from sklearn.externals import joblib
samples = np.float32(np.loadtxt('feature_vector_pixels.data'))
responses = np.float32(np.loadtxt('samples_pixels.data'))
model = cv2.KNearest()
model.train(samples, responses)
# save the model to disk
filename = 'init_model.sav'
pickle.dump(model, open(filename, 'wb'))

是否有任何替代方法可以将此模型保存到文件中。

另外 在使用model.save时,我得到Traceback (most recent call last): File "picklemake.py", line 14, in <module> pickle.dump(model, open(filename, 'wb')) File "C:\Users\Karthik\Anaconda3\envs\py27\lib\pickle.py", line 1376, in dump Pickler(file, protocol).dump(obj) File "C:\Users\Karthik\Anaconda3\envs\py27\lib\pickle.py", line 224, in dump self.save(obj) File "C:\Users\Karthik\Anaconda3\envs\py27\lib\pickle.py", line 306, in save rv = reduce(self.proto) File "C:\Users\Karthik\Anaconda3\envs\py27\lib\copy_reg.py", line 70, in _reduce_ex raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle KNearest objects

1 个答案:

答案 0 :(得分:1)

Knearest延伸CvStatModel。后者有两个save(...)方法的重载,完全符合你的需要。

我不知道为什么,但经验法则是如果你在C(++)库上使用python包装器,它的类通常不会被pickleable。

相关问题