-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
016a188
commit c551f80
Showing
5 changed files
with
786 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,5 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
.env | ||
crendentials.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import vision from "@google-cloud/vision"; | ||
|
||
const client: any = new vision.ImageAnnotatorClient(); | ||
|
||
const dataURLtoBlob = (dataurl: string) => { | ||
let arr = dataurl.split(","), | ||
mime = (arr as any)[0].match(/:(.*?);/)[1], | ||
bstr = atob(arr[1]), | ||
n = bstr.length, | ||
u8arr = new Uint8Array(n); | ||
while (n--) { | ||
u8arr[n] = bstr.charCodeAt(n); | ||
} | ||
return new Blob([u8arr], { type: mime }); | ||
}; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const image = JSON.parse(req.body).image as string; | ||
|
||
console.log(dataURLtoBlob(image)); | ||
// console.log("Image", Buffer.from(image).toString("base64")); | ||
const request = { | ||
image: { | ||
content: Buffer.from(await dataURLtoBlob(image).arrayBuffer()).toString( | ||
"base64" | ||
), | ||
}, | ||
}; | ||
|
||
const [result] = await client.objectLocalization(request); | ||
console.log(result); | ||
const objects = result.localizedObjectAnnotations; | ||
if (objects) { | ||
objects.forEach((object: { name: string; score: number }) => { | ||
console.log(`Name: ${object.name}`); | ||
console.log(`Confidence: ${object.score}`); | ||
}); | ||
|
||
res.json(objects); | ||
} else { | ||
res.json(result); | ||
} | ||
} |
Oops, something went wrong.