ChineseOcrDbCrnnJob.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\DB;
  10. class ChineseOcrDbCrnnJob implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  13. private int $userId;
  14. /**
  15. * Create a new job instance.
  16. *
  17. * @return void
  18. */
  19. public function __construct($userId)
  20. {
  21. $this->userId = $userId;
  22. }
  23. /**
  24. * Execute the job.
  25. *
  26. * @return void
  27. */
  28. public function handle()
  29. {
  30. $table = 'ocr_images';
  31. // status 0 未执行 2 已完成 3 失败
  32. DB::table($table)->where('status', 0)->where('user_id', $this->userId)
  33. ->lazyById()->each(function ($img) use ($table) {
  34. $imagePath = public_path($img->path);
  35. $cmd = 'hub run chinese_ocr_db_crnn_server --input_path "' . $imagePath . '"';
  36. $result = exec($cmd);
  37. $re = preg_replace("/\'/", "\"", $result);
  38. $result = json_decode($re, true);
  39. if (empty($result)) { // 失败
  40. DB::table($table)->where('id', $img->id)->update(['status' => 2, 'result' => $result]);
  41. } else { // 成功
  42. DB::table($table)->where('id', $img->id)->update(['status' => 3]);
  43. }
  44. });
  45. }
  46. }