Giter VIP home page Giter VIP logo

android-sudoku-solver-ocr's Introduction

Table of Contents

Introduction

This project is an android app that solves sudoku, hence the name, recognized from images using Optical Character Recognition or entered manually.

I've built this project the first time in early 2022 ( v1 branch ) and this is a refresh, an update to the original project.

How to use

Download or build the app yourself. Use it like a camera app, press the shutter button when the sudoku it's detected and it will be solved. You can also edit it or input it manually if you want.

It works on both puzzles on paper or on screen, the only requirement is there is enough light and that it's smooth.

     

How it works

Sudoku board detection

To identify the sudoku board the input image is processed as follows with OpenCV:

  • Using the Canny algorithm to detect all contours in the image.
  • The biggest contour corresponds to the whole sudoku puzzle.
  • Use the found contour to extract and warp the sudoku grid .

The numbers are detected using MLKit Text Recognition.


Solving

For solving the puzzle I'm using a fairly simple, brute-force algorithm that relies on backtracking to generate the valid solution.

It goes through the whole 2D array and for each number that needs to be found it tries all possibilities and continues with the following numbers.

For each cell there are 9 possible numbers which means the time complexity of this algorithm is O(9n).

private fun solve(index: Int = 0): Boolean {
    for (i in index until 81) {
        val r = i / 9
        val c = i % 9
        if (sudokuBoard[r][c].number == 0) {
            for (n in 1..9) {
                if (isValidSolution(r, c, n)) {
                    sudokuBoard[r][c].number = n
                    if (solve(i + 1)) {
                        return true
                    }
                    sudokuBoard[r][c].number = 0
                }
            }
            return false
        }
    }
    return true
}

Solve algorithm demo

Build

  • Download or clone the repository and open it in Android Studio.
  • Download the android version of OpenCV. I used the latest version, 4.8.0.
  • Import the OpenCV module in Android Studio.
    • Click File > New > Import Module, select the OpenCV-android-sdk/sdk directory and change the module name to opencv.
    • Goto File > Project Structure > Dependencies and on the app module add opencv as a dependency.
    • In opencv build.gradle add android { namespace 'org.opencv' }, change compileSdkVersion and targetSdkVersion to 33 and finally, add
      kotlinOptions {
        jvmTarget = '1.8'
      }
      buildFeatures {
        buildConfig true
        aidl true
      }
      
    • Further we need to modify the following classes in the OpenCV module:
      • CameraBridgeViewBase: add canvas.rotate(90f, canvas.getWidth() / 2, canvas.getHeight() / 2); in function deliverAndDrawFrame, right before drawing the bitmap.
      • JavaCameraView add:
        public void turnOnFlashlight(){
            Camera.Parameters params = mCamera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(params);
        }
            
        public void turnOffFlashLight(){
            Camera.Parameters params = mCamera.getParameters();
            params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            mCamera.setParameters(params);
        }
        
        In function iniliazeCamera change
        if ((getLayoutParams().width == LayoutParams.MATCH_PARENT) && (getLayoutParams().height == LayoutParams.MATCH_PARENT))
            mScale = Math.min(((float)height)/mFrameHeight, ((float)width)/mFrameWidth);
        else
            mScale = 0;
        
        to
        mScale = Math.max(((float)height)/mFrameHeight, ((float)width)/mFrameWidth);
        
  • Done, you can now build the project

Libraries used

android-sudoku-solver-ocr's People

Contributors

hypertensiune avatar jackploeg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.