feat(websocket): frontend websocket
This commit is contained in:
@@ -5,25 +5,28 @@ from PIL import Image
|
||||
import pytesseract
|
||||
|
||||
|
||||
def _process_ocr_file(file) -> bool:
|
||||
if file.ocr_status != OCRStatus.TO_PROCESS:
|
||||
def _process_ocr_file(obj, file, crop: ImageCrop | None = None) -> bool:
|
||||
if obj.ocr_status != OCRStatus.TO_PROCESS:
|
||||
return False
|
||||
|
||||
file.ocr_status = OCRStatus.PROCESSING
|
||||
file.save(update_fields=["ocr_status"])
|
||||
obj.ocr_status = OCRStatus.PROCESSING
|
||||
obj.save(update_fields=["ocr_status"])
|
||||
|
||||
try:
|
||||
file.ocr_raw_data = pytesseract.image_to_string(Image.open(file.file))
|
||||
image = Image.open(file)
|
||||
|
||||
except Exception:
|
||||
file.ocr_status = OCRStatus.ERROR
|
||||
file.save(update_fields=["ocr_status"])
|
||||
if crop:
|
||||
image = image.crop((crop.x1, crop.y1, crop.x2, crop.y2))
|
||||
|
||||
return False
|
||||
obj.ocr_raw_data = pytesseract.image_to_string(image)
|
||||
obj.ocr_status = OCRStatus.PROCESSED
|
||||
|
||||
except Exception as e:
|
||||
obj.ocr_status = OCRStatus.ERROR
|
||||
raise e
|
||||
|
||||
finally:
|
||||
file.ocr_status = OCRStatus.PROCESSED
|
||||
file.save(update_fields=["ocr_status", "ocr_raw_data"])
|
||||
obj.save(update_fields=["ocr_status", "ocr_raw_data"])
|
||||
|
||||
return True
|
||||
|
||||
@@ -42,7 +45,7 @@ def process_submission_file(file_id: str):
|
||||
if not file:
|
||||
return
|
||||
|
||||
_process_ocr_file(file)
|
||||
_process_ocr_file(file, file.file)
|
||||
|
||||
process_crops.enqueue(file_id)
|
||||
|
||||
@@ -65,4 +68,4 @@ def process_crop(crop_id: str):
|
||||
if not crop:
|
||||
return
|
||||
|
||||
_process_ocr_file(crop)
|
||||
_process_ocr_file(crop, crop.submission_file.file, crop=crop)
|
||||
|
||||
Reference in New Issue
Block a user