mirror of
https://github.com/nexy7574/LCC-bot.git
synced 2024-09-19 18:16:34 +01:00
Improve OCR
This commit is contained in:
parent
ad28f0fd6b
commit
6d3b5d2d63
3 changed files with 28 additions and 24 deletions
|
@ -1397,20 +1397,23 @@ class OtherCog(commands.Cog):
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
timings: Dict[str, float] = {}
|
timings: Dict[str, float] = {}
|
||||||
attachment: discord.Attachment
|
attachment: discord.Attachment
|
||||||
with Timer(timings, "download attachment"):
|
with Timer() as _t:
|
||||||
data = await attachment.read()
|
data = await attachment.read()
|
||||||
file = io.BytesIO(data)
|
file = io.BytesIO(data)
|
||||||
file.seek(0)
|
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)
|
img = await self.bot.loop.run_in_executor(None, Image.open, file)
|
||||||
|
timings["Parse image"] = _t.total
|
||||||
try:
|
try:
|
||||||
with Timer(timings, "Run OCR"):
|
with Timer() as _t:
|
||||||
text = await self.bot.loop.run_in_executor(None, pytesseract.image_to_string, img)
|
text = await self.bot.loop.run_in_executor(None, pytesseract.image_to_string, img)
|
||||||
|
timings["Perform OCR"] = _t.total
|
||||||
except pytesseract.TesseractError as e:
|
except pytesseract.TesseractError as e:
|
||||||
return await ctx.respond(f"Failed to perform OCR: `{e}`")
|
return await ctx.respond(f"Failed to perform OCR: `{e}`")
|
||||||
|
|
||||||
if len(text) > 4096:
|
if len(text) > 4096:
|
||||||
with Timer(timings, "Upload text to mystbin"):
|
with Timer() as _t:
|
||||||
try:
|
try:
|
||||||
response = await self.http.put(
|
response = await self.http.put(
|
||||||
"https://api.mystb.in/paste",
|
"https://api.mystb.in/paste",
|
||||||
|
@ -1434,14 +1437,21 @@ class OtherCog(commands.Cog):
|
||||||
colour=discord.Colour.dark_theme()
|
colour=discord.Colour.dark_theme()
|
||||||
)
|
)
|
||||||
await ctx.respond(embed=embed)
|
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:
|
else:
|
||||||
with Timer(timings, "Respond (File)"):
|
with Timer() as _t:
|
||||||
out_file = io.BytesIO(text.encode("utf-8", "replace"))
|
out_file = io.BytesIO(text.encode("utf-8", "replace"))
|
||||||
await ctx.respond(file=discord.File(out_file, filename="ocr.txt"))
|
await ctx.respond(file=discord.File(out_file, filename="ocr.txt"))
|
||||||
|
timings["Respond (File)"] = _t.total
|
||||||
|
|
||||||
if timings:
|
if timings:
|
||||||
|
text = "Timings:\n" + "\n".join("%s: %s" % (k.title(), v) for k, v in timings.items())
|
||||||
await ctx.edit(
|
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")
|
@commands.slash_command(name="image-to-gif")
|
||||||
|
|
|
@ -18,9 +18,6 @@ def schedule_times():
|
||||||
for h in range(24):
|
for h in range(24):
|
||||||
for m in range(0, 60, 15):
|
for m in range(0, 60, 15):
|
||||||
times.append(time(h, m, 0))
|
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
|
return times
|
||||||
|
|
||||||
|
|
||||||
|
@ -220,9 +217,9 @@ class TimeTableCog(commands.Cog):
|
||||||
"""Shows the current/next lesson."""
|
"""Shows the current/next lesson."""
|
||||||
if date:
|
if date:
|
||||||
try:
|
try:
|
||||||
date = datetime.strptime(date, "%d/%m/%Y %H:%M")
|
date = datetime.strptime(date, "%d/%m/%y %H:%M")
|
||||||
except ValueError:
|
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:
|
else:
|
||||||
date = datetime.now()
|
date = datetime.now()
|
||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
|
@ -237,12 +234,11 @@ class TimeTableCog(commands.Cog):
|
||||||
"""Shows the timetable for today/the specified date"""
|
"""Shows the timetable for today/the specified date"""
|
||||||
if date:
|
if date:
|
||||||
try:
|
try:
|
||||||
date = datetime.strptime(date, "%d/%m/%Y")
|
date = datetime.strptime(date, "%d/%m/%y")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return await ctx.respond("Invalid date (DD/MM/YYYY).")
|
return await ctx.respond("Invalid date (DD/MM/YY).")
|
||||||
else:
|
else:
|
||||||
date = datetime.now()
|
date = datetime.now()
|
||||||
date = datetime.now()
|
|
||||||
|
|
||||||
text = self.format_timetable_message(date)
|
text = self.format_timetable_message(date)
|
||||||
view = TimeTableDaySwitcherView(ctx.user, self, date)
|
view = TimeTableDaySwitcherView(ctx.user, self, date)
|
||||||
|
@ -256,14 +252,10 @@ class TimeTableCog(commands.Cog):
|
||||||
paper_2 = datetime(2023, 6, 21, 12, tzinfo=timezone.utc)
|
paper_2 = datetime(2023, 6, 21, 12, tzinfo=timezone.utc)
|
||||||
paper_1_url = "https://classroom.google.com/c/NTQ5MzE5ODg0ODQ2/m/NTUzNjI5NjAyMDQ2/details"
|
paper_1_url = "https://classroom.google.com/c/NTQ5MzE5ODg0ODQ2/m/NTUzNjI5NjAyMDQ2/details"
|
||||||
paper_2_url = "https://classroom.google.com/c/NTQ5MzE5ODg0ODQ2/m/NjA1Nzk3ODQ4OTg0/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 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})"
|
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_id = (await ctx.interaction.original_response()).id
|
||||||
message = await ctx.channel.fetch_message(message_id)
|
message = await ctx.channel.fetch_message(message_id)
|
||||||
await message.edit(suppress=True)
|
await message.edit(suppress=True)
|
||||||
|
|
|
@ -39,8 +39,10 @@ class Timer:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def total(self) -> float:
|
def total(self) -> float:
|
||||||
if not self._start_time and not self._end_time:
|
if not self._start_time:
|
||||||
raise RuntimeError("Timer has not been started or stopped.")
|
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
|
return self._end_time - self._start_time
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
|
Loading…
Reference in a new issue