优化课表识别并支持重新识别

This commit is contained in:
2026-11-13 14:35:28 +08:00
parent 1de03f8ff2
commit 2fb2c5e1ad
5 changed files with 199 additions and 31 deletions
+12 -1
View File
@@ -61,6 +61,17 @@ class TimetableOccupancyTests(unittest.TestCase):
result=ocr.occupancy_from_blocks(mistaken,blocks,(680,600))
self.assertEqual(result['cells'],normal['cells'])
def test_grid_handles_month_label_merged_into_monday_header(self):
raw=sample_image()
complete=ocr.occupancy_from_grid(headers(),raw,(680,600))
compact = [line(55,100,'11月16一')] + [
line(200+80*i,100,f'{17+i}{weekday}') for i,weekday in enumerate('二三四五六日')
] + [line(35,160+40*i,str(i+1)) for i in range(10)]
result=ocr.occupancy_from_grid(compact,raw,(680,600))
self.assertIsNotNone(result)
self.assertEqual(result['method'],'color_grid')
self.assertEqual(result['cells'],complete['cells'])
def test_layout_extraction_still_runs_when_monday_and_morning_are_empty(self):
image=Image.new('RGB',(680,900),'white');draw=ImageDraw.Draw(image)
draw.rectangle((325,500,395,650),fill='#8ba3fc')
@@ -73,7 +84,7 @@ class TimetableOccupancyTests(unittest.TestCase):
raw=sample_image()
with patch.object(ocr,'layout_lines',return_value=headers()) as layout,patch.object(ocr,'read_image') as full_ocr:
first=ocr.recognize(raw)
self.assertEqual(first['method'],'color_blocks');self.assertFalse(first['cache_hit'])
self.assertEqual(first['method'],'color_grid');self.assertFalse(first['cache_hit'])
self.assertEqual(first['raw_text'],'');self.assertTrue(all(c['courses']=='' for c in first['cells']))
first['cells'][0]['status']='changed'
second=ocr.recognize(raw)
+26 -1
View File
@@ -10,7 +10,7 @@ from pathlib import Path
from unittest.mock import patch
from PIL import Image
from sqlalchemy import select, func
from sqlalchemy import select, func, update
from server import timetable_ocr as ocr
from server.models import timetables
from test_api import ApiTestCase, auth, register
@@ -162,6 +162,31 @@ class TimetableTests(ApiTestCase):
self.assertEqual((await self.client.get(item['image_url'],headers=auth(user['token']))).status_code,200)
self.assertEqual((await self.confirm(user,item)).status_code,200)
async def test_admin_can_reprocess_saved_images_without_overwriting_confirmed_rows(self):
user = await register(self.client,'timetablereprocess')
item = await self.upload(user)
async with self.sessions() as db:
await db.execute(update(timetables).where(
timetables.c.user_id == user['user']['id'], timetables.c.week_type == 'odd'
).values(structure=json.dumps(ocr.blank_structure()),ocr_status='needs_review'))
await db.commit()
endpoint='/api/timetables/reprocess'
payload={'user_ids':[user['user']['id']],'week_types':['odd']}
self.assertEqual((await self.client.post(endpoint,headers=auth(user['token']),json=payload)).status_code,401)
denied=await self.client.post(endpoint,headers=self.export_headers(user,'wrong-key'),json=payload)
self.assertEqual(denied.status_code,401)
result=await self.client.post(endpoint,headers=self.export_headers(user),json=payload)
self.assertEqual(result.status_code,200,result.text)
self.assertEqual(result.json()['results'][0]['status'],'recognized')
detail=(await self.client.get('/api/timetables/'+user['user']['id']+'/odd',headers=auth(user['token']))).json()['timetable']
self.assertEqual(detail['structure']['cells'][0]['status'],'busy')
self.assertNotEqual(detail['revision'],item['revision'])
confirmed=await self.confirm(user,detail)
self.assertEqual(confirmed.status_code,200,confirmed.text)
skipped=await self.client.post(endpoint,headers=self.export_headers(user),json=payload)
self.assertEqual(skipped.json()['results'][0]['status'],'skipped_confirmed')
async def test_concurrent_uploads_keep_only_latest_one_file_for_each_week(self):
engine = self.m.create_async_engine('sqlite+aiosqlite:///'+(self.root/'concurrency.db').as_posix())
try: