OcrController.php
4.16 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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
class OcrController extends Controller
{
public function index(Request $request){
return view('dashboard',['ocr'=>[]]);
}
public function show_pdf(Request $request)
{
return response()->file($request->file_path);
}
public function preview_pdf(Request $request){
return view('ocr.ocr_pdf', ['disable'=>$request->disable??0]);
}
public function import_pdf(Request $request){
try {
[$image_path,$js_path] = $this->ocr_process($request);
$json_data = json_decode(file_get_contents($js_path), true);
$ocr=[];
$ocr['json_data']=$json_data;
$ocr['js_path'] = $js_path;
$ocr['image_path'] = $image_path;
$ocr['mst_template'] = ['image'=>$json_data['image']];
$ocr['dt_template_field'] = [];
return response()->json(['status' => true,'data' => $ocr,]);
} catch (\Throwable $e){
report($e);
return response()->json(['status' => false]);
}
}
public function ocr_process($request){
set_time_limit(0);
ini_set('max_execution_time', 0);
$ocr_serve_path=env('OCR_SERVER_PATH','');
$ocr_storage='ocr_results';
if($ocr_serve_path) $ocr_storage='ocr_results_sftp';
$now=Carbon::now()->format('YmdHis');
umask(0002);
Storage::disk($ocr_storage)->makeDirectory($now);
$input_file_name=$request->file('attached_file')->getClientOriginalName();
Log::info('upload ocr '.$input_file_name);
Storage::disk($ocr_storage)->putFileAs($now, $request->file('attached_file'),$input_file_name);
$file_path=Storage::disk($ocr_storage)->path($now.DIRECTORY_SEPARATOR.$input_file_name);
$ch = curl_init();
Storage::disk('ocr_results')->makeDirectory($now);
chmod(Storage::disk('ocr_results')->path($now), 0777);
$params = [
'file_path' => $file_path,
];
foreach (($request->option ?? []) as $key => $value) {
$params[$key] = $value;
}
$url = env('PYTHON_SERVICE_ENDPOINT', "http://127.0.0.1:8000/"). '?'. http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
$data = json_decode($response, true);
if (empty($data['success'])){
throw new Exception($data['message']);
}
if($ocr_serve_path){
$files = Storage::disk('ocr_results_sftp')->files($now);
foreach ($files as $file) {
Storage::disk('ocr_results')->put(
$file,
Storage::disk('ocr_results_sftp')->get($file)
);
}
}
if (!Storage::disk('ocr_results')->exists($now.DIRECTORY_SEPARATOR.'rs.json'))
throw new Exception('file rs ocr not found');
Log::info('read ocr successfully');
return [Storage::disk('ocr_results')->path($now.DIRECTORY_SEPARATOR.'rs.jpg'), Storage::disk('ocr_results')->path($now.DIRECTORY_SEPARATOR.'rs.json')];
}
public function ocr_db(&$ocr){
$json_data=json_decode(file_get_contents($ocr['js_path']), true);
$json_data['chars']=[];
$ocr['json_data']=$json_data;
if(!empty($ocr['tpl'])){
if(!empty($ocr['tpl']['tpl_id'])) $ocr['mst_template']=OcrMstTemplate::where('tpl_id', $ocr['tpl']['tpl_id'])->first()?->toArray()??[];
else $ocr['mst_template']=$ocr['tpl'];
}
if(empty($ocr['mst_template'])) $ocr['mst_template']=['tpl_text'=>'','tpl_xy'=>'','tpl_id'=>'','image'=>$json_data['image']];
$ocr['mst_template_field']=OcrMstTemplateField::get()->keyBy('field_name');
$ocr['dt_template_field']=!empty($ocr['tpl'])?OcrDtTemplate::where('tpl_id', $ocr['tpl']['tpl_id'])->get():[];
}
}