-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr.php
More file actions
476 lines (428 loc) · 18.2 KB
/
ocr.php
File metadata and controls
476 lines (428 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/**
* Optical Character Recognition (OCR) Tool
*
* Handle hupl configuration request
*/
if (isset($_GET['hupl'])) {
$name = 'Image OCR Tool';
$site_url = ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . $_SERVER['SERVER_NAME'] . '.hupl"');
echo "{\n";
echo " \"name\": \"$name\",\n";
echo " \"type\": \"http\",\n";
echo " \"targetUrl\": \"$site_url\",\n";
echo " \"fileParam\": \"file\"\n";
echo "}";
exit;
}
/**
* Optical Character Recognition (OCR) Tool
*
* A PHP web application that uses AI to perform OCR on uploaded images and extract
* text in Markdown format.
*
* Features:
* - AI-powered OCR of images
* - Multiple lightweight AI models support (filtered for free models)
* - Multilingual output (6 languages)
* - Web interface with real-time results
* - REST API support
* - Configurable API endpoint via external config.php
*
* Requirements:
* - PHP 7.0+
* - cURL extension
* - JSON extension
* - Access to compatible AI API (e.g., Ollama)
*
* Usage:
* - Web interface: Access via browser
* - API endpoint: POST /ocr.php with image data
*
* API Usage:
* POST /ocr.php
* Parameters:
* - image (required): Image file to process
* - model (optional): AI model to use (default: qwen2.5:1.5b)
* - language (optional): Output language (default: ro)
*
* Response:
* {
* "text": "extracted text in markdown format"
* }
*
* Configuration:
* Create a config.php file with:
* - $LLM_API_ENDPOINT: AI API endpoint URL
* - $LLM_API_KEY: API key (if required)
*
* @author Costin Stroie <costinstroie@eridu.eu.org>
* @version 1.0
* @license GPL 3
*/
// Include common functions
include 'common.php';
// Configuration - Load from config.php if available, otherwise use defaults
if (file_exists('config.php')) {
include 'config.php';
} else {
// Safe defaults
$LLM_API_ENDPOINT = 'http://127.0.0.1:11434/v1';
$LLM_API_KEY = '';
$DEFAULT_VISION_MODEL = 'gemma3:4b';
$LLM_API_FILTER = '/free/';
}
// Create chat endpoint URL
$LLM_API_ENDPOINT_CHAT = $LLM_API_ENDPOINT . '/chat/completions';
// Fetch available models from API, filtering with configured filter
$AVAILABLE_MODELS = getAvailableModels($LLM_API_ENDPOINT, $LLM_API_KEY, $LLM_API_FILTER);
// If API call fails, use default models
if (empty($AVAILABLE_MODELS)) {
$AVAILABLE_MODELS = [
'gemma3:4b' => 'Gemma 3 (4B)',
'moondream:1.8b' => 'Moondream (1.8B)'
];
}
// Set default model if not defined in config
if (!isset($DEFAULT_VISION_MODEL)) {
$DEFAULT_VISION_MODEL = !empty($AVAILABLE_MODELS) ? array_keys($AVAILABLE_MODELS)[0] : 'gemma3:4b';
}
/**
* Get selected model and language from POST data, cookies, or use defaults
*/
$MODEL = isset($_POST['model']) ? $_POST['model'] : (isset($_COOKIE['ocr-model']) ? $_COOKIE['ocr-model'] : $DEFAULT_VISION_MODEL);
$LANGUAGE = isset($_POST['language']) ? $_POST['language'] : (isset($_COOKIE['ocr-language']) ? $_COOKIE['ocr-language'] : 'ro');
/**
* Validate model selection
* Falls back to default model if invalid model is selected
*/
if (!array_key_exists($MODEL, $AVAILABLE_MODELS)) {
$MODEL = 'gemma3:4b'; // Default to a valid model
}
/**
* Validate language selection
* Falls back to Romanian if invalid language is selected
*/
if (!array_key_exists($LANGUAGE, $AVAILABLE_LANGUAGES)) {
$LANGUAGE = 'ro'; // Default to Romanian
}
/**
* System prompt for the AI model
* Contains instructions for performing OCR on images
*/
$SYSTEM_PROMPT = "Perform Optical Character Recognition (OCR) on the following image data. Extract and return ONLY the text you see in the image, formatted appropriately in Markdown. Do not add any explanations or introductions. Return only the Markdown formatted text content.
CRITICAL INSTRUCTION: " . getLanguageInstruction($LANGUAGE);
/**
* System prompt for the summary AI model
* Contains instructions for summarizing text content
*/
$SUMMARY_SYSTEM_PROMPT = "You are a helpful assistant that creates concise summaries of text content.
TASK: Create a brief summary of the provided text content. Focus on the main points and key information. Keep the summary under 100 words.
CRITICAL INSTRUCTION: " . getLanguageInstruction($LANGUAGE) . "
OUTPUT FORMAT (JSON):
{
\"summary\": \"summary text\"
}
RULES:
- Focus on main points and key information
- Keep summary under 100 words
- Respond ONLY with the JSON, without additional text";
/**
* Application state variables
* @var string|null $result Extracted text result
* @var string|null $summary Generated summary of the text
* @var string|null $error Error message if any
* @var bool $processing Whether analysis is in progress
* @var bool $is_api_request Whether request is API call (not web form)
* @var bool $is_hupl_request Whether request is hupl-compatible call
*/
$result = null;
$summary = null;
$error = null;
$processing = false;
$is_api_request = false;
$is_hupl_request = false;
/**
* Handle POST/GET requests for image OCR
* Processes both web form submissions and API requests
* Validates input, calls AI API, and processes response
*/
if (($_SERVER['REQUEST_METHOD'] === 'POST' && (isset($_FILES['image']) || isset($_FILES['file'])) &&
((isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) ||
(isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK))) ||
($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['url']))) {
// Determine which file input is being used
$file_key = isset($_FILES['file']) ? 'file' : 'image';
$image_file = $_FILES[$file_key];
$processing = true;
$is_api_request = (!isset($_POST['submit']) && !isset($_GET['submit'])); // If no submit button, it's an API request
$is_hupl_request = $file_key === 'file'; // Check for hupl-compatible request
// Validate file upload
// Check file size (max 10MB to accommodate PDFs)
if ($image_file['size'] > MAX_FILE_SIZE) {
$error = 'The file is too large. Maximum ' . (MAX_FILE_SIZE / 1024 / 1024) . 'MB allowed.';
$processing = false;
}
// Check file type
$allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'application/pdf'];
if (!in_array($image_file['type'], $allowed_types)) {
$error = 'Invalid file type. Only JPEG, PNG, GIF, WebP images and PDF documents are allowed.';
$processing = false;
}
// Only proceed with API call if validation passed
if ($processing) {
// Handle PDF files
if ($image_file['type'] === 'application/pdf') {
// Check if Imagick or Gmagick extension is available
if (!extension_loaded('imagick') && !extension_loaded('gmagick')) {
$error = 'PDF processing requires either the ImageMagick or GraphicsMagick extension which is not installed or enabled.';
$processing = false;
} else {
// Extract images from PDF
$images = extractImagesFromPDF($image_file['tmp_name']);
if ($images === false || empty($images)) {
$error = 'Failed to extract images from PDF or PDF contains no images.';
$processing = false;
} else {
// Use the first image from PDF for OCR
$temp_image_path = tempnam(sys_get_temp_dir(), 'pdf_') . '.png';
if (file_put_contents($temp_image_path, $images[0]) === false) {
$error = 'Failed to save extracted PDF image.';
$processing = false;
} else {
// Preprocess the extracted image
$preprocessed_image_path = preprocessImageForOCR($temp_image_path);
unlink($temp_image_path); // Clean up temporary file
}
}
}
} else {
// Handle regular image files
$preprocessed_image_path = preprocessImageForOCR($image_file['tmp_name']);
}
if ($processing && isset($preprocessed_image_path)) {
if ($preprocessed_image_path === false) {
$error = 'Failed to preprocess the image for OCR.';
$processing = false;
} else {
// Read and encode preprocessed image
$image_data = file_get_contents($preprocessed_image_path);
if ($image_data === false) {
$error = 'Failed to read the preprocessed image file.';
$processing = false;
} else {
$base64_image = base64_encode($image_data);
$image_url = 'data:image/png;base64,' . $base64_image;
// Store base64 for display
$preprocessed_image_base64 = $base64_image;
}
// Clean up temporary file
unlink($preprocessed_image_path);
}
}
}
if ($processing) {
// Prepare API request
$data = [
'model' => $MODEL,
'messages' => [
['role' => 'system', 'content' => $SYSTEM_PROMPT],
[
'role' => 'user',
'content' => [
[
'type' => 'image_url',
'image_url' => [
'url' => $image_url
]
]
]
]
]
];
// Make API request using common function
$response_data = callLLMApi($LLM_API_ENDPOINT_CHAT, $data, $LLM_API_KEY);
if (isset($response_data['error'])) {
$error = $response_data['error'];
} elseif (isset($response_data['choices'][0]['message']['content'])) {
$result = trim($response_data['choices'][0]['message']['content']);
// Remove markdown code fences if present
$result = preg_replace('/^```(?:markdown)?\s*(.*?)\s*```$/s', '$1', $result);
// Generate summary of the extracted text
if (!empty($result)) {
// Prepare summary API request
$summary_data = [
'model' => $DEFAULT_TEXT_MODEL ?? 'qwen2.5:1.5b',
'messages' => [
['role' => 'system', 'content' => $SUMMARY_SYSTEM_PROMPT],
['role' => 'user', 'content' => "TEXT TO SUMMARIZE:\n" . $result]
]
];
// Make summary API request
$summary_response = callLLMApi($LLM_API_ENDPOINT_CHAT, $summary_data, $LLM_API_KEY);
if (isset($summary_response['choices'][0]['message']['content'])) {
$summary_content = trim($summary_response['choices'][0]['message']['content']);
// Extract JSON from summary response
if (preg_match('/\{[^}]+\}/', $summary_content, $summary_matches)) {
$summary_json_str = $summary_matches[0];
$summary_result = json_decode($summary_json_str, true);
if (json_last_error() === JSON_ERROR_NONE && isset($summary_result['summary'])) {
$summary = $summary_result['summary'];
}
}
}
}
} else {
$error = 'Invalid API response format';
}
// Set cookies with the selected model and language only for web requests
if (!$is_api_request) {
setcookie('ocr-model', $MODEL, time() + (30 * 24 * 60 * 60), '/'); // 30 days
setcookie('ocr-language', $LANGUAGE, time() + (30 * 24 * 60 * 60), '/'); // 30 days
}
// Return JSON if it's an API request
if ($is_api_request) {
if ($is_hupl_request) {
// For hupl-compatible requests, return only the text
header('Access-Control-Allow-Origin: *');
header('Content-Type: text/plain');
if ($error) {
echo $error;
} else {
echo $result;
}
} else {
// Regular API request returns JSON
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
if ($error) {
echo json_encode(['error' => $error]);
} else {
echo json_encode(['text' => $result]);
}
}
exit;
}
}
}
?>
<!DOCTYPE html>
<html lang="ro">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DocMind AI - OCR Tool</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext y='.9em' font-size='90'%3E%F0%9F%93%B7%3C/text%3E%3C/svg%3E">
</head>
<body>
<div class="container">
<hgroup>
<h1>📷 DocMind AI - OCR Tool</h1>
<p>AI-powered optical character recognition</p>
</hgroup>
<main>
<?php if ($error): ?>
<section role="alert" class="error">
<strong>⚠️ Error:</strong>
<?php if (is_array($error)): ?>
<pre><?php echo json_encode($error, JSON_PRETTY_PRINT); ?></pre>
<?php else: ?>
<?php echo htmlspecialchars($error); ?>
<?php endif; ?>
</section>
<?php endif; ?>
<?php if ($result): ?>
<?php if ($summary): ?>
<article>
<header>
<h2>📄 Summary</h2>
</header>
<p><?php echo htmlspecialchars($summary); ?></p>
</article>
<?php endif; ?>
<article>
<header>
<h2>🔍 OCR Result</h2>
</header>
<textarea class="markdown-result" readonly><?php echo htmlspecialchars($result); ?></textarea>
</article>
<?php if (isset($preprocessed_image_base64)): ?>
<article>
<header>
<h2>Preprocessed Image</h2>
</header>
<figure class="preprocessed-image-container">
<img src="data:image/png;base64,<?php echo $preprocessed_image_base64; ?>"
alt="Preprocessed image for OCR"
class="preprocessed-image">
</figure>
</article>
<?php endif; ?>
<?php endif; ?>
<form method="POST" action="" id="ocrForm" enctype="multipart/form-data">
<fieldset>
<label for="image">Image file:</label>
<input
type="file"
id="image"
name="image"
accept="image/jpeg,image/png,image/gif,image/webp,application/pdf"
required
>
<small>
Supported formats: JPEG, PNG, GIF, WebP, PDF. Maximum size: 10MB.
</small>
<label for="model">AI model:</label>
<select id="model" name="model">
<?php foreach ($AVAILABLE_MODELS as $value => $label): ?>
<option value="<?php echo htmlspecialchars($value); ?>" <?php echo ($MODEL === $value) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($label); ?>
</option>
<?php endforeach; ?>
</select>
<small>
Select the AI model to use for OCR.
</small>
<label for="language">Response language:</label>
<select id="language" name="language">
<?php foreach ($AVAILABLE_LANGUAGES as $value => $label): ?>
<option value="<?php echo htmlspecialchars($value); ?>" <?php echo ($LANGUAGE === $value) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($label); ?>
</option>
<?php endforeach; ?>
</select>
<small>
Select the language for the OCR output.
</small>
</fieldset>
<button type="submit" name="submit" value="1" class="btn btn-primary">
<?php if ($processing && !$result && !$error): ?>
<span class="loading"></span>
<?php endif; ?>
📄 Extract text
</button>
<div class="button-grid">
<button type="button" class="btn btn-secondary" onclick="clearForm()">
🔄 New OCR
</button>
<button type="button" class="btn btn-secondary" onclick="window.location.href='index.php'">
🏠 Back to Main Menu
</button>
</div>
</form>
</main>
</div>
<script>
function clearForm() {
document.getElementById('image').value = '';
document.getElementById('model').selectedIndex = 0;
document.getElementById('language').selectedIndex = 0;
// Reload page to clear results
window.location.href = window.location.pathname;
}
</script>
</body>
</html>