Created
November 26, 2019 06:49
-
-
Save Lavanyagaur22/6fe9d5496f48c3200a38ba8383772ca3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MainActivity : AppCompatActivity() { | |
private val API_KEY = "dcf6227d8ecf4968b4e1a1b5fc1c483b" | |
private val executor = Executors.newSingleThreadExecutor() | |
private val TAG = javaClass.simpleName | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
/*In the MainActivity class, initialize Fritz SDK. | |
*/ | |
Fritz.configure(this, API_KEY) | |
/*Instead of calling `startCamera()` on the main thread, we use `viewFinder.post { ... }` | |
to make sure that `viewFinder` has already been inflated into the view when `startCamera()` is called. | |
*/ | |
view_finder.post { | |
startCamera() | |
} | |
} | |
//Function that creates and displays the camera preview | |
private fun startCamera() { | |
//Specify the configuration for the preview | |
val previewConfig = PreviewConfig.Builder() | |
.apply { | |
//Set the resolution of the captured image | |
setTargetResolution(Size(1920, 1080)) | |
} | |
.build() | |
//Generate a preview | |
val preview = Preview(previewConfig) | |
//Add a listener to update preview automatically | |
preview.setOnPreviewOutputUpdateListener { | |
val parent = view_finder.parent as ViewGroup | |
//Remove thr old preview | |
parent.removeView(view_finder) | |
//Add the new preview | |
parent.addView(view_finder, 0) | |
view_finder.surfaceTexture = it.surfaceTexture | |
} | |
val analyzerConfig = ImageAnalysisConfig.Builder().apply { | |
// In our analysis, we care more about the latest image than | |
// analyzing *every* image | |
setImageReaderMode( | |
ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE | |
) | |
}.build() | |
val imageAnalysis = ImageAnalysis(analyzerConfig).apply { | |
setAnalyzer(executor, ImageProcessor()) | |
} | |
/* Bind use cases to lifecycle. If Android Studio complains about "this" | |
being not a LifecycleOwner, try rebuilding the project or updating the appcompat dependency to | |
version 1.1.0 or higher. | |
*/ | |
CameraX.bindToLifecycle(this, preview, imageAnalysis) | |
} | |
/* It allows us to define a custom class implementing the ImageAnalysis.Analyzer interface, | |
which will be called with incoming camera frames. | |
*/ | |
inner class ImageProcessor : ImageAnalysis.Analyzer { | |
var predictor: FritzVisionStylePredictor? = null | |
val TAG = javaClass.simpleName | |
override fun analyze(image: ImageProxy?, rotationDegrees: Int) { | |
//Handle all the ML logic here | |
val mediaImage = image?.image | |
val imageRotation = ImageRotation.getFromValue(rotationDegrees) | |
val visionImage = FritzVisionImage.fromMediaImage(mediaImage, imageRotation) | |
val managedModel = PaintingManagedModels.BICENTENNIAL_PRINT_MANAGED_MODEL | |
/* Load the FritzVision Style Transfer Predictor | |
*/ | |
FritzVision.StyleTransfer.loadPredictor( | |
managedModel, object : PredictorStatusListener<FritzVisionStylePredictor> { | |
override fun onPredictorReady(stylePredictor: FritzVisionStylePredictor?) { | |
Log.d(TAG, "Style Transfer predictor is ready") | |
predictor = stylePredictor | |
} | |
} | |
) | |
/* Get the FritzVisionStyleResult by running the predictor on the vision image. | |
*/ | |
val styleResult = predictor?.predict(visionImage) | |
/* Perform UI operations accordingly. | |
*/ | |
runOnUiThread { | |
Log.d(TAG, "UI thread") | |
val bitmap = styleResult?.toBitmap() | |
image_view.setImageBitmap(bitmap) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment