class ConstructGrid: def __init__(self, cellarray, modeltype): self.cellarray = cellarray self.recognizer = DigitRecognizer(modeltype) self.finalgrid = [[0 for i in range(9)] for j in range(9)] self.imagewritten = False def constructgrid(self): threshold = 5*255 for i in range(9): for j in range(9): #First preprocess and clean the board cell tmp = np.copy(self.cellarray[i][j]) tmp = self.recognizer.preprocess_image(tmp) cv2.imwrite("CleanedBoardCells/cell"+str(i)+str(j)+".jpg", tmp) #Checking if the board cell is empty or not finsum = 0 for k in range(28): rowsum = sum(tmp[k]) finsum += rowsum if finsum < threshold: self.finalgrid[i][j] = 0 continue if not self.imagewritten: try: os.remove("StagesImages/13.jpg") os.remove("StagesImages/14.jpg") except: pass cv2.imwrite("StagesImages/13.jpg", self.cellarray[i][j]) cv2.imwrite("StagesImages/14.jpg", tmp) self.imagewritten = True pred = self.recognizer.make_prediction(str("CleanedBoardCells/cell"+str(i)+str(j)+".jpg")) self.finalgrid[i][j] = int(pred) return self.finalgrid
class ConstructGrid: def __init__(self, cellarray, modeltype): self.cellarray = cellarray self.recognizer = DigitRecognizer(modeltype) self.finalgrid = [[0 for i in range(9)] for j in range(9)] self.imagewritten = False '''This function uses a threshold of 5 white pixels to determine if a grid cell is empty. If the grid cell isn't empty, the prediction of the KNN is used to determine the number. This function writes the 13th stage image to StagesImages and finally returns the final grid of predicted numbers''' def constructgrid(self): threshold = 5 * 255 for i in range(9): for j in range(9): #First preprocess and clean the board cell tmp = np.copy(self.cellarray[i][j]) tmp = self.recognizer.preprocess_image(tmp) cv2.imwrite( "CleanedBoardCells/cell" + str(i) + str(j) + ".jpg", tmp) #Checking if the board cell is empty or not finsum = 0 for k in range(28): rowsum = sum(tmp[k]) finsum += rowsum if finsum < threshold: self.finalgrid[i][j] = 0 continue if not self.imagewritten: try: os.remove("StagesImages/13.jpg") os.remove("StagesImages/14.jpg") except: pass cv2.imwrite("StagesImages/13.jpg", self.cellarray[i][j]) cv2.imwrite("StagesImages/14.jpg", tmp) self.imagewritten = True pred = self.recognizer.make_prediction( str("CleanedBoardCells/cell" + str(i) + str(j) + ".jpg")) self.finalgrid[i][j] = int(pred) return self.finalgrid
def __init__(self, cellarray, modeltype): self.cellarray = cellarray self.recognizer = DigitRecognizer(modeltype) self.finalgrid = [[0 for i in range(9)] for j in range(9)] self.imagewritten = False
def train_start(self): self.recognizer = DigitRecognizer() self.recognizer.learn()
class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.ui = loadUi(os.path.join(THIS_DIR, 'mainwindow.ui'), self) self.thread = QThread() try: self.camera = CameraDevice() except ValueError: self.ui.video.setText("Device not found!\n\nIs FFMPEG available?") else: self.camera.frame_ready.connect(self.update_video_label) self.ui.video.setMinimumSize(640 * 2, 480) self.camera.moveToThread(self.thread) self.ui.t_max.setValue(0) self.ui.t_min.setValue(255) self.ui.s_max.setValue(200) self.ui.s_min.setValue(3) self.update_values() @pyqtSlot() def start_recog(self): if self.recognizer is not None: self.camera.set_recog(self.recognizer) else: print("plz do load recogi") @pyqtSlot() def stop_recog(self): self.camera.remove_recog() @pyqtSlot() def train_start(self): self.recognizer = DigitRecognizer() self.recognizer.learn() @pyqtSlot() def make_data(self): print("Not supported.") @pyqtSlot(int) def t_max_changed(self, val): if val < self.ui.t_min.value(): self.ui.t_min.setValue(val) self.update_values() @pyqtSlot(int) def t_min_changed(self, val): if val > self.ui.t_max.value(): self.ui.t_max.setValue(val) self.update_values() @pyqtSlot(int) def s_max_changed(self, val): if val < self.ui.s_min.value(): self.ui.s_min.setValue(val) self.update_values() @pyqtSlot(int) def s_min_changed(self, val): if val > self.ui.s_max.value(): self.ui.s_max.setValue(val) self.update_values() @pyqtSlot(QImage) def update_video_label(self, image): pixmap = QPixmap.fromImage(image) self.ui.video.setPixmap(pixmap) self.ui.video.update() def update_values(self): self.camera.set_values(self.ui.t_max.value(), self.ui.t_min.value(), self.ui.s_max.value(), self.ui.s_min.value())