Skip to content

Commit

Permalink
Implemented requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sufeenshaikh committed Aug 28, 2024
1 parent b750646 commit 913eb7f
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 296 deletions.
4 changes: 1 addition & 3 deletions appinventor/components-ios/src/ImageSprite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ open class ImageSprite: Sprite {
let doubleV = Double(components[1].trimmingCharacters(in: .whitespaces))
super.U = doubleU!
super.V = doubleV!

}

//for setOriginX
Expand All @@ -80,8 +79,7 @@ open class ImageSprite: Sprite {
super.V = v
}
}



@objc open var Rotates: Bool {
get {
return _rotates
Expand Down
8 changes: 4 additions & 4 deletions appinventor/components-ios/src/Sprite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,15 @@ open class Sprite: ViewComponent, UIGestureRecognizerDelegate {
Heading = 180 - normalizedAngle
} else if ((e == Direction.north
&& normalizedAngle > 0 && normalizedAngle < 180)
|| (e == Direction.south && normalizedAngle > 180)) {
|| (e == Direction.south && normalizedAngle > 180)) {
Heading = 360 - normalizedAngle
} else if ((e == Direction.northeast
&& normalizedAngle > 0 && normalizedAngle < 90)
|| (e == Direction.northwest
|| (e == Direction.northwest
&& normalizedAngle > 90 && normalizedAngle < 180)
|| (e == Direction.southwest
|| (e == Direction.southwest
&& normalizedAngle > 180 && normalizedAngle < 270)
|| (e == Direction.southeast && normalizedAngle > 270)) {
|| (e == Direction.southeast && normalizedAngle > 270)) {
Heading = 180 + normalizedAngle
}
}
Expand Down
106 changes: 53 additions & 53 deletions appinventor/components-ios/src/assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,53 @@ let running = false;

function processImage(base64Image) {
alert("Received base64 image string: " + base64Image);
console.log("Received base64 image string:", base64Image);
console.log("Received base64 image string:", base64Image);
//console.log("Received base64 image string:", base64String)
const img = new Image();
img.src = 'data:image/jpeg;base64,' + base64Image;

img.onload = async function () {
const canvas = document.getElementById('output');
const ctx = canvas.getContext('2d');

// Set the canvas size to match the image
canvas.width = img.width;
canvas.height = img.height;

// Draw the image on the canvas
ctx.drawImage(img, 0, 0, img.width, img.height);

// runClassifier(img, net);

// runClassifier(img, net);
// Load the FaceMesh model
const net = await facemesh.load();

const predictions = await net.estimateFaces({input: canvas});
processPredictions(predictions);

runClassifier(video, net)
FaceExtension.reportResult(JSON.stringify(newObj));
}

runClassifier(video, net)
FaceExtension.reportResult(JSON.stringify(newObj));
}
}

async function setupCamera() {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
PosenetExtension.error(ERROR_WEBVIEW_NO_MEDIA,
ERRORS[ERROR_WEBVIEW_NO_MEDIA]);
ERRORS[ERROR_WEBVIEW_NO_MEDIA]);
return;
}

const video = document.getElementById('video');
video.width = 0;
video.height = 0;

video.srcObject = await navigator.mediaDevices.getUserMedia({
'audio': false,
'video': {
facingMode: forwardCamera ? 'user' : 'environment'
facingMode: forwardCamera ? 'user' : 'environment'
}
});

return new Promise((resolve) => {
video.onloadedmetadata = () => {
resolve(video);
Expand All @@ -87,21 +87,21 @@ let stop = false;
function runClassifier(video, net) {
const canvas = document.getElementById('output');
const ctx = canvas.getContext('2d');

canvas.width = videoWidth;
canvas.height = videoHeight;

async function classifyFrame() {
const predictions = await net.estimateFaces(video, false, true);

ctx.clearRect(0, 0, videoWidth, videoHeight);

ctx.save();
ctx.scale(forwardCamera ? -1 : 1, 1);
ctx.translate(forwardCamera ? -videoWidth : 0, 0);
ctx.drawImage(video, 0, 0, videoWidth, videoHeight);
ctx.restore();

if (predictions.length > 0) {
const rightCheek = predictions[0].scaledMesh[234];
const leftCheek = predictions[0].scaledMesh[454];
Expand All @@ -122,36 +122,36 @@ function runClassifier(video, net) {
const leftNoseTop = predictions[0].scaledMesh[412];
const leftEyebrow = predictions[0].scaledMesh[443];
const rightEyebrow = predictions[0].scaledMesh[223];

let allPoints = {};
for (let i = 0; i < predictions[0].scaledMesh.length; i++) {
allPoints[i.toString()] = {x : predictions[0].scaledMesh[i][0] + 480, y: predictions[0].scaledMesh[i][1] - 20};
}

const newObj = {"leftCheek" : {x: leftCheek[0] + 480, y: leftCheek[1]-20, z: leftCheek[2]},
"rightCheek" : {x : rightCheek[0] + 480, y: rightCheek[1]-20, z: rightCheek[2]},
"leftEyebrow" : {x: leftEyebrow[0] + 480, y: leftEyebrow[1]-20, z: leftEyebrow[2]},
"rightEyebrow" : {x : rightEyebrow[0] + 480, y: rightEyebrow[1]-20, z: rightEyebrow[2]},
"forehead": {x : forehead[0] + 480, y: forehead[1]-20, z: forehead[2]},
"chin": {x : chin[0] + 480, y: chin[1]-20, z: chin[2]},
"leftEyeInnerCorner": {x : leftEyeInnerCorner[0]+ 480, y: leftEyeInnerCorner[1]-20, z: leftEyeInnerCorner[2]},
"rightEyeInnerCorner": {x : rightEyeInnerCorner[0]+ 480, y: rightEyeInnerCorner[1]-20, z: rightEyeInnerCorner[2]},
"mouthTop": {x : mouthTop[0]+ 480, y: mouthTop[1]-20, z: mouthTop[2]},
"mouthBottom": {x : mouthBottom[0]+ 480, y: mouthBottom[1]-20, z: mouthBottom[2]},
"leftEyeTop": {x : leftEyeTop[0]+ 480, y: leftEyeTop[1]-20, z: leftEyeTop[2]},
"leftEyeBottom": {x : leftEyeBottom[0]+ 480, y: leftEyeBottom[1]-20, z: leftEyeBottom[2]},
"rightEyeTop": {x : rightEyeTop[0]+ 480, y: rightEyeTop[1]-20, z: rightEyeTop[2]},
"rightEyeBottom": {x : rightEyeBottom[0]+ 480, y: rightEyeBottom[1]-20, z: rightEyeBottom[2]},
"rightEarStart": {x : rightEarStart[0]+ 480, y: rightEarStart[1]-20, z: rightEarStart[2]},
"leftEarStart": {x : leftEarStart[0]+ 480, y: leftEarStart[1]-20, z: leftEarStart[2]},
"noseBottom": {x : noseBottom[0]+ 480, y: noseBottom[1]-20, z: noseBottom[2]},
"rightNoseTop": {x : rightNoseTop[0]+ 480, y: rightNoseTop[1]-20, z: rightNoseTop[2]},
"leftNoseTop": {x : leftNoseTop[0]+ 480, y: leftNoseTop[1]-20, z: leftNoseTop[2]},
"allPoints": allPoints
};

"rightCheek" : {x : rightCheek[0] + 480, y: rightCheek[1]-20, z: rightCheek[2]},
"leftEyebrow" : {x: leftEyebrow[0] + 480, y: leftEyebrow[1]-20, z: leftEyebrow[2]},
"rightEyebrow" : {x : rightEyebrow[0] + 480, y: rightEyebrow[1]-20, z: rightEyebrow[2]},
"forehead": {x : forehead[0] + 480, y: forehead[1]-20, z: forehead[2]},
"chin": {x : chin[0] + 480, y: chin[1]-20, z: chin[2]},
"leftEyeInnerCorner": {x : leftEyeInnerCorner[0]+ 480, y: leftEyeInnerCorner[1]-20, z: leftEyeInnerCorner[2]},
"rightEyeInnerCorner": {x : rightEyeInnerCorner[0]+ 480, y: rightEyeInnerCorner[1]-20, z: rightEyeInnerCorner[2]},
"mouthTop": {x : mouthTop[0]+ 480, y: mouthTop[1]-20, z: mouthTop[2]},
"mouthBottom": {x : mouthBottom[0]+ 480, y: mouthBottom[1]-20, z: mouthBottom[2]},
"leftEyeTop": {x : leftEyeTop[0]+ 480, y: leftEyeTop[1]-20, z: leftEyeTop[2]},
"leftEyeBottom": {x : leftEyeBottom[0]+ 480, y: leftEyeBottom[1]-20, z: leftEyeBottom[2]},
"rightEyeTop": {x : rightEyeTop[0]+ 480, y: rightEyeTop[1]-20, z: rightEyeTop[2]},
"rightEyeBottom": {x : rightEyeBottom[0]+ 480, y: rightEyeBottom[1]-20, z: rightEyeBottom[2]},
"rightEarStart": {x : rightEarStart[0]+ 480, y: rightEarStart[1]-20, z: rightEarStart[2]},
"leftEarStart": {x : leftEarStart[0]+ 480, y: leftEarStart[1]-20, z: leftEarStart[2]},
"noseBottom": {x : noseBottom[0]+ 480, y: noseBottom[1]-20, z: noseBottom[2]},
"rightNoseTop": {x : rightNoseTop[0]+ 480, y: rightNoseTop[1]-20, z: rightNoseTop[2]},
"leftNoseTop": {x : leftNoseTop[0]+ 480, y: leftNoseTop[1]-20, z: leftNoseTop[2]},
"allPoints": allPoints
};
FaceExtension.reportResult(JSON.stringify(newObj));

if (showMesh == "1") {
for (let i = 0; i < predictions[0].scaledMesh.length; i++) {
ctx.font = "3pt Calibri";
Expand All @@ -163,25 +163,25 @@ function runClassifier(video, net) {
}

}

const dataURL = canvas.toDataURL();
FaceExtension.reportImage(dataURL);


if (!stop) requestAnimationFrame(classifyFrame);
}

return classifyFrame();
}

async function loadModel() {
try {
return facemesh.load({
maxFaces: 1
maxFaces: 1
});
} catch (e) {
FaceExtension.error(ERROR_MODEL_LOAD,
ERRORS[ERROR_MODEL_LOAD]);
ERRORS[ERROR_MODEL_LOAD]);
throw e;
}
}
Expand All @@ -190,15 +190,15 @@ let net = null;

async function runModel() {
let video;

try {
video = await loadVideo();
} catch (e) {
FaceExtension.error(ERROR_WEBVIEW_NO_MEDIA,
ERRORS[ERROR_WEBVIEW_NO_MEDIA]);
ERRORS[ERROR_WEBVIEW_NO_MEDIA]);
throw e;
}

running = true;
return runClassifier(video, net);
}
Expand Down Expand Up @@ -237,7 +237,7 @@ function setCameraFacingMode(useForward) {

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

loadModel().then(model => {
net = model;
Expand Down
Loading

0 comments on commit 913eb7f

Please sign in to comment.