Skip to content

Commit

Permalink
Implement MinimumInterval property
Browse files Browse the repository at this point in the history
Change-Id: I26cb0129a8e131f77dad45f1d02440e79c1c2880
  • Loading branch information
ewpatton committed Mar 12, 2021
1 parent 0d7fcaf commit 773f87d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public final class PersonalImageClassifier extends AndroidNonvisibleComponent
private List<String> labels = Collections.emptyList();
private String modelPath = null;
private boolean running = false;
private int minClassTime = 0;

public PersonalImageClassifier(final Form form) {
super(form);
Expand Down Expand Up @@ -286,6 +287,21 @@ public boolean Running() {
return running;
}

@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER,
defaultValue = "0")
@SimpleProperty(category = PropertyCategory.BEHAVIOR)
public void MinimumInterval(int interval) {
minClassTime = interval;
if (webview != null) {
webview.evaluateJavascript("minClassTime = " + interval + ";", null);
}
}

@SimpleProperty
public int MinimumInterval() {
return minClassTime;
}

@SimpleFunction(description = "Performs classification on the image at the given path and triggers the GotClassification event when classification is finished successfully.")
public void ClassifyImageData(final String image) {
assertWebView("ClassifyImageData");
Expand Down Expand Up @@ -348,6 +364,7 @@ public void StopContinuousClassification() {
@SimpleEvent(description = "Event indicating that the classifier is ready.")
public void ClassifierReady() {
InputMode(inputMode);
MinimumInterval(minClassTime);
EventDispatcher.dispatchEvent(this, "ClassifierReady");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ img.style.display = "block";
let frontFacing = false;
let isVideoMode = false;
let isRunning = false;
let minClassTime = 0;
let lastClassification = new Date();
let webcamHolder = document.getElementById('webcam-box');
let video = /** @type {HTMLVideoElement} */ (document.getElementById('webcam'));
webcamHolder.style.display = 'none';
Expand Down Expand Up @@ -260,7 +262,13 @@ function cvcHandler() {
if (!isRunning || !isVideoMode) {
return;
}
predict(video, true).then(() => setTimeout(cvcHandler, 16));
let now = new Date();
if (now.getTime() - lastClassification.getTime() > minClassTime) {
lastClassification = now;
predict(video, true).then(() => requestAnimationFrame(cvcHandler));
} else {
requestAnimationFrame(cvcHandler);
}
}

// Called from PersonalImageClassifier.java
Expand Down

0 comments on commit 773f87d

Please sign in to comment.