Я пытаюсь получить точность и вывести результаты КНН на следующий код: https://github.com/MicrocontrollersAndMore/OpenCV_3_License_Plate_Recognition_Python.git?
Комментированный код - это то, что я пытался сделать;)
def recognizeCharsInPlate(imgThresh, listOfMatchingChars):
strChars = "" # this will be the return value, the chars in the lic plate
npaFlattenedImages = np.loadtxt("flattened_images.txt", np.float32)
npaClassifications = np.loadtxt("classifications.txt", np.float32)
height, width = imgThresh.shape
imgThreshColor = np.zeros((height, width, 3), np.uint8)
listOfMatchingChars.sort(key = lambda matchingChar: matchingChar.intCenterX) # sort chars from left to right
cv2.cvtColor(imgThresh, cv2.COLOR_GRAY2BGR, imgThreshColor) # make color version of threshold image so we can draw contours in color on it
for currentChar in listOfMatchingChars: # for each char in plate
pt1 = (currentChar.intBoundingRectX, currentChar.intBoundingRectY)
pt2 = ((currentChar.intBoundingRectX + currentChar.intBoundingRectWidth), (currentChar.intBoundingRectY + currentChar.intBoundingRectHeight))
cv2.rectangle(imgThreshColor, pt1, pt2, Main.SCALAR_GREEN, 2) # draw green box around the char
# crop char out of threshold image
imgROI = imgThresh[currentChar.intBoundingRectY : currentChar.intBoundingRectY + currentChar.intBoundingRectHeight,
currentChar.intBoundingRectX : currentChar.intBoundingRectX + currentChar.intBoundingRectWidth]
imgROIResized = cv2.resize(imgROI, (RESIZED_CHAR_IMAGE_WIDTH, RESIZED_CHAR_IMAGE_HEIGHT)) # resize image, this is necessary for char recognition
npaROIResized = imgROIResized.reshape((1, RESIZED_CHAR_IMAGE_WIDTH * RESIZED_CHAR_IMAGE_HEIGHT)) # flatten image into 1d numpy array
npaROIResized = np.float32(npaROIResized) # convert from 1d numpy array of ints to 1d numpy array of floats
retval, npaResults, neigh_resp, dists = kNearest.findNearest(npaROIResized, k = 1) # finally we can call findNearest !!!
# print(retval, npaResults, neigh_resp, dists)
# matches = npaResults==npaFlattenedImages
# correct = np.count_nonzero(matches)
# accuracy = correct*100.0/10000
# print(accuracy)
strCurrentChar = str(chr(int(npaResults[0][0]))) # get character from results
strChars = strChars + strCurrentChar # append current char to full string
# end for
# Take Red families and plot them
# red = npaClassifications[npaFlattenedImages.ravel()==0]
# plt.scatter(red[:,0],red[:,1],80,'r','^')
# # Take Blue families and plot them
# blue = npaClassifications[npaFlattenedImages.ravel()==1]
# plt.scatter(blue[:,0],blue[:,1],80,'b','s')
# plt.show()
# print( "result: {}\n".format(npaResults) )
# print( "neighbours: {}\n".format(neigh_resp) )
# print( "distance: {}\n".format(dists) )
# plt.show()
#
if Main.showSteps == True: # show steps #######################################################
cv2.imshow("10", imgThreshColor)
# end if # show steps #########################################################################
return strChars
# end function
Я ожидаю метрическую точность и график зависимости между K и точностью тестирования. Я спрашиваю об этом, потому что я не уверен, правильно ли я это делаю.