如何解决试图匹配mongodb图像数据库中的google vision标签
我对所有代码事物都是陌生的。现在,我正在开发一个使用图像搜索相似图像的应用程序(非常类似于使用图像的Google图像搜索)。图片正在通过Google视觉API进行标签运行,但是我不确定如何使用数据库来匹配标签。这是我的server.js代码。任何帮助将不胜感激。基本上,我希望能够上传照片并使具有相似标签的照片对象以降序显示。有人告诉我在newImage.save()行的位置添加一些内容,但是我不知道要去哪里。谢谢!!
const path = require("path");
const mongoose = require("mongoose");
require("dotenv").config();
const config = require("./config");
const routes = require("./routes");
var bodyParser = require('body-parser');
var multer = require ("multer");
const Images = require("./models/images");
const fs = require ('fs')
const app = express();
//google vision
async function quickstart(uploadedFile) {
const uploadFilename = uploadedFile.filename;
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient({
keyFilename: "./apiAuthorization.json"
});
// Performs label detection on the image file
const [result] = await client.labelDetection("./client/public/uploads/" + uploadFilename);
const labels = result.labelAnnotations;
const labelArray = [];
console.log('Labels:');
labels.forEach(label => labelArray.push(label.description));
//goes to google returns array
return labelArray;
}
//GOOGLE VISIONS CODE
// middleware to parse data
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
const storage = multer.diskStorage({
destination: function (req,file,cb){
cb(null,__dirname + "/client/public/uploads/")
},filename: function(req,cb){
cb(null,file.fieldname + "-" + Date.Now() + path.extname(file.originalname));
}
})
const upload = multer ({storage: storage})
app.get("/search",(req,res)=>{
res.sendFile(__dirname + "/client/src/pages/Search/index.js")
})
app.post('/uploadFile',upload.single('myImage'),async (req,res,next) => {
const file = req.file;
if(!file){
const error = new Error ("please upload");
error.httpStatusCode = 400;
return next(error);
}
// res.send (file);
var uploadedFile = file;
//await because quickstart takes time waits for return
const labelsFinal = await quickstart(uploadedFile);
//model
const newImage = new Images({
imageName: uploadedFile.filename,labels: labelsFinal
})
newImage.save().then(image => res.json(image))
//something to match the labels should go here
.catch(err => console.log(err))
})
// serve up static assets
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname,"./client/build")))
};
// connect to Mongo DB
mongoose.connect(config.MONGO_URI,{ useNewUrlParser: true,useUnifiedTopology: true,useCreateIndex: true,useFindAndModify: true })
.then(() => console.log(`Mongo DB Succesfully Connected`))
.catch(err => console.log(err));
// use routes
app.use(routes);
// check for "production" enviroment and set port
const PORT = process.env.PORT || 3001;
// start server
app.listen(PORT,() => {
console.log(`App listening on port: ${PORT}`);
})```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。