This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await Tflite.loadModel( | |
model: "assets/mobilenet_v1_1.0_224.tflite", | |
labels: "assets/labels.txt", | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Recognition { | |
label: String; | |
confidence: double; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_cameraController.startImageStream((img) async { | |
if (available) { | |
available = false; | |
await _tensorflowService.runModel(img); // Tflite.runModelOnFrame(...) code | |
await Future.delayed(Duration(seconds: 1)); | |
available = true; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
... | |
<body> | |
... | |
<script src="./index.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as toxicity from '@tensorflow-models/toxicity'; | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |