0
votes

I am just beginning to learn the sklearn module and faced my first roadblocks. First I created a number recognition logistic regression model and it seem to work fine. But then I decided to test a random image myself, so I used OpenCV module to opened a picture that I randomly select from online (Shown below). The shape of the image is (425,425,3). I turned them into grayScale and threshold it. Then I try to predict it using the model I just created. But I got a "ValueError: Found array with dim 3. Estimator expected <= 2." I tried to use resize, reshape function to my image to match the array size but never seem to work, anyone can help me to solve the problem?

import cv2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.linear_model import LogisticRegression 

test_case = cv2.imread('unnamed.jpg')
test_case = cv2.cvtColor(test_case, cv2.COLOR_BGR2GRAY)
ret, test_case = cv2.threshold(test_case, 200, 255, cv2.THRESH_BINARY)



digits = load_digits()
x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size = 0.23, random_state = 2)

#test_case = test_case.reshape(test_case.shape[0], -1)

train = LogisticRegression(dual = False, max_iter = 1000000)
train.fit(x_train, y_train)
nbr = train.predict(np.array([test_case], 'float64'))

The image I am reading from

1

1 Answers

0
votes

First, you train your model on the figures from sklearn. Each digit has the form = (64), which means that they are flattened in a row.

Second, you must change the size of your image to 8x8

For example:

test_case = cv2.imread('ELXkj.jpg')
test_case = cv2.cvtColor(test_case, cv2.COLOR_BGR2GRAY)
test_case = cv2.resize(test_case, (8,8))
ret, test_case = cv2.threshold(test_case, 200, 255, cv2.THRESH_BINARY)


digits = load_digits()
x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size = 0.23, random_state = 2)

train = LogisticRegression(dual = False, max_iter = 1000000)
train.fit(x_train, y_train)
nbr = train.predict(np.array([test_case.flatten()], 'float64'))