OCR
Advanced solution for Optical Character Recognition
by api4ai
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>OCR Text Recognition API sample</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css">
</head>
<body>
<main class="container">
<article style="margin-top: 0px; text-align: center;">
<h3>OCR API sample</h3>
<p>This API processes images and performs Optical Character Recognition.</p>
</article>
<label for="file">Select image to upload
<input type="file" id="file" name="file">
</label>
<h4 id="spinner" style="text-align: center;" aria-busy="true" hidden>Processing</h4>
<section id="sectionParsed" hidden>
<label for="parsed">💬 Recognized text:
<pre id="parsed" style="padding: 10px;"></pre>
</label>
</section>
<section id="sectionRaw" hidden>
<label for="raw">💬 Raw response:
<pre id="raw" style="padding: 10px;"></pre>
</label>
</section>
</main>
</body>
</html>
JavaScript
// Example of using API4AI OCR text recognition.
// Use 'demo' mode just to try api4ai for free. Free demo is rate limited.
// For more details visit:
// https://api4.ai
// Use 'rapidapi' if you want to try api4ai via RapidAPI marketplace.
// For more details visit:
// https://rapidapi.com/api4ai-api4ai-default/api/ocr43/details
const MODE = 'demo'
// Your RapidAPI key. Fill this variable with the proper value if you want
// to try api4ai via RapidAPI marketplace.
const RAPIDAPI_KEY = ''
const OPTIONS = {
demo: {
url: 'https://demo.api4ai.cloud/ocr/v1/results',
headers: { 'A4A-CLIENT-APP-ID': 'sample' }
},
rapidapi: {
url: 'https://ocr43.p.rapidapi.com/v1/results',
headers: { 'X-RapidAPI-Key': RAPIDAPI_KEY }
}
}
document.addEventListener('DOMContentLoaded', function (event) {
const input = document.getElementById('file')
const raw = document.getElementById('raw')
const sectionRaw = document.getElementById('sectionRaw')
const parsed = document.getElementById('parsed')
const sectionParsed = document.getElementById('sectionParsed')
const spinner = document.getElementById('spinner')
input.addEventListener('change', (event) => {
const file = event.target.files[0]
if (!file) {
return false
}
sectionRaw.hidden = true
sectionParsed.hidden = true
spinner.hidden = false
// Preapare request.
const form = new FormData()
form.append('image', file)
const requestOptions = {
method: 'POST',
body: form,
headers: OPTIONS[MODE].headers
}
// Make request.
fetch(OPTIONS[MODE].url, requestOptions)
.then(response => response.json())
.then(function (response) {
// Print raw response.
raw.textContent = JSON.stringify(response, undefined, 2)
sectionRaw.hidden = false
// Parse response and print recognized text.
const text = response.results[0].entities[0].objects[0].entities[0].text
parsed.textContent =...