🚧 Load canvas from frame mesasge

main
huchenlei 2023-05-25 23:52:28 -04:00
parent e0cf5c6c3b
commit 9d7743f75e
2 changed files with 66 additions and 1 deletions

View File

@ -41,6 +41,18 @@ interface AppData {
// The corresponding OpenposeObject(Hand/Face) that the user has the // The corresponding OpenposeObject(Hand/Face) that the user has the
// collapse element expanded. // collapse element expanded.
activeBodyPart: OpenposeBodyPart | undefined; activeBodyPart: OpenposeBodyPart | undefined;
// The modal id to post message back to.
modalId: string | undefined;
};
/**
* The frame message from the main frame (ControlNet).
*/
interface IncomingFrameMessage {
modalId: string;
imageURL: string;
poseURL: string;
}; };
const default_body_keypoints: [number, number, number][] = [ const default_body_keypoints: [number, number, number][] = [
@ -272,6 +284,24 @@ const default_face_keypoints: [number, number, number][] = [];
// identity_metrics * point == point. // identity_metrics * point == point.
const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0]; const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
function parseDataURLtoJSON(dataURL: string): any {
const data = dataURL.split(',')[1]; // Extract the data portion
const decodedData = atob(data); // Decode the data
const json = JSON.parse(decodedData); // Parse the decoded data as JSON
return json;
}
async function calculateHash(s: string): Promise<string> {
const encoder = new TextEncoder();
const data = encoder.encode(s);
const buffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(buffer));
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hashHex;
}
export default defineComponent({ export default defineComponent({
data(): AppData { data(): AppData {
return { return {
@ -286,6 +316,7 @@ export default defineComponent({
canvasImageMap: new Map<string, fabric.Image>(), canvasImageMap: new Map<string, fabric.Image>(),
activePersonId: undefined, activePersonId: undefined,
activeBodyPart: undefined, activeBodyPart: undefined,
modalId: undefined,
}; };
}, },
setup() { setup() {
@ -352,6 +383,12 @@ export default defineComponent({
this.canvas.on('selection:created', selectionHandler); this.canvas.on('selection:created', selectionHandler);
this.canvas.on('selection:cleared', selectionHandler); this.canvas.on('selection:cleared', selectionHandler);
this.canvas.on('selection:updated', selectionHandler); this.canvas.on('selection:updated', selectionHandler);
// Handle incoming frame message.
window.addEventListener('message', (event) => {
const message = event.data as IncomingFrameMessage;
this.loadCanvasFromFrameMessage(message);
});
}); });
}, },
methods: { methods: {
@ -677,7 +714,21 @@ export default defineComponent({
this.resizeCanvas(this.canvasWidth, this.canvasHeight); this.resizeCanvas(this.canvasWidth, this.canvasHeight);
this.parseOpenposeJson(poseJson).forEach(person => this.addPerson(person)); this.parseOpenposeJson(poseJson).forEach(person => this.addPerson(person));
}, },
loadFromRequestParams() { /**
* Clear everything on the canvas.
*/
clearCanvas() {
// Remove all people.
[...this.people.values()].forEach(person => {
this.removePerson(person);
});
// Remove all background images.
this.uploadedImageList.forEach(image => {
this.handleRemoveImage(image);
});
},
loadCanvasFromRequestParams() {
this.clearCanvas();
const data = window.dataFromServer; const data = window.dataFromServer;
if (_.isEmpty(data)) { if (_.isEmpty(data)) {
return; return;
@ -693,6 +744,17 @@ export default defineComponent({
this.loadPeopleFromJson(poseJson); this.loadPeopleFromJson(poseJson);
this.loadBackgroundImageFromURL(data.image_url); this.loadBackgroundImageFromURL(data.image_url);
}, },
async loadCanvasFromFrameMessage(message: IncomingFrameMessage) {
this.clearCanvas();
this.uploadedImageList.push({
locked: false,
scale: 1.0,
name: 'imageFromURL',
uid: await calculateHash(message.imageURL),
} as LockableUploadFile);
this.loadBackgroundImageFromURL(message.imageURL);
this.loadPeopleFromJson(parseDataURLtoJSON(message.poseURL) as IOpenposeJson);
},
downloadCanvasAsJson() { downloadCanvasAsJson() {
const data = { const data = {
people: [...this.people.values()].map(person => person.toJson()), people: [...this.people.values()].map(person => person.toJson()),

View File

@ -1,5 +1,8 @@
{ {
"files": [], "files": [],
"compilerOptions": {
"lib": ["es2017"],
},
"references": [ "references": [
{ {
"path": "./tsconfig.node.json" "path": "./tsconfig.node.json"