Skip to content

Commit 5fba61c

Browse files
committed
Calculate average processing time for 20 processings, display number of analysis per seconds.
1 parent de20d81 commit 5fba61c

File tree

4 files changed

+64
-33
lines changed

4 files changed

+64
-33
lines changed

AI_MultiBarcodes_Capture/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ android {
3939
}
4040

4141
buildTypes {
42+
debug {
43+
// Disable native debugging to prevent dual process launch
44+
// Set to true only when you need to debug native C++ code
45+
isJniDebuggable = false
46+
packaging {
47+
jniLibs {
48+
keepDebugSymbols -= setOf("**/*.so")
49+
}
50+
}
51+
}
4252
release {
4353
isMinifyEnabled = false
4454
proguardFiles(

AI_MultiBarcodes_Capture/src/main/java/com/zebra/ai_multibarcodes_capture/barcodedecoder/BarcodeAnalyzer.java

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ public interface DetectionCallback {
6464

6565
/**
6666
* Interface for receiving analysis timing data.
67-
* Implement this interface to display performance metrics (FPS, analysis time).
67+
* Implement this interface to display performance metrics (analysis count, analysis time).
6868
*/
6969
public interface AnalysisTimingCallback {
7070
/**
7171
* Called with timing information after each analysis completes.
7272
* @param analysisTimeMs The time in milliseconds taken to analyze the frame
73-
* @param fps The current frames per second (analysis rate)
73+
* @param analysisPerSecond The number of analyses completed in the last second
7474
*/
75-
void onAnalysisTiming(long analysisTimeMs, float fps);
75+
void onAnalysisTiming(long analysisTimeMs, int analysisPerSecond);
7676
}
7777

7878
private static final String TAG = "BarcodeAnalyzer";
@@ -97,12 +97,17 @@ public interface AnalysisTimingCallback {
9797
@Nullable
9898
private volatile AnalysisTimingCallback timingCallback = null;
9999
private volatile boolean timingEnabled = false;
100-
private volatile long analysisStartTimeNanos = 0;
101-
private volatile long lastAnalysisTimeMs = 0;
102-
private volatile long fpsWindowStartTime = 0;
100+
private volatile long apsWindowStartTime = 0;
103101
private volatile int frameCountInWindow = 0;
104-
private volatile float currentFps = 0.0f;
105-
private static final long FPS_WINDOW_MS = 1000; // Calculate FPS over 1 second window
102+
private volatile int currentAnalysisPerSecond = 0;
103+
private static final long APS_WINDOW_MS = 1000; // Calculate APS over 1 second window
104+
105+
// Average analysis time calculation
106+
private static final int AVG_WINDOW_SIZE = 20; // Calculate average over 20 analyses
107+
private final long[] analysisTimesBuffer = new long[AVG_WINDOW_SIZE];
108+
private volatile int analysisTimesIndex = 0;
109+
private volatile int analysisTimesCount = 0;
110+
private volatile long averageAnalysisTimeMs = 0;
106111

107112
/**
108113
* Constructs a new BarcodeAnalyzer with the specified callback and barcode decoder.
@@ -183,27 +188,39 @@ public void analyze(@NonNull ImageProxy image) {
183188
if (trackTiming) {
184189
long endTimeNanos = System.nanoTime();
185190
long analysisTimeMs = (endTimeNanos - startTimeNanos) / 1_000_000;
186-
lastAnalysisTimeMs = analysisTimeMs;
187191

188-
// Update FPS calculation
192+
// Update rolling average for analysis time (over 20 samples)
193+
analysisTimesBuffer[analysisTimesIndex] = analysisTimeMs;
194+
analysisTimesIndex = (analysisTimesIndex + 1) % AVG_WINDOW_SIZE;
195+
if (analysisTimesCount < AVG_WINDOW_SIZE) {
196+
analysisTimesCount++;
197+
}
198+
// Calculate average
199+
long sum = 0;
200+
for (int i = 0; i < analysisTimesCount; i++) {
201+
sum += analysisTimesBuffer[i];
202+
}
203+
averageAnalysisTimeMs = sum / analysisTimesCount;
204+
205+
// Update analysis per second calculation
189206
long currentTimeMs = System.currentTimeMillis();
190207
frameCountInWindow++;
191208

192-
if (fpsWindowStartTime == 0) {
193-
fpsWindowStartTime = currentTimeMs;
209+
if (apsWindowStartTime == 0) {
210+
apsWindowStartTime = currentTimeMs;
194211
} else {
195-
long elapsedMs = currentTimeMs - fpsWindowStartTime;
196-
if (elapsedMs >= FPS_WINDOW_MS) {
197-
// Calculate FPS and reset window
198-
currentFps = (frameCountInWindow * 1000.0f) / elapsedMs;
199-
fpsWindowStartTime = currentTimeMs;
212+
long elapsedMs = currentTimeMs - apsWindowStartTime;
213+
if (elapsedMs >= APS_WINDOW_MS) {
214+
// Store count and reset window
215+
currentAnalysisPerSecond = frameCountInWindow;
216+
apsWindowStartTime = currentTimeMs;
200217
frameCountInWindow = 0;
201218
}
202219
}
203220

204-
// Notify callback with timing data
221+
// Notify callback with average timing data
205222
if (timingCallback != null) {
206-
timingCallback.onAnalysisTiming(analysisTimeMs, currentFps);
223+
timingCallback.onAnalysisTiming(averageAnalysisTimeMs, currentAnalysisPerSecond);
207224
}
208225
}
209226

@@ -513,17 +530,21 @@ public void setTimingCallback(@Nullable AnalysisTimingCallback callback) {
513530

514531
/**
515532
* Enables or disables timing tracking for performance monitoring.
516-
* When enabled, the analyzer will calculate FPS and analysis time for each frame.
533+
* When enabled, the analyzer will calculate analysis per second and average analysis time.
517534
*
518535
* @param enabled true to enable timing tracking, false to disable
519536
*/
520537
public void setTimingEnabled(boolean enabled) {
521538
this.timingEnabled = enabled;
522539
if (enabled) {
523540
// Reset timing state when enabling
524-
fpsWindowStartTime = 0;
541+
apsWindowStartTime = 0;
525542
frameCountInWindow = 0;
526-
currentFps = 0.0f;
543+
currentAnalysisPerSecond = 0;
544+
// Reset average calculation
545+
analysisTimesIndex = 0;
546+
analysisTimesCount = 0;
547+
averageAnalysisTimeMs = 0;
527548
}
528549
}
529550

@@ -537,20 +558,20 @@ public boolean isTimingEnabled() {
537558
}
538559

539560
/**
540-
* Gets the last calculated analysis time.
561+
* Gets the average analysis time over the last 20 analyses.
541562
*
542-
* @return The time in milliseconds taken to analyze the last frame
563+
* @return The average time in milliseconds taken to analyze frames
543564
*/
544-
public long getLastAnalysisTimeMs() {
545-
return lastAnalysisTimeMs;
565+
public long getAverageAnalysisTimeMs() {
566+
return averageAnalysisTimeMs;
546567
}
547568

548569
/**
549-
* Gets the current frames per second rate.
570+
* Gets the current number of analyses per second.
550571
*
551-
* @return The current analysis rate in frames per second
572+
* @return The number of analyses completed in the last second
552573
*/
553-
public float getCurrentFps() {
554-
return currentFps;
574+
public int getCurrentAnalysisPerSecond() {
575+
return currentAnalysisPerSecond;
555576
}
556577
}

AI_MultiBarcodes_Capture/src/main/java/com/zebra/ai_multibarcodes_capture/java/CameraXLivePreviewActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,11 @@ private void updateAnalyzerTimingCallback() {
705705
}
706706

707707
@Override
708-
public void onAnalysisTiming(long analysisTimeMs, float fps) {
708+
public void onAnalysisTiming(long analysisTimeMs, int analysisPerSecond) {
709709
// Update the overlay on UI thread
710710
runOnUiThread(() -> {
711711
if (analysisOverlay != null && displayAnalysisPerSecond) {
712-
String text = String.format(getString(R.string.analysis_overlay_format), fps, analysisTimeMs);
712+
String text = String.format(getString(R.string.analysis_overlay_format), analysisPerSecond, analysisTimeMs);
713713
analysisOverlay.setText(text);
714714
}
715715
});

AI_MultiBarcodes_Capture/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229
<string name="display_analysis_per_second_title">Performance Monitoring</string>
230230
<string name="display_analysis_per_second_description">Display an overlay on the camera preview showing real-time performance metrics: number of image analyses per second and processing time in milliseconds.</string>
231231
<string name="display_analysis_per_second_checkbox">Show analysis rate overlay</string>
232-
<string name="analysis_overlay_format">%1$.1f FPS\n%2$d ms</string>
232+
<string name="analysis_overlay_format">%1$d APS\n%2$d ms</string>
233233

234234
<!-- Dialog Messages (French -> English) -->
235235
<string name="rename">Rename</string>

0 commit comments

Comments
 (0)