omrkit 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- omrkit/__init__.py +76 -0
- omrkit/__main__.py +6 -0
- omrkit/api/__init__.py +5 -0
- omrkit/api/app.py +75 -0
- omrkit/api/routes.py +490 -0
- omrkit/api/settings.py +55 -0
- omrkit/api/static/assets/index-BFfdzDls.js +224 -0
- omrkit/api/static/assets/index-DRPOjlrM.css +1 -0
- omrkit/api/static/index.html +23 -0
- omrkit/cli/__init__.py +5 -0
- omrkit/cli/app.py +440 -0
- omrkit/core/__init__.py +44 -0
- omrkit/core/auto_scanner.py +132 -0
- omrkit/core/benchmark.py +204 -0
- omrkit/core/migrator.py +149 -0
- omrkit/core/scanner.py +281 -0
- omrkit/core/validators.py +62 -0
- omrkit/generator/__init__.py +13 -0
- omrkit/generator/pdf_sheet.py +508 -0
- omrkit/generator/presets.py +109 -0
- omrkit/grading/__init__.py +6 -0
- omrkit/grading/analytics.py +177 -0
- omrkit/grading/grader.py +191 -0
- omrkit/io/__init__.py +37 -0
- omrkit/io/exporters.py +355 -0
- omrkit/io/loader.py +207 -0
- omrkit/models/__init__.py +65 -0
- omrkit/models/grading.py +222 -0
- omrkit/models/scan.py +100 -0
- omrkit/models/template.py +285 -0
- omrkit/py.typed +0 -0
- omrkit/vision/__init__.py +30 -0
- omrkit/vision/detector.py +313 -0
- omrkit/vision/grid_finder.py +280 -0
- omrkit/vision/preprocess.py +57 -0
- omrkit/vision/qr.py +83 -0
- omrkit/vision/registration.py +486 -0
- omrkit/vision/visualizer.py +99 -0
- omrkit-2.1.0.dist-info/METADATA +284 -0
- omrkit-2.1.0.dist-info/RECORD +43 -0
- omrkit-2.1.0.dist-info/WHEEL +4 -0
- omrkit-2.1.0.dist-info/entry_points.txt +2 -0
- omrkit-2.1.0.dist-info/licenses/LICENSE +21 -0
omrkit/io/exporters.py
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
"""CSV, JSON, formatted Excel (.xlsx), and PDF student scorecard exporters."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import csv
|
|
6
|
+
import io
|
|
7
|
+
import json
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from reportlab.lib import colors
|
|
11
|
+
from reportlab.lib.pagesizes import A4
|
|
12
|
+
from reportlab.pdfgen import canvas as rl_canvas
|
|
13
|
+
|
|
14
|
+
from omrkit.models.grading import GradingReport, StudentGrade
|
|
15
|
+
from omrkit.models.scan import SheetResult
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _collect_columns(sheets: list[SheetResult]) -> tuple[list[str], list[str]]:
|
|
19
|
+
grouped_cols: list[str] = []
|
|
20
|
+
field_cols: list[str] = []
|
|
21
|
+
grouped_field_prefixes: set[str] = set()
|
|
22
|
+
|
|
23
|
+
for s in sheets:
|
|
24
|
+
for gk in s.grouped_values:
|
|
25
|
+
if gk not in grouped_cols:
|
|
26
|
+
grouped_cols.append(gk)
|
|
27
|
+
grouped_field_prefixes.add(gk.lower() + "_")
|
|
28
|
+
for fk in s.fields:
|
|
29
|
+
if fk not in field_cols:
|
|
30
|
+
field_cols.append(fk)
|
|
31
|
+
|
|
32
|
+
# Keep ungrouped fields cleanly in output
|
|
33
|
+
filtered_fields = [
|
|
34
|
+
fk
|
|
35
|
+
for fk in field_cols
|
|
36
|
+
if not any(fk.lower().startswith(pref) for pref in grouped_field_prefixes)
|
|
37
|
+
]
|
|
38
|
+
return grouped_cols, filtered_fields
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def export_csv_string(
|
|
42
|
+
sheets: list[SheetResult],
|
|
43
|
+
report: GradingReport | None = None,
|
|
44
|
+
) -> str:
|
|
45
|
+
"""Serialize scan results (and optional grades) to a clean CSV string."""
|
|
46
|
+
buf = io.StringIO()
|
|
47
|
+
grouped_cols, field_cols = _collect_columns(sheets)
|
|
48
|
+
grade_by_id = {g.sheet_id: g for g in report.grades} if report else {}
|
|
49
|
+
|
|
50
|
+
headers = [
|
|
51
|
+
"sheet_id",
|
|
52
|
+
"source_file",
|
|
53
|
+
"page_index",
|
|
54
|
+
"overall_confidence",
|
|
55
|
+
"review_flags",
|
|
56
|
+
*grouped_cols,
|
|
57
|
+
]
|
|
58
|
+
if report:
|
|
59
|
+
headers.extend(["score", "max_score", "percentage", "correct", "incorrect", "blank"])
|
|
60
|
+
headers.extend(field_cols)
|
|
61
|
+
|
|
62
|
+
writer = csv.writer(buf)
|
|
63
|
+
writer.writerow(headers)
|
|
64
|
+
|
|
65
|
+
for s in sheets:
|
|
66
|
+
row: list[object] = [
|
|
67
|
+
s.sheet_id,
|
|
68
|
+
s.source_file,
|
|
69
|
+
s.page_index,
|
|
70
|
+
f"{s.overall_confidence:.4f}",
|
|
71
|
+
len(s.review_items),
|
|
72
|
+
*[s.grouped_values.get(gk, "") for gk in grouped_cols],
|
|
73
|
+
]
|
|
74
|
+
if report:
|
|
75
|
+
g = grade_by_id.get(s.sheet_id)
|
|
76
|
+
if g:
|
|
77
|
+
row.extend(
|
|
78
|
+
[g.raw_score, g.max_score, g.percentage, g.correct, g.incorrect, g.blank]
|
|
79
|
+
)
|
|
80
|
+
else:
|
|
81
|
+
row.extend(["", "", "", "", "", ""])
|
|
82
|
+
row.extend(s.fields[fk].value if fk in s.fields else "" for fk in field_cols)
|
|
83
|
+
writer.writerow(row)
|
|
84
|
+
|
|
85
|
+
return buf.getvalue()
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def export_csv(
|
|
89
|
+
sheets: list[SheetResult],
|
|
90
|
+
path: str | Path,
|
|
91
|
+
report: GradingReport | None = None,
|
|
92
|
+
) -> Path:
|
|
93
|
+
"""Write scan and optional grading results to a CSV file."""
|
|
94
|
+
dst = Path(path)
|
|
95
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
96
|
+
dst.write_text(export_csv_string(sheets, report=report), encoding="utf-8-sig")
|
|
97
|
+
return dst
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def export_json(
|
|
101
|
+
sheets: list[SheetResult],
|
|
102
|
+
path: str | Path,
|
|
103
|
+
report: GradingReport | None = None,
|
|
104
|
+
) -> Path:
|
|
105
|
+
"""Write structured scan results and optional grading report to JSON."""
|
|
106
|
+
dst = Path(path)
|
|
107
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
108
|
+
payload: dict[str, object] = {
|
|
109
|
+
"sheets": [s.model_dump(mode="json") for s in sheets],
|
|
110
|
+
}
|
|
111
|
+
if report is not None:
|
|
112
|
+
payload["grading_report"] = report.model_dump(mode="json")
|
|
113
|
+
dst.write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
|
114
|
+
return dst
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def export_excel_bytes(
|
|
118
|
+
sheets: list[SheetResult],
|
|
119
|
+
report: GradingReport | None = None,
|
|
120
|
+
) -> bytes:
|
|
121
|
+
"""Build a multi-sheet formatted Excel (.xlsx) workbook in memory."""
|
|
122
|
+
try:
|
|
123
|
+
from openpyxl import Workbook
|
|
124
|
+
from openpyxl.styles import Alignment, Font, PatternFill
|
|
125
|
+
except ImportError as exc:
|
|
126
|
+
raise RuntimeError(
|
|
127
|
+
"Excel export requires 'openpyxl'. Install with: pip install 'omrkit[excel]'"
|
|
128
|
+
) from exc
|
|
129
|
+
|
|
130
|
+
wb = Workbook()
|
|
131
|
+
header_fill = PatternFill(start_color="18202A", end_color="18202A", fill_type="solid")
|
|
132
|
+
header_font = Font(color="FFFFFF", bold=True)
|
|
133
|
+
warn_fill = PatternFill(start_color="FEF3C7", end_color="FEF3C7", fill_type="solid")
|
|
134
|
+
|
|
135
|
+
# Sheet 1: Responses
|
|
136
|
+
ws_resp = wb.active
|
|
137
|
+
assert ws_resp is not None
|
|
138
|
+
ws_resp.title = "Responses"
|
|
139
|
+
|
|
140
|
+
grouped_cols, field_cols = _collect_columns(sheets)
|
|
141
|
+
grade_by_id = {g.sheet_id: g for g in report.grades} if report else {}
|
|
142
|
+
|
|
143
|
+
headers = ["Sheet ID", "Source File", "Confidence", "Review Flags", *grouped_cols]
|
|
144
|
+
if report:
|
|
145
|
+
headers.extend(["Score", "Max Score", "Percent (%)", "Correct", "Incorrect", "Blank"])
|
|
146
|
+
headers.extend(field_cols)
|
|
147
|
+
ws_resp.append(headers)
|
|
148
|
+
|
|
149
|
+
for cell in ws_resp[1]:
|
|
150
|
+
cell.fill = header_fill
|
|
151
|
+
cell.font = header_font
|
|
152
|
+
cell.alignment = Alignment(horizontal="center")
|
|
153
|
+
|
|
154
|
+
for row_idx, s in enumerate(sheets, start=2):
|
|
155
|
+
row: list[object] = [
|
|
156
|
+
s.sheet_id,
|
|
157
|
+
s.source_file,
|
|
158
|
+
round(s.overall_confidence, 4),
|
|
159
|
+
len(s.review_items),
|
|
160
|
+
*[s.grouped_values.get(gk, "") for gk in grouped_cols],
|
|
161
|
+
]
|
|
162
|
+
if report:
|
|
163
|
+
g = grade_by_id.get(s.sheet_id)
|
|
164
|
+
if g:
|
|
165
|
+
row.extend(
|
|
166
|
+
[g.raw_score, g.max_score, g.percentage, g.correct, g.incorrect, g.blank]
|
|
167
|
+
)
|
|
168
|
+
else:
|
|
169
|
+
row.extend(["", "", "", "", "", ""])
|
|
170
|
+
row.extend(s.fields[fk].value if fk in s.fields else "" for fk in field_cols)
|
|
171
|
+
ws_resp.append(row)
|
|
172
|
+
if s.review_items:
|
|
173
|
+
ws_resp.cell(row=row_idx, column=4).fill = warn_fill
|
|
174
|
+
|
|
175
|
+
ws_resp.freeze_panes = "A2"
|
|
176
|
+
|
|
177
|
+
# Sheet 2 & 3: Summary & Item Psychometrics when graded
|
|
178
|
+
if report is not None:
|
|
179
|
+
ws_sum = wb.create_sheet("Exam Summary")
|
|
180
|
+
a = report.analytics
|
|
181
|
+
summary_rows = [
|
|
182
|
+
("Exam Name", report.exam_name),
|
|
183
|
+
("Grading Policy", report.policy_name),
|
|
184
|
+
("Students Graded", a.student_count),
|
|
185
|
+
("Questions", a.question_count),
|
|
186
|
+
("Mean Score", a.mean_score),
|
|
187
|
+
("Median Score", a.median_score),
|
|
188
|
+
("Std Dev", a.std_score),
|
|
189
|
+
("Min Score", a.min_score),
|
|
190
|
+
("Max Score", a.max_score),
|
|
191
|
+
("Mean Percentage (%)", a.mean_percentage),
|
|
192
|
+
("KR-20 Reliability", a.kr20_reliability if a.kr20_reliability is not None else "N/A"),
|
|
193
|
+
]
|
|
194
|
+
ws_sum.append(["Metric", "Value"])
|
|
195
|
+
for cell in ws_sum[1]:
|
|
196
|
+
cell.fill = header_fill
|
|
197
|
+
cell.font = header_font
|
|
198
|
+
for metric, val in summary_rows:
|
|
199
|
+
ws_sum.append([metric, val])
|
|
200
|
+
|
|
201
|
+
ws_items = wb.create_sheet("Item Analytics")
|
|
202
|
+
item_headers = [
|
|
203
|
+
"Question",
|
|
204
|
+
"Expected",
|
|
205
|
+
"Correct",
|
|
206
|
+
"Incorrect",
|
|
207
|
+
"Blank",
|
|
208
|
+
"Difficulty (p)",
|
|
209
|
+
"Discrimination (D)",
|
|
210
|
+
"Omit Rate",
|
|
211
|
+
"Quality Flag",
|
|
212
|
+
]
|
|
213
|
+
ws_items.append(item_headers)
|
|
214
|
+
for cell in ws_items[1]:
|
|
215
|
+
cell.fill = header_fill
|
|
216
|
+
cell.font = header_font
|
|
217
|
+
for item in a.items:
|
|
218
|
+
ws_items.append(
|
|
219
|
+
[
|
|
220
|
+
item.question_id,
|
|
221
|
+
item.expected,
|
|
222
|
+
item.correct_count,
|
|
223
|
+
item.incorrect_count,
|
|
224
|
+
item.blank_count,
|
|
225
|
+
item.difficulty_p,
|
|
226
|
+
item.discrimination_d,
|
|
227
|
+
item.omit_rate,
|
|
228
|
+
item.quality_flag,
|
|
229
|
+
]
|
|
230
|
+
)
|
|
231
|
+
ws_items.freeze_panes = "A2"
|
|
232
|
+
|
|
233
|
+
buf = io.BytesIO()
|
|
234
|
+
wb.save(buf)
|
|
235
|
+
return buf.getvalue()
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def export_excel(
|
|
239
|
+
sheets: list[SheetResult],
|
|
240
|
+
path: str | Path,
|
|
241
|
+
report: GradingReport | None = None,
|
|
242
|
+
) -> Path:
|
|
243
|
+
"""Write formatted Excel report to disk."""
|
|
244
|
+
dst = Path(path)
|
|
245
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
246
|
+
dst.write_bytes(export_excel_bytes(sheets, report=report))
|
|
247
|
+
return dst
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def export_pdf_scorecard_bytes(grade: StudentGrade, exam_name: str = "Exam Scorecard") -> bytes:
|
|
251
|
+
"""Render a clean, printable A4 PDF student scorecard in memory."""
|
|
252
|
+
buf = io.BytesIO()
|
|
253
|
+
w, h = A4
|
|
254
|
+
pdf = rl_canvas.Canvas(buf, pagesize=A4)
|
|
255
|
+
pdf.setTitle(f"Scorecard - {grade.sheet_id}")
|
|
256
|
+
|
|
257
|
+
def safe(text: str) -> str:
|
|
258
|
+
return text.translate(str.maketrans("çğıöşüÇĞİÖŞÜ", "cgiosuCGIOSU"))
|
|
259
|
+
|
|
260
|
+
ink = colors.HexColor("#14181F")
|
|
261
|
+
muted = colors.HexColor("#5C6773")
|
|
262
|
+
green = colors.HexColor("#0F8A5F")
|
|
263
|
+
red = colors.HexColor("#DC2626")
|
|
264
|
+
border = colors.HexColor("#D5DCE4")
|
|
265
|
+
|
|
266
|
+
# Header
|
|
267
|
+
pdf.setFillColor(ink)
|
|
268
|
+
pdf.setFont("Helvetica-Bold", 16)
|
|
269
|
+
pdf.drawString(42, h - 52, safe(exam_name))
|
|
270
|
+
pdf.setFillColor(muted)
|
|
271
|
+
pdf.setFont("Helvetica", 9)
|
|
272
|
+
pdf.drawString(42, h - 68, f"Sheet: {safe(grade.sheet_id)} | Booklet: {grade.booklet}")
|
|
273
|
+
|
|
274
|
+
# Summary KPI cards
|
|
275
|
+
y_card = h - 132
|
|
276
|
+
card_w = (w - 84 - 24) / 4.0
|
|
277
|
+
kpis = [
|
|
278
|
+
("SCORE", f"{grade.raw_score:.2f} / {grade.max_score:.2f}", ink),
|
|
279
|
+
("PERCENTAGE", f"{grade.percentage:.1f}%", green if grade.percentage >= 50 else red),
|
|
280
|
+
("CORRECT", str(grade.correct), green),
|
|
281
|
+
("WRONG / BLANK", f"{grade.incorrect} / {grade.blank}", red if grade.incorrect else muted),
|
|
282
|
+
]
|
|
283
|
+
for idx, (label, val, clr) in enumerate(kpis):
|
|
284
|
+
x = 42 + idx * (card_w + 8)
|
|
285
|
+
pdf.setStrokeColor(border)
|
|
286
|
+
pdf.setFillColor(colors.HexColor("#FAF9F6"))
|
|
287
|
+
pdf.roundRect(x, y_card, card_w, 48, 4, stroke=1, fill=1)
|
|
288
|
+
pdf.setFillColor(muted)
|
|
289
|
+
pdf.setFont("Helvetica-Bold", 7)
|
|
290
|
+
pdf.drawString(x + 10, y_card + 33, label)
|
|
291
|
+
pdf.setFillColor(clr)
|
|
292
|
+
pdf.setFont("Helvetica-Bold", 13)
|
|
293
|
+
pdf.drawString(x + 10, y_card + 13, val)
|
|
294
|
+
|
|
295
|
+
# Metadata row
|
|
296
|
+
y_cursor = y_card - 22
|
|
297
|
+
if grade.student_metadata:
|
|
298
|
+
meta_text = " | ".join(f"{k}: {v or '-'}" for k, v in grade.student_metadata.items())
|
|
299
|
+
pdf.setFillColor(ink)
|
|
300
|
+
pdf.setFont("Helvetica", 9)
|
|
301
|
+
pdf.drawString(42, y_cursor, safe(meta_text))
|
|
302
|
+
y_cursor -= 20
|
|
303
|
+
|
|
304
|
+
# Question verdict grid (4 columns)
|
|
305
|
+
pdf.setFillColor(ink)
|
|
306
|
+
pdf.setFont("Helvetica-Bold", 10)
|
|
307
|
+
pdf.drawString(42, y_cursor, "Question Breakdown")
|
|
308
|
+
y_cursor -= 16
|
|
309
|
+
|
|
310
|
+
cols = 4
|
|
311
|
+
col_w = (w - 84) / cols
|
|
312
|
+
row_h = 14
|
|
313
|
+
rows_per_col = max(1, (len(grade.verdicts) + cols - 1) // cols)
|
|
314
|
+
|
|
315
|
+
for idx, v in enumerate(grade.verdicts):
|
|
316
|
+
col = idx // rows_per_col
|
|
317
|
+
row = idx % rows_per_col
|
|
318
|
+
x = 42 + col * col_w
|
|
319
|
+
y = y_cursor - row * row_h
|
|
320
|
+
if y < 48:
|
|
321
|
+
break
|
|
322
|
+
pdf.setFont("Helvetica-Bold", 7.5)
|
|
323
|
+
pdf.setFillColor(ink)
|
|
324
|
+
pdf.drawString(x, y, f"{v.question_id}:")
|
|
325
|
+
pdf.setFont("Helvetica", 7.5)
|
|
326
|
+
mark_str = v.marked or "-"
|
|
327
|
+
if v.verdict in {"correct", "bonus"}:
|
|
328
|
+
pdf.setFillColor(green)
|
|
329
|
+
tag = "OK"
|
|
330
|
+
elif v.verdict == "blank":
|
|
331
|
+
pdf.setFillColor(muted)
|
|
332
|
+
tag = "--"
|
|
333
|
+
else:
|
|
334
|
+
pdf.setFillColor(red)
|
|
335
|
+
tag = "X"
|
|
336
|
+
pdf.drawString(x + 28, y, f"{mark_str} (Key: {v.expected}) [{tag}]")
|
|
337
|
+
|
|
338
|
+
pdf.setFillColor(muted)
|
|
339
|
+
pdf.setFont("Helvetica", 7)
|
|
340
|
+
pdf.drawString(42, 28, "Generated by OMRKit v2.0 — https://github.com/duhanguler/omr")
|
|
341
|
+
pdf.showPage()
|
|
342
|
+
pdf.save()
|
|
343
|
+
return buf.getvalue()
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def export_pdf_scorecard(
|
|
347
|
+
grade: StudentGrade,
|
|
348
|
+
path: str | Path,
|
|
349
|
+
exam_name: str = "Exam Scorecard",
|
|
350
|
+
) -> Path:
|
|
351
|
+
"""Write a student PDF scorecard to disk."""
|
|
352
|
+
dst = Path(path)
|
|
353
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
354
|
+
dst.write_bytes(export_pdf_scorecard_bytes(grade, exam_name=exam_name))
|
|
355
|
+
return dst
|
omrkit/io/loader.py
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"""Unified document loader for images (with EXIF auto-orientation) and multi-page PDFs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import io
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import cv2
|
|
10
|
+
import numpy as np
|
|
11
|
+
import pypdfium2 as pdfium
|
|
12
|
+
from PIL import Image, ImageOps
|
|
13
|
+
|
|
14
|
+
IMAGE_EXTENSIONS: frozenset[str] = frozenset(
|
|
15
|
+
{".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tif", ".tiff"}
|
|
16
|
+
)
|
|
17
|
+
PDF_EXTENSIONS: frozenset[str] = frozenset({".pdf"})
|
|
18
|
+
SUPPORTED_EXTENSIONS: frozenset[str] = IMAGE_EXTENSIONS | PDF_EXTENSIONS
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class DocumentLoadError(RuntimeError):
|
|
22
|
+
"""Raised when an input image or PDF cannot be opened or decoded."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(slots=True)
|
|
26
|
+
class LoadedPage:
|
|
27
|
+
"""A single decoded document page in standard BGR OpenCV format."""
|
|
28
|
+
|
|
29
|
+
source_name: str
|
|
30
|
+
page_index: int
|
|
31
|
+
image_bgr: np.ndarray
|
|
32
|
+
original_width: int
|
|
33
|
+
original_height: int
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _pil_to_bgr(img: Image.Image) -> np.ndarray:
|
|
37
|
+
"""Apply EXIF orientation and convert a PIL Image to an OpenCV BGR array."""
|
|
38
|
+
oriented = ImageOps.exif_transpose(img)
|
|
39
|
+
rgb = oriented.convert("RGB")
|
|
40
|
+
arr = np.asarray(rgb, dtype=np.uint8)
|
|
41
|
+
return cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _render_pdf_document(
|
|
45
|
+
pdf_doc: pdfium.PdfDocument,
|
|
46
|
+
source_name: str,
|
|
47
|
+
dpi: int = 200,
|
|
48
|
+
max_pages: int | None = None,
|
|
49
|
+
) -> list[LoadedPage]:
|
|
50
|
+
scale = dpi / 72.0
|
|
51
|
+
pages: list[LoadedPage] = []
|
|
52
|
+
try:
|
|
53
|
+
page_count = len(pdf_doc)
|
|
54
|
+
if max_pages is not None and page_count > max_pages:
|
|
55
|
+
raise DocumentLoadError(
|
|
56
|
+
f"PDF '{source_name}' has {page_count} pages, exceeding the configured limit of {max_pages}."
|
|
57
|
+
)
|
|
58
|
+
for idx in range(page_count):
|
|
59
|
+
page = pdf_doc[idx]
|
|
60
|
+
try:
|
|
61
|
+
bitmap = page.render(scale=scale)
|
|
62
|
+
pil_img = bitmap.to_pil()
|
|
63
|
+
bgr = _pil_to_bgr(pil_img)
|
|
64
|
+
h, w = bgr.shape[:2]
|
|
65
|
+
pages.append(
|
|
66
|
+
LoadedPage(
|
|
67
|
+
source_name=source_name,
|
|
68
|
+
page_index=idx,
|
|
69
|
+
image_bgr=bgr,
|
|
70
|
+
original_width=w,
|
|
71
|
+
original_height=h,
|
|
72
|
+
)
|
|
73
|
+
)
|
|
74
|
+
finally:
|
|
75
|
+
page.close()
|
|
76
|
+
finally:
|
|
77
|
+
pdf_doc.close()
|
|
78
|
+
return pages
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def load_document(
|
|
82
|
+
source: str | Path,
|
|
83
|
+
dpi: int = 200,
|
|
84
|
+
max_pages: int | None = None,
|
|
85
|
+
) -> list[LoadedPage]:
|
|
86
|
+
"""Load an image file or multi-page PDF into a list of EXIF-oriented BGR pages."""
|
|
87
|
+
path = Path(source)
|
|
88
|
+
if not path.exists():
|
|
89
|
+
raise DocumentLoadError(f"Input file does not exist: '{path}'")
|
|
90
|
+
if not path.is_file():
|
|
91
|
+
raise DocumentLoadError(f"Expected a file path, got directory: '{path}'")
|
|
92
|
+
|
|
93
|
+
ext = path.suffix.lower()
|
|
94
|
+
if ext not in SUPPORTED_EXTENSIONS:
|
|
95
|
+
raise DocumentLoadError(
|
|
96
|
+
f"Unsupported file format '{ext}' for '{path.name}'. "
|
|
97
|
+
f"Supported: {', '.join(sorted(SUPPORTED_EXTENSIONS))}"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
if ext in PDF_EXTENSIONS:
|
|
101
|
+
try:
|
|
102
|
+
pdf_doc = pdfium.PdfDocument(str(path))
|
|
103
|
+
return _render_pdf_document(
|
|
104
|
+
pdf_doc, source_name=path.name, dpi=dpi, max_pages=max_pages
|
|
105
|
+
)
|
|
106
|
+
except DocumentLoadError:
|
|
107
|
+
raise
|
|
108
|
+
except Exception as exc:
|
|
109
|
+
raise DocumentLoadError(f"Failed to decode PDF '{path.name}': {exc}") from exc
|
|
110
|
+
|
|
111
|
+
try:
|
|
112
|
+
with Image.open(path) as pil_img:
|
|
113
|
+
bgr = _pil_to_bgr(pil_img)
|
|
114
|
+
except Exception as exc:
|
|
115
|
+
raise DocumentLoadError(f"Failed to decode image '{path.name}': {exc}") from exc
|
|
116
|
+
|
|
117
|
+
h, w = bgr.shape[:2]
|
|
118
|
+
return [
|
|
119
|
+
LoadedPage(
|
|
120
|
+
source_name=path.name,
|
|
121
|
+
page_index=0,
|
|
122
|
+
image_bgr=bgr,
|
|
123
|
+
original_width=w,
|
|
124
|
+
original_height=h,
|
|
125
|
+
)
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def load_bytes(
|
|
130
|
+
data: bytes,
|
|
131
|
+
filename: str,
|
|
132
|
+
dpi: int = 200,
|
|
133
|
+
max_pages: int | None = None,
|
|
134
|
+
) -> list[LoadedPage]:
|
|
135
|
+
"""Decode uploaded image or PDF bytes directly in memory."""
|
|
136
|
+
ext = Path(filename).suffix.lower()
|
|
137
|
+
if ext not in SUPPORTED_EXTENSIONS:
|
|
138
|
+
raise DocumentLoadError(
|
|
139
|
+
f"Unsupported file format '{ext}' for '{filename}'. "
|
|
140
|
+
f"Supported: {', '.join(sorted(SUPPORTED_EXTENSIONS))}"
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
if ext in PDF_EXTENSIONS:
|
|
144
|
+
try:
|
|
145
|
+
pdf_doc = pdfium.PdfDocument(data)
|
|
146
|
+
return _render_pdf_document(pdf_doc, source_name=filename, dpi=dpi, max_pages=max_pages)
|
|
147
|
+
except DocumentLoadError:
|
|
148
|
+
raise
|
|
149
|
+
except Exception as exc:
|
|
150
|
+
raise DocumentLoadError(f"Failed to decode PDF '{filename}': {exc}") from exc
|
|
151
|
+
|
|
152
|
+
try:
|
|
153
|
+
with Image.open(io.BytesIO(data)) as pil_img:
|
|
154
|
+
bgr = _pil_to_bgr(pil_img)
|
|
155
|
+
except Exception as exc:
|
|
156
|
+
raise DocumentLoadError(f"Failed to decode image '{filename}': {exc}") from exc
|
|
157
|
+
|
|
158
|
+
h, w = bgr.shape[:2]
|
|
159
|
+
return [
|
|
160
|
+
LoadedPage(
|
|
161
|
+
source_name=filename,
|
|
162
|
+
page_index=0,
|
|
163
|
+
image_bgr=bgr,
|
|
164
|
+
original_width=w,
|
|
165
|
+
original_height=h,
|
|
166
|
+
)
|
|
167
|
+
]
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def discover_documents(path: str | Path, recursive: bool = False) -> list[Path]:
|
|
171
|
+
"""Discover all supported image and PDF files from a file or directory path."""
|
|
172
|
+
target = Path(path)
|
|
173
|
+
if not target.exists():
|
|
174
|
+
raise DocumentLoadError(f"Input path does not exist: '{target}'")
|
|
175
|
+
if target.is_file():
|
|
176
|
+
if target.suffix.lower() not in SUPPORTED_EXTENSIONS:
|
|
177
|
+
raise DocumentLoadError(f"Unsupported file type: '{target}'")
|
|
178
|
+
return [target]
|
|
179
|
+
|
|
180
|
+
iterator = target.rglob("*") if recursive else target.iterdir()
|
|
181
|
+
files = [p for p in iterator if p.is_file() and p.suffix.lower() in SUPPORTED_EXTENSIONS]
|
|
182
|
+
return sorted(files)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def strip_exif_and_save(
|
|
186
|
+
src: str | Path,
|
|
187
|
+
dst: str | Path,
|
|
188
|
+
quality: int = 92,
|
|
189
|
+
max_dimension: int | None = None,
|
|
190
|
+
) -> tuple[int, int]:
|
|
191
|
+
"""Apply EXIF orientation permanently to pixels and write a clean metadata-free image."""
|
|
192
|
+
src_path = Path(src)
|
|
193
|
+
dst_path = Path(dst)
|
|
194
|
+
dst_path.parent.mkdir(parents=True, exist_ok=True)
|
|
195
|
+
|
|
196
|
+
with Image.open(src_path) as img:
|
|
197
|
+
oriented = ImageOps.exif_transpose(img).convert("RGB")
|
|
198
|
+
if max_dimension is not None:
|
|
199
|
+
oriented.thumbnail((max_dimension, max_dimension), Image.Resampling.LANCZOS)
|
|
200
|
+
# Re-create pixel-only image to guarantee zero EXIF/GPS/ICC leftovers
|
|
201
|
+
clean = Image.new("RGB", oriented.size)
|
|
202
|
+
clean.paste(oriented)
|
|
203
|
+
if dst_path.suffix.lower() in {".jpg", ".jpeg"}:
|
|
204
|
+
clean.save(dst_path, quality=quality, optimize=True)
|
|
205
|
+
else:
|
|
206
|
+
clean.save(dst_path)
|
|
207
|
+
return clean.size
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Public Pydantic data models for OMRKit."""
|
|
2
|
+
|
|
3
|
+
from omrkit.models.grading import (
|
|
4
|
+
AnswerKey,
|
|
5
|
+
ExamAnalytics,
|
|
6
|
+
GradingPolicy,
|
|
7
|
+
GradingReport,
|
|
8
|
+
ItemPsychometrics,
|
|
9
|
+
QuestionVerdict,
|
|
10
|
+
ScoreHistogramBin,
|
|
11
|
+
SectionRule,
|
|
12
|
+
SectionScore,
|
|
13
|
+
StudentGrade,
|
|
14
|
+
)
|
|
15
|
+
from omrkit.models.scan import (
|
|
16
|
+
BatchResult,
|
|
17
|
+
BubbleReading,
|
|
18
|
+
FieldReading,
|
|
19
|
+
FieldStatus,
|
|
20
|
+
ReviewItem,
|
|
21
|
+
SheetResult,
|
|
22
|
+
)
|
|
23
|
+
from omrkit.models.template import (
|
|
24
|
+
BlockDirection,
|
|
25
|
+
BubbleCoord,
|
|
26
|
+
DetectionConfig,
|
|
27
|
+
FieldBlock,
|
|
28
|
+
FieldType,
|
|
29
|
+
OriginMode,
|
|
30
|
+
RegistrationConfig,
|
|
31
|
+
RegistrationMode,
|
|
32
|
+
Template,
|
|
33
|
+
ValidatorType,
|
|
34
|
+
expand_field_labels,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"AnswerKey",
|
|
39
|
+
"BatchResult",
|
|
40
|
+
"BlockDirection",
|
|
41
|
+
"BubbleCoord",
|
|
42
|
+
"BubbleReading",
|
|
43
|
+
"DetectionConfig",
|
|
44
|
+
"ExamAnalytics",
|
|
45
|
+
"FieldBlock",
|
|
46
|
+
"FieldReading",
|
|
47
|
+
"FieldStatus",
|
|
48
|
+
"FieldType",
|
|
49
|
+
"GradingPolicy",
|
|
50
|
+
"GradingReport",
|
|
51
|
+
"ItemPsychometrics",
|
|
52
|
+
"OriginMode",
|
|
53
|
+
"QuestionVerdict",
|
|
54
|
+
"RegistrationConfig",
|
|
55
|
+
"RegistrationMode",
|
|
56
|
+
"ReviewItem",
|
|
57
|
+
"ScoreHistogramBin",
|
|
58
|
+
"SectionRule",
|
|
59
|
+
"SectionScore",
|
|
60
|
+
"SheetResult",
|
|
61
|
+
"StudentGrade",
|
|
62
|
+
"Template",
|
|
63
|
+
"ValidatorType",
|
|
64
|
+
"expand_field_labels",
|
|
65
|
+
]
|