Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switchcamera #36

Open
wants to merge 4 commits into
base: extension/posenet
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Canvas;
import com.google.appinventor.components.runtime.Deleteable;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.Form;
Expand All @@ -52,7 +53,7 @@
import org.json.JSONException;
import org.json.JSONObject;

@DesignerComponent(version = 20200304,
@DesignerComponent(version = 20200309,
category = ComponentCategory.EXTENSION,
description = "An extension that embeds a Posenet model.",
iconName = "aiwebres/icon.png",
Expand All @@ -73,6 +74,7 @@ public class PosenetExtension extends AndroidNonvisibleComponent
private static final String FRONT_CAMERA = "Front";

private WebView webview = null;
private Canvas canvas = null;
private final Map<String, YailList> keyPoints = new ConcurrentHashMap<>();
private double minPoseConfidence = 0.1;
private double minPartConfidence = 0.5;
Expand Down Expand Up @@ -190,6 +192,16 @@ public void WebViewer(WebViewer webviewer) {
}
}

@SuppressWarnings("squid:S00100")
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_COMPONENT
+ ":com.google.appinventor.components.runtime.Canvas")
@SimpleProperty(userVisible = false)
public void Canvas(Canvas canvas) {
if (canvas != null) {
this.canvas = canvas;
}
}

public void Initialize() {
if (webview != null) {
initialized = true;
Expand Down Expand Up @@ -400,6 +412,10 @@ public boolean Enabled() {
@SuppressWarnings("squid:S00100")
@SimpleEvent(description = "Event indicating that the classifier is ready.")
public void ModelReady() {
if (canvas != null) {
SetVideoHeight(canvas.Height());
SetVideoWidth(canvas.Width());
}
EventDispatcher.dispatchEvent(this, "ModelReady");
}

Expand All @@ -419,6 +435,7 @@ public void PoseUpdated() {
@SuppressWarnings("squid:S00100")
@SimpleEvent(description = "Event indicating that a new video frame is ready. ")
public void VideoUpdated() {
canvas.BackgroundImageinBase64(BackgroundImage);
EventDispatcher.dispatchEvent(this, "VideoUpdated");
}

Expand All @@ -444,6 +461,16 @@ public String UseCamera() {
return cameraMode;
}

@SimpleProperty (description = "Configure video width.")
public void SetVideoWidth(int width) {
ellelili2025 marked this conversation as resolved.
Show resolved Hide resolved
webview.evaluateJavascript("setVideoWidth(" + width + ");", null);
}

@SimpleProperty (description = "Configure video height.")
public void SetVideoHeight(int height) {
ellelili2025 marked this conversation as resolved.
Show resolved Hide resolved
webview.evaluateJavascript("setVideoHeight(" + height + ");", null);
}

private static void requestHardwareAcceleration(Activity activity) {
activity.getWindow().setFlags(LayoutParams.FLAG_HARDWARE_ACCELERATED,
LayoutParams.FLAG_HARDWARE_ACCELERATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ console.log("Posenet Extension using tfjs-converter version " + tf.version_conve

const ERROR_WEBVIEW_NO_MEDIA = 400;
const ERROR_MODEL_LOAD = 401;
const videoWidth = 300;
const videoHeight = 250;

const ERRORS = {
ERROR_WEBVIEW_NO_MEDIA: "WebView does not support navigator.mediaDevices",
ERROR_MODEL_LOAD: "Unable to load model"
};

let videoWidth = 300;
let videoHeight = 250;
let forwardCamera = true;
let running = false;
let stream = null;

async function setupCamera() {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
Expand All @@ -29,13 +29,18 @@ async function setupCamera() {
const video = document.getElementById('video');
video.width = videoWidth;
video.height = videoHeight;

video.srcObject = await navigator.mediaDevices.getUserMedia({
'audio': false,
'video': {
facingMode: forwardCamera ? 'user' : 'environment'
}
if (stream != null) {
stream.getTracks().forEach(t => {
t.stop();
});
}
stream = await navigator.mediaDevices.getUserMedia({
'audio': false,
'video': {
facingMode: forwardCamera ? 'user' : 'environment'
}
});
video.srcObject = stream;

return new Promise((resolve) => {
video.onloadedmetadata = () => {
Expand Down Expand Up @@ -106,7 +111,7 @@ async function loadModel() {
});
} catch (e) {
PosenetExtension.error(ERROR_MODEL_LOAD,
ERRORS[ERROR_MODEL_LOAD]);
ERRORS.ERROR_MODEL_LOAD);
throw e;
}
}
Expand Down Expand Up @@ -152,6 +157,14 @@ function setCameraFacingMode(useForward) {
})
}

function setVideoWidth(width) {
videoWidth = width;
}

function setVideoHeight(height) {
videoHeight = height;
}

// noinspection JSUnresolvedVariable
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
Expand Down