def Segment(NumberOfData): ImgCount = 0 AllLength = 0 AllCorrect = 0 count = 0 WrongImgs = 0 for scanned in os.listdir('dataset/scannedTest'): Path = 'dataset/scannedTest/' + scanned # Path2 = 'dataset/scanned2/'+scanned print(Path) Img = cv2.imread(Path) # Img2 = cv2.imread(Path2) S = Segmentation(Img) # S2 = Segmentation(Img2) try: S.Start() # S2.Start() except: print("Error in reading image") continue Words = S.GetSegmentedWords() # Words2 = S2.GetSegmentedWords() for i in range(len(Words)): WL = len(Words[i]) for j in range(WL): name = str(ImgCount) + ".png" cv2.imwrite("trainTest/" + name, Words[i][j]) ImgCount += 1 # AllLength += Length # AllCorrect += Correct count += 1 if count == NumberOfData: break # File.close() # AllAccuracy = (AllCorrect / AllLength) * 100 # print("Segmentation Finished") # print(str(WrongImgs) + " Failed Images") # print("Testing on " + str(AllLength) + " Words ") # print(str(AllCorrect) + " Are Correct") # print("Accuracy : " + str(AllAccuracy) + "%") return Words
def Test(Img): S = Segmentation(Img) S.Start() Words = S.GetSegmentedWords() Length = len(Words) ImgCount = 0 for i in range(Length): WL = len(Words[i]) for j in range(WL): name = str(ImgCount) + ".png" cv2.imwrite("Test/" + name, Words[i][j]) ImgCount += 1 return ImgCount
def Train(NumberOfData): ImgCount = 0 AllLength = 0 AllCorrect = 0 count = 0 WrongImgs = 0 try: shutil.rmtree("train") except: print("No train folder") os.mkdir("train") File = open("associtations.txt", "w") # exit(0) for scanned in os.listdir('dataset/scanned'): Path = 'dataset/scanned/' + scanned # Path2 = 'dataset/scanned2/'+scanned print(Path) Img = cv2.imread(Path) S = Segmentation(Img) try: S.Start() except: print("Error in reading image") continue FileName = 'dataset/text/' + scanned[:-4] + '.txt' print(FileName) File = open(FileName, "r") Lines = File.readlines() RealWords = Lines[0].split(" ") Words = S.GetSegmentedWords() Length = len(RealWords) print("================================") print(Length) print(len(Words)) # print(len(Words2)) if Length != len(Words): print("Error in Words") print("Number Of True Words: " + str(Length)) print("Number of Words: " + str(len(Words))) WrongImgs += 1 continue File = open("associtations.txt", "a") Correct = 0 for i in range(Length): WL = len(Words[i]) if WordLength(RealWords[i]) == WL: Correct += 1 for j in range(WL): name = str(ImgCount) + ".png" Char = RealWords[i][j] cv2.imwrite("train/" + name, Words[i][j]) if j < WL - 1: if (RealWords[i][j] + RealWords[i][j + 1]) == "لا": Char += 'ا' File.write(str(Dict[Char]) + " " + name + "\n") ImgCount += 1 print(str((Correct / Length) * 100) + "%") print("================================") AllLength += Length AllCorrect += Correct count += 1 if count == NumberOfData: break File.close() AllAccuracy = (AllCorrect / AllLength) * 100 print("Segmentation Finished") print(str(WrongImgs) + " Failed Images") print("Testing on " + str(AllLength) + " Words ") print(str(AllCorrect) + " Are Correct") print("Accuracy : " + str(AllAccuracy) + "%")