Improve OCR

This commit is contained in:
Nexus 2023-05-30 19:53:47 +01:00
parent ad28f0fd6b
commit 6d3b5d2d63
Signed by: nex
GPG key ID: 0FA334385D0B689F
3 changed files with 28 additions and 24 deletions

View file

@ -1397,20 +1397,23 @@ class OtherCog(commands.Cog):
await ctx.defer()
timings: Dict[str, float] = {}
attachment: discord.Attachment
with Timer(timings, "download attachment"):
with Timer() as _t:
data = await attachment.read()
file = io.BytesIO(data)
file.seek(0)
with Timer(timings, "Parse image"):
timings["Download attachment"] = _t.total
with Timer() as _t:
img = await self.bot.loop.run_in_executor(None, Image.open, file)
timings["Parse image"] = _t.total
try:
with Timer(timings, "Run OCR"):
with Timer() as _t:
text = await self.bot.loop.run_in_executor(None, pytesseract.image_to_string, img)
timings["Perform OCR"] = _t.total
except pytesseract.TesseractError as e:
return await ctx.respond(f"Failed to perform OCR: `{e}`")
if len(text) > 4096:
with Timer(timings, "Upload text to mystbin"):
with Timer() as _t:
try:
response = await self.http.put(
"https://api.mystb.in/paste",
@ -1434,14 +1437,21 @@ class OtherCog(commands.Cog):
colour=discord.Colour.dark_theme()
)
await ctx.respond(embed=embed)
timings["Upload text to mystbin"] = _t.total
elif len(text) <= 1500 and text.count("\n") <= 7:
with Timer() as _t:
await ctx.respond(embed=discord.Embed(description=text))
timings["Respond (Text)"] = _t.total
else:
with Timer(timings, "Respond (File)"):
with Timer() as _t:
out_file = io.BytesIO(text.encode("utf-8", "replace"))
await ctx.respond(file=discord.File(out_file, filename="ocr.txt"))
timings["Respond (File)"] = _t.total
if timings:
text = "Timings:\n" + "\n".join("%s: %s" % (k.title(), v) for k, v in timings.items())
await ctx.edit(
content="Timings:\n" + "\n".join("%s: %s" % (k.title(), v) for k, v in timings.items()),
content=text,
)
@commands.slash_command(name="image-to-gif")

View file

@ -18,9 +18,6 @@ def schedule_times():
for h in range(24):
for m in range(0, 60, 15):
times.append(time(h, m, 0))
console.print("[TimeTable Updater Task] Update times:")
for _time in times:
console.print("[TimeTable Updater Task] {0.hour}:{0.minute}".format(_time))
return times
@ -220,9 +217,9 @@ class TimeTableCog(commands.Cog):
"""Shows the current/next lesson."""
if date:
try:
date = datetime.strptime(date, "%d/%m/%Y %H:%M")
date = datetime.strptime(date, "%d/%m/%y %H:%M")
except ValueError:
return await ctx.respond("Invalid date (DD/MM/YYYY HH:MM).")
return await ctx.respond("Invalid date (DD/MM/YY HH:MM).")
else:
date = datetime.now()
await ctx.defer()
@ -237,12 +234,11 @@ class TimeTableCog(commands.Cog):
"""Shows the timetable for today/the specified date"""
if date:
try:
date = datetime.strptime(date, "%d/%m/%Y")
date = datetime.strptime(date, "%d/%m/%y")
except ValueError:
return await ctx.respond("Invalid date (DD/MM/YYYY).")
return await ctx.respond("Invalid date (DD/MM/YY).")
else:
date = datetime.now()
date = datetime.now()
text = self.format_timetable_message(date)
view = TimeTableDaySwitcherView(ctx.user, self, date)
@ -256,17 +252,13 @@ class TimeTableCog(commands.Cog):
paper_2 = datetime(2023, 6, 21, 12, tzinfo=timezone.utc)
paper_1_url = "https://classroom.google.com/c/NTQ5MzE5ODg0ODQ2/m/NTUzNjI5NjAyMDQ2/details"
paper_2_url = "https://classroom.google.com/c/NTQ5MzE5ODg0ODQ2/m/NjA1Nzk3ODQ4OTg0/details"
response = await ctx.respond(
await ctx.respond(
f"Paper A: [{discord.utils.format_dt(paper_1, 'R')}]({paper_1_url})\n"
f"Paper B: [{discord.utils.format_dt(paper_2, 'R')}]({paper_2_url})"
)
await asyncio.sleep(2)
if response.message:
await response.message.edit(suppress=True)
else:
message_id = (await ctx.interaction.original_response()).id
message = await ctx.channel.fetch_message(message_id)
await message.edit(suppress=True)
message_id = (await ctx.interaction.original_response()).id
message = await ctx.channel.fetch_message(message_id)
await message.edit(suppress=True)
def setup(bot):

View file

@ -39,8 +39,10 @@ class Timer:
@property
def total(self) -> float:
if not self._start_time and not self._end_time:
raise RuntimeError("Timer has not been started or stopped.")
if not self._start_time:
raise RuntimeError("Timer has not been started.")
if not self._end_time:
return time.time() - self._start_time
return self._end_time - self._start_time
def __enter__(self):