Question

Using the Nearest Neighbor Rule and mesh feature, perform character recognition in opencv. Characters to be...

Using the Nearest Neighbor Rule and mesh feature, perform character
recognition in opencv. Characters to be recognized include alphabetical letters, numerics, Hiragana, or Kanji
(Chinese characters) etc. from input image jpeg into traning data set using simple AI protocol

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Digit Recognition -

import numpy as np

import cv2 as cv

img = cv.imread('digits.png')

gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)

# Now we split the image to 5000 cells, each 20x20 size

cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]

# Make it into a Numpy array. It size will be (50,100,20,20)

x = np.array(cells)

# Now we prepare train_data and test_data.

train = x[:,:50].reshape(-1,400).astype(np.float32) # Size = (2500,400)

test = x[:,50:100].reshape(-1,400).astype(np.float32) # Size = (2500,400)

# Create labels for train and test data

k = np.arange(10)

train_labels = np.repeat(k,250)[:,np.newaxis]

test_labels = train_labels.copy()

# Initiate kNN, train the data, then test it with test data for k=1

knn = cv.ml.KNearest_create()

knn.train(train, cv.ml.ROW_SAMPLE, train_labels)

ret,result,neighbours,dist = knn.findNearest(test,k=5)

# Now we check the accuracy of classification

# For that, compare the result with test_labels and check which are wrong

matches = result==test_labels

correct = np.count_nonzero(matches)

accuracy = correct*100.0/result.size

print( accuracy )

English alphabets recognition -

import cv2 as cv

import numpy as np

# Load the data, converters convert the letter to a number

data= np.loadtxt('letter-recognition.data', dtype= 'float32', delimiter = ',',

converters= {0: lambda ch: ord(ch)-ord('A')})

# split the data to two, 10000 each for train and test

train, test = np.vsplit(data,2)

# split trainData and testData to features and responses

responses, trainData = np.hsplit(train,[1])

labels, testData = np.hsplit(test,[1])

# Initiate the kNN, classify, measure accuracy.

knn = cv.ml.KNearest_create()

knn.train(trainData, cv.ml.ROW_SAMPLE, responses)

ret, result, neighbours, dist = knn.findNearest(testData, k=5)

correct = np.count_nonzero(result == labels)

accuracy = correct*100.0/10000

print( accuracy )

Add a comment
Know the answer?
Add Answer to:
Using the Nearest Neighbor Rule and mesh feature, perform character recognition in opencv. Characters to be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT