Skip to content

Instantly share code, notes, and snippets.

View Lavanyagaur22's full-sized avatar
🚩

Lavanya gaur Lavanyagaur22

🚩
View GitHub Profile
class ImageProcessor : ImageAnalysis.Analyzer {
var predictor: FritzVisionLabelPredictor? = 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)
var predictor: FritzVisionLabelPredictor? = null
val managedModel = ImageLabelManagedModelFast()
FritzVision.ImageLabeling.loadPredictor(
managedModel,
object : PredictorStatusListener<FritzVisionLabelPredictor> {
override fun onPredictorReady(p0: FritzVisionLabelPredictor?) {
Log.d(TAG, "Image Labeling predictor is ready")
predictor = p0
}
val mediaImage = image?.image
val imageRotation = ImageRotation.getFromValue(rotationDegrees)
val visionImage = FritzVisionImage.fromMediaImage(mediaImage, imageRotation)
val labelResult = predictor?.predict(visionImage)
runOnUiThread {
labelResult?.resultString?.let {
val sname = it.split(":")
Log.e(TAG, it)
// Get the system service for the camera manager
val manager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
// Gets the first camera id
var cameraId = manager.getCameraIdList().get(0)
// Determine the rotation on the FritzVisionImage from the camera orientaion and the device rotation.
// "this" refers to the calling Context (Application, Activity, etc)
var imageRotationFromCamera = FritzVisionOrientation.getImageRotationFromCamera(this, cameraId)
package com.lavanya.imagelabeling
import ai.fritz.core.Fritz
import ai.fritz.vision.FritzVision
import ai.fritz.vision.FritzVisionImage
import ai.fritz.vision.ImageRotation
import ai.fritz.vision.PredictorStatusListener
import ai.fritz.vision.imagelabeling.FritzVisionLabelPredictor
import ai.fritz.vision.imagelabeling.ImageLabelManagedModelFast
import android.os.Bundle
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextureView
android:id="@+id/view_finder"
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.
/* It allows us to define a custom class implementing the ImageAnalysis.Analyzer interface,
which will be called with incoming camera frames.
*/
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
var predictor: FritzVisionStylePredictor? = null
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
val mediaImage = image?.image
val imageRotation = ImageRotation.getFromValue(rotationDegrees)
val visionImage = FritzVisionImage.fromMediaImage(mediaImage, imageRotation)
/* Get the FritzVisionStyleResult by running the predictor on the vision image.
*/
val styleResult = predictor?.predict(visionImage)
/* Perform UI operations accordingly.
*/