def detect(self, img): if img.shape[0] < self.winSize_[1] or img.shape[1] < self.winSize_[0]: img = imageProcessor.resize(img, self.winSize_[0], self.winSize_[1]) # detector = cv2.HOGDescriptor() # detector.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() ) # detector.save("hog.xml") detector = imageProcessor.getMultiScaleDetector(self.winSize_) supportVector = self.classifier_.getSupportVectors()[0] supportVector = self.__convert2ColSample(supportVector, "float32") detector.setSVMDetector(supportVector) img = imutils.resize(img, width=min(960, img.shape[1])) pad = 15 rois, weights = detector.detectMultiScale(img, winStride=(4, 4), padding=(pad, pad), scale=1.2) # apply non-maxima suppression to the bounding boxes using a # fairly large overlap threshold to try to maintain overlapping # boxes that are still people rois = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rois]) rois = non_max_suppression(rois, probs=None, overlapThresh=0.65) for (x1, y1, x2, y2) in rois: cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2) return img, rois
def predict(self, img): if img is None: print "detector detect(): no image input" quit() # check the size of the training sample, if not equal to the windowSize then resize it if img.shape[0] != self.winSize_[1] or img.shape[1] != self.winSize_[0]: img = imageProcessor.resize(img, self.winSize_[0], self.winSize_[1]) # get the feature from this img file, the output is an 2d np.array feature = self.featureExtract(img) # convert the feature into ROW_SAMPLE form for the training of classifier # the output is a 1d np.array rowData = self.__convert2RowSample(feature, "float32") # convert the 1d np.array to a 2d np.array with only 1 row rowData = np.array([rowData], dtype="float32") # the return value is a 2d np.array, res[i][0] is the result for the i-th item res = self.classifier_.predict(rowData) return res[0][0]
def train(self, dataSet, labels): featureVectorSet = [] for img in dataSet: # check the size of the training sample, if not equal to the windowSize then resize it if img.shape[0] != self.winSize_[1] or img.shape[ 1] != self.winSize_[0]: img = imageProcessor.resize(img, self.winSize_[0], self.winSize_[1]) # get the feature from this img file, the output is an 2d np.array feature = self.featureExtract(img) # convert the feature into ROW_SAMPLE form for the training of classifier # the output is a 1d np.array feature = self.__convert2RowSample(feature, "float32") featureVectorSet.append(feature) # convert the vectorSet from a list of 1d np.array to a 2d np.array featureVectorSet = np.array(featureVectorSet, dtype="float32") # convert the list of labels to a 1d np.array labels = np.array(labels, dtype="int32") self.classifier_.train(featureVectorSet, labels)
end='\r') im = imageProcessor.getImage(mpath, imgSize) print(' Processing (' + str(path.getPercentage()) + '% Completed) - {0} - Image Uploaded '.format( path.getCurrentFileName()), end='\r') imageProcessor.sliceImg(im) print(' Processing (' + str(path.getPercentage()) + '% Completed) - {0} - Smart Cropping Completed '.format( path.getCurrentFileName()), end='\r') imageProcessor.arr2rImage(im) im.RealImage = imageProcessor.resize(im.RealImage, (imgSize - (imgMargin * 2))) im.RealImage = imageProcessor.expand(im.RealImage, imgSize, im.bgColor) print(' Processing (' + str(path.getPercentage()) + '% Completed) - {0} - Smart Resizing Completed '.format( path.getCurrentFileName()), end='\r') imageProcessor.saveImage(im, path.getCurrentDestination(), imgQuality) print( ' ', end='\r') print(' {0} Processed and saved as {1}'.format( path.getCurrentFileName(), path.getCurrentDestination())) if im.bgDifference > 50: path.moveProcessed('error')
x, y, w, h = cv2.boundingRect(c) if (pow(max(h, w), 2) > (imgHeight * imgWidth) / 1200 and pow(max(h, w), 2) < (imgHeight * imgWidth) / 5): img = binImg[y:y + h, x:x + w] char = Character(img, x, y, w, h) characters.append(char) imgWithBounds = ip.drawCharacterBounds(binImg.copy(), characters) cv2.imwrite("figures/preCharProcessing.jpg", imgWithBounds) cp.processCharacters(characters) imgWithBounds = ip.drawCharacterBounds(binImg.copy(), characters) cv2.imwrite("figures/postCharProcessing.jpg", imgWithBounds) cv2.imwrite("figures/characterPreFormatting.jpg", characters[0].image) for char in characters: ip.squareImage(char) ip.resize(char, (45, 45)) cv2.imwrite("figures/characterPostFormatting.jpg", characters[0].image) classifiedImg = ip.drawClassifiedCharacters(imgWithBounds, classifier, characters) cv2.imwrite("figures/classifiedCharacters.jpg", classifiedImg) lines = parseEquation(characters) final = binImg.copy() drawSolvedEquations(lines, final, 'x') cv2.imwrite("figures/solvedEquation.jpg", final)