def recognizeCharacters(self, image): global RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT, kNearest self.text = "" informations = [] grayBlurredImage = cv2.GaussianBlur( ImageProcessor.convertImageToGray(image), (5, 5), 0) # convert the image to gray and blur it... thresholdedImage = cv2.adaptiveThreshold( grayBlurredImage, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) image, contours, hierarchy = cv2.findContours(thresholdedImage.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: information = Information() information.contour = contour information.contourArea = cv2.contourArea(information.contour) if information.isContourValid(): information.boundingRectangle = cv2.boundingRect( information.contour) information.calculateRectangle() informations.append(information) informations.sort(key=operator.attrgetter( "rectangle.x")) # sort contours from left to right... del self.rectangles[:] # clearing the list "rectangles"... for information in informations: image = thresholdedImage[ information.rectangle.y:information.rectangle.y + information.rectangle.height, information.rectangle.x:information.rectangle.x + information.rectangle.width] image = cv2.resize(image, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT)) numpyArray = numpy.float32( image.reshape( (1, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT ))) # convert type of numpy array from int to float... returnValue, result, neighborResponse, distance = kNearest.findNearest( numpyArray, k=1 ) # result = vector with results of prediction (regression or classification) for each input sample... it is a single-precision floating-point vector with number_of_samples elements... if distance > 4712875: continue self.text = self.text + str(chr(int(result[0][0]))) self.rectangles.append(information.rectangle) print "=" + str(distance) + "=" # for debugging purpose... if len(self.text.strip()) != 0: print self.text + "\n"
def recognizeCharacters(self, image): global text, kNearest text = "" informations = [] validInformations = [] imgGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # get grayscale image imgBlurred = cv2.GaussianBlur(imgGray, (5, 5), 0) # blur # filter image from grayscale to black and white imgThresh = cv2.adaptiveThreshold( imgBlurred, # input image 255, # make pixels that pass the threshold full white cv2. ADAPTIVE_THRESH_GAUSSIAN_C, # use gaussian rather than mean, seems to give better results cv2. THRESH_BINARY_INV, # invert so foreground will be white, background will be black 11, # size of a pixel neighborhood used to calculate threshold value 2) # constant subtracted from the mean or weighted mean imgThreshCopy = imgThresh.copy( ) # make a copy of the thresh image, this in necessary b/c findContours modifies the image imgContours, contours, npaHierarchy = cv2.findContours( imgThreshCopy, # input image, make sure to use a copy since the function will modify this image in the course of finding contours cv2.RETR_EXTERNAL, # retrieve the outermost contours only cv2.CHAIN_APPROX_SIMPLE ) # compress horizontal, vertical, and diagonal segments and leave only their end points for contour in contours: information = Information() information.contour = contour information.boundingRectangle = cv2.boundingRect( information.contour) information.calculateRectangle() information.contourArea = cv2.contourArea(information.contour) informations.append(information) for information in informations: if information.isContourValid(): validInformations.append(information) validInformations.sort(key=operator.attrgetter( "rectangle.x")) # sort contours from left to right for information in validInformations: # cv2.rectangle(image, (information.x, information.y), (information.x + information.width, information.y + information.height), (0, 255, 0), 2) imgROI = imgThresh[ information.rectangle.y:information.rectangle.y + information.rectangle.height, information.rectangle.x:information.rectangle.x + information.rectangle.width] imgROIResized = cv2.resize( imgROI, (self.RESIZED_IMAGE_WIDTH, self.RESIZED_IMAGE_HEIGHT) ) # resize image, this will be more consistent for recognition and storage npaROIResized = imgROIResized.reshape( (1, self.RESIZED_IMAGE_WIDTH * self.RESIZED_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) if dists > 4712875: continue self.rectangles.append(information.rectangle) print "=" + str(dists) + "=" text = text + str(chr(int( npaResults[0][0]))) # append current char to full string if len(text.strip()) != 0: print text + "\n" return image