Skip to content

Instantly share code, notes, and snippets.

@altrim
Created December 6, 2021 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altrim/85e550840e8e41090994f1c438625138 to your computer and use it in GitHub Desktop.
Save altrim/85e550840e8e41090994f1c438625138 to your computer and use it in GitHub Desktop.
import "@johnlindquist/kit";
// Menu: Translate Image
// Description: Translate text extracted from a selected image
// Author: Altrim Beqiri
// Twitter: @altrimbeqiri
const vision = await npm("@google-cloud/vision");
const gtranslate = await npm("@google-cloud/translate");
const { Translate } = gtranslate.v2;
const client = new vision.ImageAnnotatorClient();
const translate = new Translate();
const extractText = async (): Promise<string> => {
const file = await getSelectedFile();
const [result] = await client.textDetection(file);
return result.fullTextAnnotation.text;
};
const translateText = async (text: string) => {
let [translations] = await translate.translate(text, "en");
translations = Array.isArray(translations) ? translations : [translations];
return translations.join(" ");
};
div(md("Extracting and Translating..."));
const text = await extractText();
const translation = await translateText(text);
show(
`
<div>
<div class="max-w-2xl mx-auto sm:px-6 lg:px-8 text-gray-800 bg-white">
<div class="overflow-hidden shadow-md">
<div class="px-6 py-4 border-b border-gray-200 font-bold">Extracted Text</div>
<div class="p-6 border-b border-gray-200">${text}</div>
</div>
</div>
<div class="max-w-2xl mx-auto sm:px-6 lg:px-8 text-gray-800 bg-white">
<div class="overflow-hidden shadow-md">
<div class="px-6 py-4 border-b border-gray-200 font-bold">Translation</div>
<div class="p-6 border-b border-gray-200">${translation}</div>
</div>
</div>
</div>
`,
{ width: 640, height: 420, transparent: true }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment