1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace App\Jobs;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldBeUnique;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\DB;
- class ChineseOcrDbCrnnJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- private int $userId;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct($userId)
- {
- $this->userId = $userId;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $table = 'ocr_images';
- // status 0 未执行 2 已完成 3 失败
- DB::table($table)->where('status', 0)->where('user_id', $this->userId)
- ->lazyById()->each(function ($img) use ($table) {
- $imagePath = public_path($img->path);
- $cmd = 'hub run chinese_ocr_db_crnn_server --input_path "' . $imagePath . '"';
- $result = exec($cmd);
- $re = preg_replace("/\'/", "\"", $result);
- $result = json_decode($re, true);
- if (empty($result)) { // 失败
- DB::table($table)->where('id', $img->id)->update(['status' => 2, 'result' => $result]);
- } else { // 成功
- DB::table($table)->where('id', $img->id)->update(['status' => 3]);
- }
- });
- }
-
- }
|