Skip to content

Instantly share code, notes, and snippets.

View MCarlomagno's full-sized avatar
🚀

Marcos Carlomagno MCarlomagno

🚀
  • OpenZeppelin
  • Rosario - Argentina
View GitHub Profile
await Tflite.loadModel(
model: "assets/mobilenet_v1_1.0_224.tflite",
labels: "assets/labels.txt",
);
_cameraController.startImageStream((img) async {
List<dynamic> recognitions = await Tflite.runModelOnFrame(
bytesList: img.planes.map((plane) {
return plane.bytes;
}).toList(), // transforms the image into a bit array
imageHeight: img.height,
imageWidth: img.width,
numResults: 3,
);
print(recognitions); // shows the predictions in console
Recognition {
label: String;
confidence: double;
}
_cameraController.startImageStream((img) async {
if (available) {
available = false;
await _tensorflowService.runModel(img); // Tflite.runModelOnFrame(...) code
await Future.delayed(Duration(seconds: 1));
available = true;
}
});
<!DOCTYPE html>
<html>
...
<body>
...
<script src="./index.js"></script>
</body>
</html>
import * as toxicity from '@tensorflow-models/toxicity';
...
let model;
...
const startUp = async () => {
...
// Loads the model with threshold = 0.7 and labels = ['toxicity']
// labels can also include any of this range of values:
// ['identity_attack', 'insult', 'obscene', 'severe_toxicity', 'sexual_explicit', 'threat', 'toxicity']
const fetchTweets = async (text) => {
// fetch tweets from server
// enviroment [string]: Base url to the express.js server app
// text [string]: username of the tweets author
// tweetsToSearch [number]: number of tweets (10, 20, 50 or 100)
const result = await fetch(`${enviroment}/twits/${text}/${tweetsToSearch}`);
const response = await result.json();
return response.body;
const classify = (inputs) => {
// model previously loaded
const results = await model.classify(inputs);
return inputs.map((d, i) => {
const obj = {'text': d};
results.forEach((classification) => {
obj[classification.label] = classification.results[i].match;
});
return obj;
const showResults = (predictions) => {
let found = false;
predictions.map((p) => {
if(p.toxicity !== false) {
found = true;
var results = document.getElementById("results");
var element = document.createElement('p');
element.textContent = p.text;
results.appendChild(element);