This is an android camerax object detection example. This example is written in Kotlin and supports Androidx. This example uses Firebase ML librar for object detection.
Requirements
This project, because it uses CameraX requires android API Level 21 and above.
Build.gradle
Go to your app level build.gradle and add dependencies as follows:
After adding dependencies apply the google services plugin:
apply plugin: "com.google.gms.google-services"
ObjectDetectionAnalyzer
Create a class implementing the ImageAnalysis.Analyzer:
class ObjectDetectionAnalyzer(private val overlay: GraphicOverlay) : ImageAnalysis.Analyzer {
The graphic overlay is a custom view object created in the project to overlay the image.
Prepare instance fields:
@GuardedBy("this")
private var processingImage: Image? = null
private val detector: FirebaseVisionObjectDetector
@GuardedBy("this")
@FirebaseVisionImageMetadata.Rotation
var rotation = FirebaseVisionImageMetadata.ROTATION_90
@GuardedBy("this")
var scaledWidth = 0
@GuardedBy("this")
var scaledHeight = 0
NB/= GuarededBy is an annotation that denotes that the annotated method or field can only be accessed when holding the referenced lock. Read more here.
Create an inti to initialize some of our Firebase ML classes:
init {
Inside the init initialize the FirebaseVisionObjectDetectorOptions:
val options = FirebaseVisionObjectDetectorOptions.Builder()
.setDetectorMode(FirebaseVisionObjectDetectorOptions.STREAM_MODE)
.enableClassification()
.build()
I get the rectangle in the correct place if I keep my Object in the center of the camera view, but when I move my object left the rectangle moves to the right, and when I move my object to the right, the rectangle moves left.
Oclemy
This is an android camerax object detection example. This example is written in Kotlin and supports Androidx. This example uses Firebase ML librar for object detection.
Requirements
This project, because it uses CameraX requires android API Level 21 and above.
Build.gradle
Go to your app level build.gradle and add dependencies as follows:
After adding dependencies apply the google services plugin:
ObjectDetectionAnalyzer
Create a class implementing the ImageAnalysis.Analyzer:
The graphic overlay is a custom view object created in the project to overlay the image.
Prepare instance fields:
NB/=
GuarededBy
is an annotation that denotes that the annotated method or field can only be accessed when holding the referenced lock. Read more here.Create an inti to initialize some of our Firebase ML classes:
Inside the
init
initialize the FirebaseVisionObjectDetectorOptions:then FirebaseVisionObjectDetector:
Create a method to process the latest frame:
Then override the
analyze()
method:Special thanks to @yanzm for creating this project.
Find the whole project in the download.
Hi,
CameraX.LensFacing.FRONT (FRONT Camera)
I get the rectangle in the correct place if I keep my Object in the center of the camera view, but when I move my object left the rectangle moves to the right, and when I move my object to the right, the rectangle moves left.