使用YOLO
和p5.js
,我正在尝试使用网络摄像头识别对象。即使我已授予访问网络摄像头的权限,但仍然存在以下问题。有什么方法可以更快,更一致地改进对象识别?
test.mp4
视频保存在本地,如何在保存的视频上进行对象识别。 以下是我的代码:
let video; //Variable for video stream
let yolo; //Initializing model method with YOLO.
let status; //Status check to determine whether the model has been loaded
let objects = []; //List of objects returned from YOLO
function setup() {
createCanvas(800, 600);
video = createCapture(VIDEO); //Capturing live video from webcam
video.size(400, 500);
// Creating a YOLO method using ml5
yolo = ml5.YOLO(video, startDetecting);
// Hide the original video
video.hide();
status = select('#status');
}
function draw() {
image(video, 0, 0, width, height); // Displaying image on a canvas
for (let i = 0; i < objects.length; i++) //Iterating through all objects
{
noStroke();
fill(0, 255, 0); //Color of text
text(objects[i].label, objects[i].x * width, objects[i].y * height - 5); //Displaying the label
noFill();
strokeWeight(4);
stroke(0, 255, 0); // Define rectangular outline here
rect(objects[i].x * width, objects[i].y * height, objects[i].w * width, objects[i].h * height);
}
}
function startDetecting() {
status.html('Model loaded!'); //When the model is loaded
detect(); //Calling detect method
}
function detect() {
yolo.detect(function(err, results) {
objects = results; //Storing results in object
detect(); //Continuous detection
});
}
一种可能的解决方案是不使用p5.js,而是使用getUserMedia()方法访问摄像机。这样,您可以将视频流镜像到图像元素并检测该图像元素。
对于保存的视频文件,您可以使用HTML Media Capture接受视频文件并将其放入HTML中:https://www.html5rocks.com/en/tutorials/getusermedia/intro/
完成后,您可以创建一个功能,当您按下播放按钮时,该功能可以进行检测。像这样。
var video = document.getElementById('video');
video.addEventListener('play', function() { // block of code }, 16);