commented ocr veify in filament

This commit is contained in:
dhanabalan
2025-10-17 12:14:07 +05:30
parent f32cf7ca7d
commit 41fce18aa2

View File

@@ -30,6 +30,9 @@ class OcrValidationResource extends Resource
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
public $camera_capture;
public static function form(Form $form): Form public static function form(Form $form): Form
{ {
return $form return $form
@@ -70,10 +73,20 @@ class OcrValidationResource extends Resource
->helperText('You can take a picture from your webcam') ->helperText('You can take a picture from your webcam')
->reactive(), ->reactive(),
Forms\Components\TextInput::make('photo1') // Forms\Components\TextInput::make('camera_capture')
->label('Captured Photo') // ->label('Captured Photo')
->hidden() // ->hidden()
->extraAttributes(['x-ref' => 'hiddenInput']), // ->extraAttributes([
// 'x-ref' => 'hiddenInput', // so Alpine can target it
// ]),
Forms\Components\TextInput::make('camera_capture_file')
->label('Captured Photo')
->reactive()
->hidden()
->extraAttributes([
'x-ref' => 'hiddenInput', // so Alpine can reference it
]),
Forms\Components\Actions::make([ Forms\Components\Actions::make([
Action::make('uploadNow1') Action::make('uploadNow1')
->label('Upload OCR') ->label('Upload OCR')
@@ -203,56 +216,59 @@ class OcrValidationResource extends Resource
} }
}), }),
Action::make('verifyOcr') // Action::make('verifyOcr')
->label('Verify OCR') // ->label('Verify OCR')
->action(function ($get) { // ->action(function ($get) {
$base64Image = $get('photo1'); // $base64Image = $get('camera_capture_file');
// // $fileName = 'capture_1760680702.png';
if (!$base64Image) { // // //dd($base64Image);
Notification::make() // if (!$base64Image) {
->title('No captured image found') // Notification::make()
->warning() // ->title('No captured image found')
->send(); // ->warning()
return; // ->send();
} // return;
// }
// Decode and store the base64 image // // Decode and store the base64 image
$image = str_replace('data:image/jpeg;base64,', '', $base64Image); // $image = str_replace('data:image/jpeg;base64,', '', $base64Image);
$image = str_replace(' ', '+', $image); // $image = str_replace(' ', '+', $image);
$grNumber = $get('gr_number') ?: 'Unknown'; // $grNumber = $get('gr_number') ?: 'Unknown';
$safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber); // $safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber);
$fileName = $safeName . '.jpg'; // $fileName = $safeName . '.jpg';
$filePath = storage_path("app/private/uploads/OCR/{$fileName}"); // $filePath = storage_path("app/private/uploads/OCR/{$fileName}");
if (!file_exists(dirname($filePath))) { // if (!file_exists(dirname($filePath))) {
mkdir(dirname($filePath), 0777, true); // mkdir(dirname($filePath), 0777, true);
} // }
file_put_contents($filePath, base64_decode($image)); // file_put_contents($filePath, base64_decode($image));
// 🧠 Run OCR // // 🧠 Run OCR
$text = (new TesseractOCR($filePath)) // $text = (new TesseractOCR($filePath))
->lang('eng') // ->lang('eng')
->run(); // ->run();
preg_match_all('/\d+/', $text, $matches); // preg_match_all('/\d+/', $text, $matches);
$serialNumbers = array_slice($matches[0], 0, 4); // $serialNumbers = array_slice($matches[0], 0, 4);
if (empty($serialNumbers)) { // if (empty($serialNumbers)) {
Notification::make() // Notification::make()
->title('No numbers found in image') // ->title('No numbers found in image')
->danger() // ->danger()
->send(); // ->send();
return; // return;
} // }
Notification::make() // Notification::make()
->title('OCR Read Successfully') // ->title('OCR Read Successfully')
->body('Serial Numbers: ' . implode(', ', $serialNumbers)) // ->body('Serial Numbers: ' . implode(', ', $serialNumbers))
->success() // ->success()
->send(); // ->send();
}),
// }),
Action::make('downloadAttachment') Action::make('downloadAttachment')
->label('Download PDF') ->label('Download PDF')