def main(): features = [] for type in ['number', 'upper', 'lower']: bw = gambar.to_bw(gambar.to_gray(gambar.read('ocr_train_{}.PNG'.format(type)))) features.extend([array.tolist() for array in ocr.get_features(bw)]) labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] with open('ocr_train.json', 'w') as outfile: json.dump({'features': features, 'labels': labels}, outfile)
def main(path): img = gambar.read(path) gray = gambar.to_gray(img) bw = gambar.to_bw(gray) tulang = zhangsuen.penulangan(bw) with open('ocr_train.json') as infile: train = json.load(infile) results = [] objects = get_objects(bw) for object in objects: feature = get_feature(tulang[object[0]-1:object[1]+2, object[2]-1:object[3]+2]) results.append(classify(train, feature)) return ''.join(results)
tulangr = tulang.copy() codes = [] ujung, simpangan = get_identity(tulangr) while len(ujung) > 0: y, x = ujung[0] if tulangr[y, x]: code = chaincode.iterate_chaincode(tulangr, y, x) if len(code) > 0: codes.append(code) ujung, simpangan = get_identity(tulangr) codes.extend(chaincode.get_chaincode(tulangr)) return codes if __name__ == ('__main__'): img = gambar.read(sys.argv[1]) gray = gambar.to_gray(img) bw = gambar.to_bw(gray) tulang = penulangan(bw) ujung, simpangan = get_identity(tulang) json_data = { 'ujung': [list((int(y), int(x))) for y, x in ujung], 'simpangan': [list((int(y), int(x))) for y, x in simpangan], } print json.dumps(json_data) # code = get_chaincode(tulang) # belok = chaincode.get_kodebelok(code) # print belok
def main(): method = sys.argv[1] input = sys.argv[2] id = sys.argv[3] home = os.path.expanduser('~')+'\\.IF5181\\' dir = home+id+'\\' if not os.path.isdir(dir): if os.path.isdir(home): shutil.rmtree(home, ignore_errors=True) if not os.path.isdir(home): os.mkdir(home) os.mkdir(dir) os.chdir('py') if method == 'histogram': img = gambar.read(input) hist = gambar.get_histogram(img) json_save({ 'r': list(gambar.get_histogram(img[:, :, 0])), 'g': list(gambar.get_histogram(img[:, :, 1])), 'b': list(gambar.get_histogram(img[:, :, 2])), }, dir+'histogram.json') elif method == 'equalize': img = gambar.read(input) gray = gambar.to_gray(img) hist_gray = gambar.get_histogram(gray) result, hist_result = gambar.equalize(gray, hist_gray) gambar.save(gray, dir+'gray.jpg') json_save(list(hist_gray), dir+'histogram_gray.json') gambar.save(result, dir+'equalized.jpg') json_save(list(hist_result), dir+'histogram_equalized.json') elif method == 'otsu': img = gambar.read(input) gray = gambar.to_gray(img) gambar.save(gray, dir+'gray.jpg') bw = gambar.to_bw(gray) gambar.save(bw, dir+'binary.jpg') elif method == 'derajat0': img = gambar.read(input) gray = gambar.to_gray(img) gambar.save(gray, dir+'gray.jpg') average = konvolusi.derajat0(gray, 'average') gambar.save(average, dir+'derajat0-average.jpg') homogen = konvolusi.derajat0(gray, 'homogen') gambar.save(homogen, dir+'derajat0-homogen.jpg') difference = konvolusi.derajat0(gray, 'difference') gambar.save(difference, dir+'derajat0-difference.jpg') elif method == 'derajat1': img = gambar.read(input) gray = gambar.to_gray(img) gambar.save(gray, dir+'gray.jpg') sobel = konvolusi.derajat1(gray, 'sobel') gambar.save(sobel, dir+'derajat1-sobel.jpg') prewitt = konvolusi.derajat1(gray, 'prewitt') gambar.save(prewitt, dir+'derajat1-prewitt.jpg') elif method == 'derajat2': img = gambar.read(input) gray = gambar.to_gray(img) gambar.save(gray, dir+'gray.jpg') kirsch = konvolusi.derajat2(gray, 'kirsch') gambar.save(kirsch, dir+'derajat2-kirsch.jpg') prewitt = konvolusi.derajat2(gray, 'prewitt') gambar.save(prewitt, dir+'derajat2-prewitt.jpg') elif method == 'gauss': img = gambar.read(input) gambar.save(konvolusi.gaussian(img), dir+'gauss.jpg') elif method == 'chaincode': img = gambar.read(input) gray = gambar.to_gray(img) bw = gambar.to_bw(gray) gambar.save(bw, dir+'binary.jpg') kopong = gambar.koponging(bw) gambar.save(kopong, dir+'kopong.jpg') code = chaincode.get_chaincode(kopong) belok = chaincode.get_kodebelok(code) json_save(code, dir+'chaincode.json') json_save(belok, dir+'kodebelok.json') elif method == 'skeleton': img = gambar.read(input) gray = gambar.to_gray(img) bw = gambar.to_bw(gray) gambar.save(bw, dir+'binary.jpg') tulang = zhangsuen.penulangan(bw) gambar.save(tulang, dir+'tulang.jpg') ujung, simpangan = zhangsuen.get_identity(tulang) json_save({ 'ujung': [[int(y), int(x)] for y, x in ujung], 'simpangan': [[int(y), int(x)] for y, x in simpangan], }, dir+'tulang-identity.json') code = zhangsuen.get_chaincode(tulang) belok = chaincode.get_kodebelok(code) json_save(code, dir+'tulang-chaincode.json') json_save(belok, dir+'tulang-kodebelok.json') elif method == 'ocr': result = ocr.main(input) json_save({ 'result': result }, dir+'ocr-result.json') elif method == 'face-warna': img = gambar.read(input) masker = wajah.get_masker(img) maskered = wajah.draw_masker(img, masker) gambar.save(maskered, dir+'face-masker.jpg') wajahs = wajah.get_wajahs(masker) kotaked = wajah.draw_kotak(img, wajahs) gambar.save(kotaked, dir+'face-kotak.jpg') elif method == 'face-bentuk': img = gambar.read(input) wajah.limatitik(img) wajah.titikobjek(img) elif method == 'face-recognition': facer.main(input)