View docscanner_6.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build Android APK | |
on: | |
push: | |
branches: | |
- main | |
- on_device_scanning_app | |
pull_request: | |
branches: | |
- main |
View docscanner_5.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given the bitmap, send the request to the API | |
private fun createRequest(image : Bitmap, url : String, callback: Callback , processImage : Boolean = false ) { | |
// Create a temporary file and write the processed Bitmap to it. | |
// Note, the image is scaled down and then sent to the API. See `@processImage` method. | |
tempImageFile = File.createTempFile( "image" , "png" ) | |
FileOutputStream( tempImageFile ).run{ | |
val resizedImage = if ( processImage ) { | |
processImage( image ) | |
} | |
else { |
View docscanner_4.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoreAlgorithm( private var openCVResultCallback: OpenCVResultCallback ) { | |
interface OpenCVResultCallback { | |
fun onDocumentRectResult( rect : Rect ) | |
fun onBinarizeDocResult( binImage : Bitmap ) | |
} | |
fun getDocumentRect( image: Bitmap ) { | |
var x = Mat() | |
Utils.bitmapToMat(image, x) |
View docscanner_3.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from document import get_rect | |
# Streamlit application to test the document scanning algorithm | |
st.title( '📄 Document Scanning with OpenCV' ) | |
st.markdown( | |
""" | |
Upload an image containing a document. Make sure that, | |
1. There's good contrast between the background and the document. | |
2. The entire document is contained within the image. Meaning, all corners of the document should be visible in the image | |
""" |
View docscanner_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
from fastapi import FastAPI, File, UploadFile | |
from fastapi.responses import Response | |
from document import get_rect, get_binarized_img | |
app = FastAPI() | |
# POST method to get the rect of the cropped document | |
# It requires an `image` in the body of the request |
View docscanner_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
# Returns the coordinates ( x , y , w , h ) for the bounding box of the document in the given | |
# image. This image processing algorithm has a number of limitations like: | |
# 1. It requires high contrast between the document and the background to detect the edges | |
# 2. The document should be contained within the image only ( the four vertices of the document must be present | |
# in the input image ) | |
def get_rect( img ): |
View sklearn_android_5.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize the views | |
val inputEditText = findViewById<EditText>( R.id.input_edittext ) | |
val outputTextView = findViewById<TextView>( R.id.output_textview ) | |
val button = findViewById<Button>( R.id.predict_button ) | |
button.setOnClickListener { | |
// Parse input from inputEditText | |
val inputs = inputEditText.text.toString().toFloatOrNull() | |
if ( inputs != null ) { | |
val ortEnvironment = OrtEnvironment.getEnvironment() |
View sklearn_android_4.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make predictions with given inputs | |
private fun runPrediction( input : Float , ortSession: OrtSession , ortEnvironment: OrtEnvironment ) : Float { | |
// Get the name of the input node | |
val inputName = ortSession.inputNames?.iterator()?.next() | |
// Make a FloatBuffer of the inputs | |
val floatBufferInputs = FloatBuffer.wrap( floatArrayOf( input ) ) | |
// Create input tensor with floatBufferInputs of shape ( 1 , 1 ) | |
val inputTensor = OnnxTensor.createTensor( ortEnvironment , floatBufferInputs , longArrayOf( 1, 1 ) ) | |
// Run the model | |
val results = ortSession.run( mapOf( inputName to inputTensor ) ) |
View sklearn_android_3.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create an OrtSession with the given OrtEnvironment | |
private fun createORTSession( ortEnvironment: OrtEnvironment ) : OrtSession { | |
val modelBytes = resources.openRawResource( R.raw.sklearn_model ).readBytes() | |
return ortEnvironment.createSession( modelBytes ) | |
} |
View sklearn_android_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from skl2onnx import convert_sklearn | |
from skl2onnx.common.data_types import FloatTensorType | |
# Specify an initial type for the model ( similar to input shape for the model ) | |
initial_type = [ | |
( 'input_study_hours' , FloatTensorType( [None,1] ) ) | |
] | |
# Write the ONNX model to disk | |
converted_model = convert_sklearn( regressor , initial_types=initial_type ) |
NewerOlder