xxxxxxxxxx
let video;
let detector;
let detections = [];
let car = 0;
let carcount = 0;
let truck = 0;
let truckcount = 0;
let person = 0;
let personcount = 0;
let last_time = 0;
let table;
let label = "";
function setup() {
video = createVideo('220427_video.mp4', videoReady);
// video = createVideo('220427_Zeitraffer.mp4', videoReady);
video.size(1080, 1920);
video.loop();
video.hide();
frameRate(30) //???
/// Table
table = new p5.Table();
table.addColumn('KI');
table.addColumn('all');
createCanvas(1080, 1920);
}
function videoReady() {
// Models available are 'cocossd', 'yolo'
detector = ml5.objectDetector('cocossd', modelReady);
}
function gotDetections(error, results) {
if (error) {
console.error(error);
}
detections = results;
detector.detect(video, gotDetections);
}
function modelReady() {
//video.play();
detector.detect(video, gotDetections);
}
function draw() {
//video.play();
image(video, 0, 0);
// KI
for (let i = 1; i < detections.length; i += 1) {
const object = detections[i];
label += (object.label + ",");
if (object.label == 'car') {
car += i;
carcount += 1;
}
if (object.label == 'truck') {
truck += i;
truckcount += 1;
}
if (object.label == 'person') {
person += i;
personcount += 1;
}
stroke(0, 255, 0);
strokeWeight(4);
noFill();
rect(object.x, object.y, object.width, object.height);
noStroke();
fill(255);
textSize(24);
text(object.label, object.x + 10, object.y + 24);
// save to csv
if (millis() - last_time > 1000) {
let frame = frameCount;
let newRow = table.addRow();
//newRow.setString('car', car/carcount); // i = detection length
//newRow.setString('train', train/traincount);
//newRow.setString('truck', truck/truckcount);
newRow.setString('KI', car/carcount + "," + person/personcount + "," + truck/truckcount);
newRow.setString('all', label);
car = 0;
person = 0;
truck = 0;
carcount = 0;
personcount = 0;
truckcount = 0;
last_time = millis();
label = "";
}
}
}
function keyPressed() {
if (key == "c") {
save('KI.png');
saveTable(table, 'KI.csv');
}
}