第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 Node JS(fs 和 http)保存后無法打開 JPG 或 PNG

使用 Node JS(fs 和 http)保存后無法打開 JPG 或 PNG

BIG陽 2024-01-18 20:41:42
我的代碼的目標(biāo)是 1)用戶將發(fā)送圖像 URL 2)機(jī)器人讀取 URL 3)機(jī)器人將圖像保存到“images”文件夾中 4)機(jī)器人使用 tfjs 將圖像轉(zhuǎn)換為張量 5)機(jī)器人使用COCO-SSD JS作為預(yù)訓(xùn)練模型(以圖像張量為參數(shù))并打印然后發(fā)送結(jié)果?,F(xiàn)在,我的問題是,每當(dāng)我嘗試使用圖像的 URL 保存圖像時(shí),我都會(huì)得到一個(gè)程序和 Windows 都無法讀取的文件!它工作過一次,能夠打開和使用文件。但現(xiàn)在返回 Windows 10 無法讀取 .PNG 或 .JPG 文件的文件。這是我的代碼:    const {Client, MessageAttachment} = require('discord.js');    const bot = new Client();    const tf = require('@tensorflow/tfjs-node');    const ts = require('@tensorflow/tfjs-core');    require('@tensorflow/tfjs-backend-cpu');    require('@tensorflow/tfjs-backend-webgl');    const coco = require('@tensorflow-models/coco-ssd');    const fs = require('fs');    const fetch = require("node-fetch");    const https = require('https');    const request = require('request');      bot.on('message', gotMessage);    function gotMessage(msg) {        if(msg.content === '!object')  {               const attachments = (msg.attachments).array();            const filepath = "./images/" + Date.now() + "J" + ".jpg";            console.log(filepath);            const imageurl = attachments[0].url;            saveImageToDisk(imageurl,filepath)            const img_buffer = fs.readFileSync(filepath)            const img = tf.node.decodeImage(img_buffer)            coco.load().then(model => {                // detect objects in the image.                model.detect(img).then(predictions => {                    console.log('Predictions: ', predictions);                });              });            msg.reply('Enjoy');            msg.channel.send(attachments[0].url);        }    }    function saveImageToDisk(url,path) {        var fullUrl = url;        var localPath = fs.createWriteStream(path);        var request = https.get(fullUrl,function(response) {             console.log(response)            response.pipe(localPath)        });    }PS:后面的“J”Date.now()是有意的。
查看完整描述

1 回答

?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

我看到的是你的應(yīng)用程序完全不同步 - 我的意思是


你有saveImageToDisk(imageurl,filepath)它將文件寫入磁盤,但它執(zhí)行,將文件寫入隊(duì)列,aa,然后你用同步讀取(尚未保存)文件。


我將嘗試對(duì)它進(jìn)行一些修復(fù),以向您展示至少一種執(zhí)行此操作的方法 - 但在規(guī)劃應(yīng)用程序的流程時(shí),您需要考慮代碼中的一些同步。


console.log('Authenticating bot...');


const {Client, MessageAttachment} = require('discord.js');

const bot = new Client();

const tf = require('@tensorflow/tfjs-node');

const ts = require('@tensorflow/tfjs-core');

require('@tensorflow/tfjs-backend-cpu');

require('@tensorflow/tfjs-backend-webgl');

const coco = require('@tensorflow-models/coco-ssd');

const fs = require('fs');

const fetch = require("node-fetch");

const https = require('https');

const request = require('request');


bot.login('BOTTOKEN');


bot.on('ready', readyDiscord);


function readyDiscord() {

    console.log('Authenticated and On!');

}


bot.on('message', gotMessage);


async function gotMessage(msg) {

    if(msg.content === '!object')  {   

        const attachments = (msg.attachments).array();


        const filepath = "./images/" + Date.now() + "J" + ".jpg";

        console.log(filepath);

        const imageurl = attachments[0].url;


        await saveImageToDisk(imageurl,filepath)


        const img_buffer = fs.readFileSync(filepath)

        const img = tf.node.decodeImage(img_buffer)


        coco.load().then(model => {

            // detect objects in the image.

            model.detect(img).then(predictions => {

                console.log('Predictions: ', predictions);

            });

          });

        msg.reply('Enjoy');

        msg.channel.send(attachments[0].url);

    }

}

async function saveImageToDisk(url,path) {

  return new Promise((resolve, reject) => {

    var fullUrl = url;

    var localPath = fs.createWriteStream(path);

    var request = https.get(fullUrl,function(response) { 

        console.log(response)

        response.pipe(localPath)

        response.on('end', resolve);

    }).on('error', reject);

  });

}

這樣,在執(zhí)行讀取尚未填充的文件的代碼之前,代碼將等待,直到文件被寫入(或發(fā)生錯(cuò)誤) - 您當(dāng)然應(yīng)該嘗試捕獲錯(cuò)誤并處理它們。


查看完整回答
反對(duì) 回復(fù) 2024-01-18
  • 1 回答
  • 0 關(guān)注
  • 250 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)