Generative AI with Node js in Hindi #18 Read Image with Google GenAI through UI
Read Image with GenAI through UI
Steps:-
Setup Express js
Make form
Get Image from UI and read
Pass data to google gen ai
Pass Prompt to read image
Interview Question
Code and Notes
Code:-
import { GoogleGenAI } from '@google/genai'
import express from 'express';
import multer from 'multer'
import dotenv from 'dotenv'
import { readFileSync } from 'fs'
const app = express();
const upload = multer({ dest: "uploads" })
app.get("/", (req, resp) => {
resp.send(`
<form method="post" action ="/upload" enctype="multipart/form-data">
<input type="file" name="image" />
<button>Read Image </button>
</form>
`)
})
app.post("/upload", upload.single("image"), async (req, resp) => {
const path = req.file.path;
resp.send(await main(path))
})
dotenv.config();
const GoogleAI = new GoogleGenAI({ apiKey: process.env.geminiKey });
async function main(path) {
const base64Img = readFileSync(path, {
encoding: "base64"
})
const response = await GoogleAI.models.generateContent({
model: "gemini-2.5-flash",
contents: [
{
inlineData: {
mimeType: "image/png",
data: base64Img
},
},
{ text: "read text from this image" }
]
})
return response.text;
}
// main();
app.listen(3000)