QR code color, eval wait for input option, optional tfjs AI models url

This commit is contained in:
Michal Kolář
2024-04-10 13:47:41 +02:00
committed by Stanislav Fifik
parent 315d1acc9b
commit a3b2b84b69
5 changed files with 55 additions and 9 deletions

View File

@@ -1,7 +1,8 @@
import {
BooleanVar,
ImageVar,
NumberVar
NumberVar,
StringVar,
} from '../io/AbstractIOSet';
import Node from "../Node";
import '@tensorflow/tfjs-backend-webgl';
@@ -66,6 +67,9 @@ const inputs = {
min: 0,
max: 1,
} as NumberVar,
modelUrl: {
type: 'String',
} as StringVar,
...(flow([
bodyParts => keyBy(bodyParts, (bodyPart) => bodyPart),
bodyParts => mapValues(bodyParts, () => ({
@@ -106,6 +110,7 @@ export default class BodyPartSegmentation extends Node<typeof inputs, typeof out
runtime: 'tfjs',
modelType: 'general',
segmentBodyParts: true,
modelUrl: this.in.modelUrl.value ? this.in.modelUrl.value : undefined,
} as BodyPixSegmentationConfig;
this.segmenter = await createSegmenter(model, segmenterConfig);
}

View File

@@ -1,6 +1,7 @@
import {
ImageVar,
NumberVar
NumberVar,
StringVar,
} from '../io/AbstractIOSet';
import Node from "../Node";
import '@tensorflow/tfjs-backend-webgl';
@@ -35,6 +36,9 @@ const inputs = {
min: 0,
max: 1,
} as NumberVar,
modelUrl: {
type: 'String',
} as StringVar,
}
const outputs = {
@@ -66,7 +70,8 @@ export default class BodySegmentation extends Node<typeof inputs, typeof outputs
const model = SupportedModels.MediaPipeSelfieSegmentation;
const segmenterConfig = {
runtime: 'tfjs',
modelType: 'general'
modelType: 'general',
modelUrl: this.in.modelUrl.value ? this.in.modelUrl.value : undefined,
} as MediaPipeSelfieSegmentationMediaPipeModelConfig;
this.segmenter = await createSegmenter(model, segmenterConfig);
}

View File

@@ -1,13 +1,15 @@
import {
ImageVar,
NumberVar, VariableValueType
NumberVar,
StringVar,
VariableValueType,
} from '../io/AbstractIOSet';
import Node from "../Node";
import '@tensorflow/tfjs-backend-webgl';
import {Face,
FaceDetector,
FaceDetectorInput,
MediaPipeFaceDetectorModelConfig,
MediaPipeFaceDetectorMediaPipeModelConfig,
SupportedModels,
createDetector,
} from '@tensorflow-models/face-detection';
@@ -22,6 +24,9 @@ const inputs = {
image: {
type: 'Image'
} as ImageVar,
modelUrl: {
type: 'String',
} as StringVar,
}
const outputs = {
@@ -97,7 +102,8 @@ export default class FaceFeaturePosition extends Node<typeof inputs, typeof outp
maxFaces: 1,
modelType: 'short',
runtime: 'tfjs',
} as MediaPipeFaceDetectorModelConfig;
detectorModelUrl: this.in.modelUrl.value ? this.in.modelUrl.value : undefined,
} as MediaPipeFaceDetectorMediaPipeModelConfig;
this.detector = await createDetector(model, detectorConfig);
}

View File

@@ -1,11 +1,16 @@
import Node from './Node';
import {
ImageVar,
NumberVar,
StringVar,
BooleanVar,
} from './io/AbstractIOSet';
import get from 'lodash/get';
const inputs = {
image: {
type: 'Image'
} as ImageVar,
i0: {
type: 'Number',
default: 0,
@@ -41,16 +46,23 @@ const inputs = {
formula: {
type: 'String',
default: '',
} as StringVar
} as StringVar,
waitForInput: {
type: 'Boolean',
default: true,
} as BooleanVar
};
const outputs = {
image: {
type: 'Image'
} as ImageVar,
result: {
type: 'Number'
} as NumberVar,
}
export default class NumberBinaryOperation extends Node<typeof inputs, typeof outputs> {
export default class Eval extends Node<typeof inputs, typeof outputs> {
constructor() {
super('Eval', inputs, outputs);
}
@@ -61,12 +73,20 @@ export default class NumberBinaryOperation extends Node<typeof inputs, typeof ou
)
}
hasAllConnectedInputsValue() {
if (this.in.waitForInput.value) {
return super.hasAllConnectedInputsValue();
}
return true;
}
evaluate() {
try {
this.out.result.value = eval(this.interpolate());
} catch (e) {
console.error(e);
}
this.out.image.value = this.in.image.value;
}
async _update() {

View File

@@ -2,6 +2,7 @@ import {
ImageVar,
NumberVar,
StringVar,
ColorVar,
} from './io/AbstractIOSet';
import Node from './Node';
import {canvasPool2D} from '../canvas/CanvasPool';
@@ -30,6 +31,10 @@ const inputs = {
type: 'Number',
min: 0,
} as NumberVar,
color: {
type: 'Color',
default: '#000000',
} as ColorVar,
}
const outputs = {
@@ -59,7 +64,12 @@ export default class QRCodeGenerator extends Node<typeof inputs, typeof outputs>
const encodeHintTypeMap = new Map<EncodeHintType, any>();
encodeHintTypeMap.set(EncodeHintType.MARGIN, this.in.margin.value);
encodeHintTypeMap.set(EncodeHintType.ERROR_CORRECTION, QRCodeDecoderErrorCorrectionLevel[this.in.correction.value]);
const svgElement = this.writer.write(this.in.text.value, this.in.size.value, this.in.size.value, encodeHintTypeMap);
const svgElement: SVGSVGElement = this.writer.write(this.in.text.value, this.in.size.value, this.in.size.value, encodeHintTypeMap);
for(const element of svgElement.querySelectorAll('[fill]')) {
element.setAttribute('fill', this.in.color.value);
}
const canvas = canvasPool2D.createCanvas();
canvas.width = this.in.size.value;
canvas.height = this.in.size.value;