我正在研究 chrome 扩展。任务是对网页上的图像进行分类。我已经建立了模型,在进行预测之前将图像转换为张量时经常出错。我正在使用document.getElementsByTagName('img')
. 这将返回一个对象。然后我tensor = tf.fromPixels(image[0])
用来获取张量。
它说 :
runtime.onMessage 的事件处理程序出错:错误:传递给 tf.fromPixels() 的像素必须是 HTMLVideoElement、HTMLImageElement、HTMLCanvasElement 或 ImageData,但在 pre_process() 处是对象
请帮忙。
编辑:我的代码是:
//to load images
function load_imgs(debug) {
//for onload images of a webpage
if(debug == 'static'){
imgs = document.getElementsByTagName('img');
console.log('imgs type after get bytag', typeof imgs);
var imgSrcs = [];
for (var i = 0; i < imgs.length; i++) {
imgSrcs.push(imgs[i]);
}
console.log('imgSrcs type:', typeof imgSrcs);
return imgSrcs;
}
}
将其传递给后台脚本:
//send request to background script,takes image elem ref as input
function back_request(elem_ref){
console.log('from back_request ' + typeof elem_ref);
console.log("back_request : ", elem_ref);
chrome.runtime.sendMessage({
get_predict: "predict",
img: elem_ref
},function (response) {
console.log(response.prediction_model);
});
}
这是消息的后台脚本侦听器
//message listner for the prediction
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.get_predict == "predict") {
to_predict = request.img;
console.log('from receive request ', to_predict);
tensor = pre_process(to_predict); //this generates an error, since i am using tf.fromPixels() method to convert it to a tensor to make a prediction
ans = predict_str(model, tensor);
sendResponse({
prediction_model: 'Processing'
});
}