首页 > 解决方案 > 没有从谷歌视觉 API 获得正确的输出格式

问题描述

输出在 console.log() 中正确显示,但是当我将其导出并使用 res.end() 时,它会显示其他内容。

这是我得到的输出 [1]:https ://i.stack.imgur.com/RWj65.png

ocr.js

const vision = require('@google-cloud/vision');

process.env.GOOGLE_APPLICATION_CREDENTIALS = 'C:/Users/animesh/Downloads/polished-signer-319320-9bf98d18ed89.json'

async function quickstart() {
    try {
        // Creates a client
        const client = new vision.ImageAnnotatorClient();

        // Performs text detection on the local file
        const [result] = await client.textDetection('so.jpg');
        const detections = result.textAnnotations;
        ***const [ text ] = detections
        const data =  text.description ;
       
        console.log(data);
        module.exports = data**;*
       
    } catch (error) {
        console.log(error)
    }

}

quickstart()

索引.JS

const http = require('http');
const data = require("./Home.js")
const port = process.env.PORT || 3000;

const server  = http.createServer((req, res)=>{
    
    res.setHeader('Content-Type', 'text/html')
    console.log(req.url)
    console.log(data);
    if(req.url == '/'){
        res.statusCode = 200;
        **res.end(data.toString());**
    }[enter image description here][1]
})

server.listen(port, ()=>{
    console.log(`Server is listening on port ${port}`);

});

标签: node.jsbackendgoogle-vision

解决方案


推荐阅读