예제 #1
0
파일: creator.py 프로젝트: xsyann/ocr
class DatasetCreator:

    ESC = 27
    DEL = 127
    RET = 13
    KEY_UP = 0
    KEY_DOWN = 1
    EXT = ".bmp"
    FINAL_SIZE = 50

    def __init__(self, prefix, dryRun):
        h, w = 300, 300
        img = np.zeros((h, w, 3), np.uint8)
        img[:,:] = (255, 255, 255)
        self.brush = Brush("Brush")
        self.sketcher = Sketcher("Dataset Creator", img, self.brush)
        self.__folders = OCR.generateFolderList(OCR.DIGITS | OCR.LETTERS | OCR.SYMBOLS)
        self.__prefix = prefix
        self.__lastFile = None
        self.__dryRun = dryRun

    def run(self):
        while True:
            k = cv2.waitKey(0) & 0xFF

            if k == self.ESC:
                break
            elif k == self.RET:
                self.sketcher.reset()
                self.sketcher.show()
            elif k == self.KEY_UP:
                self.brush.brushSize += 1
            elif k == self.KEY_DOWN:
                self.brush.brushSize -= 1
            elif k == self.DEL and not self.__dryRun:
                if self.__lastFile:
                    os.remove(self.__lastFile)
                    self.__lastFile = None
                    print path + " Removed"
            elif chr(k) in self.__folders:
                folder = self.__folders[chr(k)]
                path = common.generateFilename(folder, self.__prefix, self.EXT)
                item = self.sketcher.sketch.copy()
                item = cv2.resize(item, (self.FINAL_SIZE, self.FINAL_SIZE))
                cv2.imshow("Generated Dataset item", item)
                self.sketcher.reset()
                self.__lastFile = path
                if not self.__dryRun:
                    cv2.imwrite(path, item)
                    print path + " Written"
                else:
                    print path + " Preview"
예제 #2
0
파일: test.py 프로젝트: xsyann/ocr
class TestOCR:

    ESC = 27
    KEY_UP = 0
    KEY_DOWN = 1

    def __init__(self, model, filenames, flags, pattern):
        self.ocr = OCR()
        self.ocr.loadModel(model, OCR.MODEL_ANN, flags)
        if filenames:
            counter = {}
            for filename in filenames:
                ch = self.ocr.charFromFile(filename)
                if pattern:
                    label = self.__exec(pattern, filename)
                    if label in counter:
                        predicted, total = counter[label]
                        counter[label] = (predicted + int(label == ch), total + 1)
                    else:
                        counter[label] = (int(label == ch), 1)
                print "%s in %s" % (ch, filename)
            if pattern:
                self.__analyze(counter)
        else:
            h, w = 200, 200
            img = np.zeros((h, w, 3), np.uint8)
            img[:,:] = (255, 255, 255)
            self.brush = Brush("Brush")
            self.sketcher = Sketcher('Test OCR', img, self.brush)

    def __analyze(self, counter):
        print ""
        totalPercent = []
        for label, (predicted, total) in sorted(counter.iteritems()):
            percent = int(float(predicted) / total * 100)
            totalPercent.append(percent)
            print "{0}\t{1} / {2} - {3} %".format(label, predicted, total, percent)
        print "\nTotal recognized : {0:.1f} %\n".format(np.mean(totalPercent))

    def __exec(self, file, arg):
        out = subprocess.Popen([file, arg], stdout=subprocess.PIPE).communicate()[0]
        return out

    def __displayResponse(self, ch):
        response = np.zeros((75, 75, 3), np.uint8)
        response[:,:] = (255, 255, 255)
        cv2.putText(response, ch, (15, 35), cv2.FONT_HERSHEY_PLAIN, 3.0, (0, 0, 0), 1)
        cv2.imshow("Response", response)

    def run(self):
        while True:
            k = cv2.waitKey(0) & 0xFF
            if k == self.ESC:
                break
            elif k == ord('r'):
                self.sketcher.reset()
                self.sketcher.show()
            elif k == ord(' '):
                ch = self.ocr.charFromImage(self.sketcher.sketch)
                self.sketcher.show()
                self.sketcher.reset()
                self.__displayResponse(ch)
                print "OCR : %s" % ch
            elif k == self.KEY_UP:
                self.brush.brushSize += 1
            elif k == self.KEY_DOWN:
                self.brush.brushSize -= 1