From b16ff88a79ede312139cdb692e6e3daafa67d4c1 Mon Sep 17 00:00:00 2001 From: Riley Housden Date: Mon, 22 Aug 2022 14:35:13 -0400 Subject: [PATCH] Added reporting for a failed OCR match --- ext/libcal.py | 11 +++++++++-- lib/ocr.py | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ext/libcal.py b/ext/libcal.py index 141e53d..eec9da5 100644 --- a/ext/libcal.py +++ b/ext/libcal.py @@ -10,7 +10,7 @@ from typing import Optional from lib.config import load_config from lib.gcal import GoogleCalendarAPI from lib.room import Room -from lib.ocr import get_room_data, NoMatchException +from lib.ocr import get_room_data, NoMatchException, get_image_string plugin = lightbulb.Plugin("LibCal") config = load_config() @@ -82,8 +82,15 @@ async def book_error_handler(event: lightbulb.CommandErrorEvent): print("ERROR TYPE:", type(event.exception)) if isinstance(event.exception.__cause__, NoMatchException): embed = hikari.Embed(title="Huh, I can't read that", - description="Could you try another picture?") + description="Could you try another picture?\nI'll send a notice to the bot's maintainer. :)") await event.context.respond(embed=embed) + embed.title = "Image match failed!" + embed.description = f"I failed to find any bookings in the attached image." + embed.add_field("Image text", get_image_string(event.context.options.img)) + embed.set_image(event.context.options.img) + owners = event.context.bot.fetch_owner_ids() + for user_id in owners: + await event.context.bot.rest.fetch_user(user_id).send(embed) else: embed = hikari.Embed( title="Booking Error", description="Whelp, better luck next time I guess... the images used are attached.") diff --git a/lib/ocr.py b/lib/ocr.py index 343f66e..8d1ed65 100644 --- a/lib/ocr.py +++ b/lib/ocr.py @@ -67,7 +67,7 @@ def get_room_data(img: bytes) -> list[Room]: rooms: list[Room] = [] start_time: datetime | None = None end_time: datetime | None = None - img_string = image_to_string(Image.open(BytesIO(img))) + img_string = get_image_string(img) img_string = correct_newlines(img_string) img_string = correct_commas(img_string) matches = re.finditer(RE_STRING, img_string) @@ -85,3 +85,6 @@ def get_room_data(img: bytes) -> list[Room]: if len(rooms) == 0: raise NoMatchException return rooms + +def get_image_string(img: bytes) -> str: + return image_to_string(Image.open(BytesIO(img))) \ No newline at end of file