feat(ocr): add guided OCR mode using existing annotation regions
When a document has manually drawn annotation boxes, the user can now enable "Nur annotierte Bereiche" in the OCR trigger panel. The engine skips layout detection entirely and runs recognition only within the pre-drawn bounding boxes, preserving manual transcription blocks. - Python: adds OcrRegion model, extend OcrRequest/OcrBlock; guided branch in /ocr/stream groups by page and crops each region - Engines: add extract_region_text() to both Kraken and Surya - Java: adds OcrBlockResult.annotationId, OcrClient.OcrRegion, TriggerOcrDTO.useExistingAnnotations; OcrAsyncRunner dispatches to upsertGuidedBlock when annotationId is present; OcrService threads the flag through to runSingleDocument - TranscriptionService: adds upsertGuidedBlock (creates, updates OCR, or preserves MANUAL blocks) - Frontend: guided OCR toggle in OcrTrigger shown when blocks exist; skips destructive-replace confirmation in guided mode Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -51,7 +51,8 @@ public class OcrController {
|
||||
@RequestBody TriggerOcrDTO dto,
|
||||
Authentication authentication) {
|
||||
UUID userId = resolveUserId(authentication);
|
||||
UUID jobId = ocrService.startOcr(documentId, dto.getScriptType(), userId);
|
||||
UUID jobId = ocrService.startOcr(documentId, dto.getScriptType(), userId,
|
||||
Boolean.TRUE.equals(dto.getUseExistingAnnotations()));
|
||||
return Map.of("jobId", jobId);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,4 +10,5 @@ import org.raddatz.familienarchiv.model.ScriptType;
|
||||
@AllArgsConstructor
|
||||
public class TriggerOcrDTO {
|
||||
private ScriptType scriptType;
|
||||
private Boolean useExistingAnnotations = false;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ public interface TranscriptionBlockRepository extends JpaRepository<Transcriptio
|
||||
|
||||
Optional<TranscriptionBlock> findByIdAndDocumentId(UUID id, UUID documentId);
|
||||
|
||||
Optional<TranscriptionBlock> findByAnnotationId(UUID annotationId);
|
||||
|
||||
int countByDocumentId(UUID documentId);
|
||||
|
||||
@Query("""
|
||||
|
||||
@@ -32,6 +32,11 @@ public class OcrAsyncRunner {
|
||||
|
||||
@Async
|
||||
public void runSingleDocument(UUID jobId, UUID documentId, UUID userId) {
|
||||
runSingleDocument(jobId, documentId, userId, false);
|
||||
}
|
||||
|
||||
@Async
|
||||
public void runSingleDocument(UUID jobId, UUID documentId, UUID userId, boolean useExistingAnnotations) {
|
||||
OcrJob job = ocrJobRepository.findById(jobId).orElse(null);
|
||||
if (job == null) return;
|
||||
|
||||
@@ -49,7 +54,18 @@ public class OcrAsyncRunner {
|
||||
|
||||
try {
|
||||
updateProgress(job, "LOADING");
|
||||
clearExistingBlocks(documentId);
|
||||
|
||||
List<OcrClient.OcrRegion> regions = null;
|
||||
if (useExistingAnnotations) {
|
||||
regions = annotationService.listAnnotations(documentId).stream()
|
||||
.map(a -> new OcrClient.OcrRegion(
|
||||
a.getId().toString(), a.getPageNumber(),
|
||||
a.getX(), a.getY(), a.getWidth(), a.getHeight()))
|
||||
.toList();
|
||||
} else {
|
||||
clearExistingBlocks(documentId);
|
||||
}
|
||||
|
||||
String pdfUrl = fileService.generatePresignedUrl(doc.getFilePath());
|
||||
|
||||
AtomicInteger blockCounter = new AtomicInteger(0);
|
||||
@@ -57,7 +73,7 @@ public class OcrAsyncRunner {
|
||||
AtomicInteger skippedPages = new AtomicInteger(0);
|
||||
AtomicInteger totalPages = new AtomicInteger(0);
|
||||
|
||||
ocrClient.streamBlocks(pdfUrl, doc.getScriptType(), event -> {
|
||||
ocrClient.streamBlocks(pdfUrl, doc.getScriptType(), regions, event -> {
|
||||
switch (event) {
|
||||
case OcrStreamEvent.Start start -> {
|
||||
totalPages.set(start.totalPages());
|
||||
@@ -204,14 +220,21 @@ public class OcrAsyncRunner {
|
||||
|
||||
void createSingleBlock(UUID documentId, OcrBlockResult block,
|
||||
UUID userId, String fileHash, int sortOrder) {
|
||||
CreateAnnotationDTO annotationDTO = new CreateAnnotationDTO(
|
||||
block.pageNumber(), block.x(), block.y(),
|
||||
block.width(), block.height(), OCR_ANNOTATION_COLOR);
|
||||
if (block.annotationId() != null) {
|
||||
// Guided mode — annotation already exists; upsert the text block only
|
||||
transcriptionService.upsertGuidedBlock(
|
||||
documentId, UUID.fromString(block.annotationId()), block.text(), userId);
|
||||
} else {
|
||||
// Normal mode — create a new annotation and a new OCR block
|
||||
CreateAnnotationDTO annotationDTO = new CreateAnnotationDTO(
|
||||
block.pageNumber(), block.x(), block.y(),
|
||||
block.width(), block.height(), OCR_ANNOTATION_COLOR);
|
||||
|
||||
DocumentAnnotation annotation = annotationService.createOcrAnnotation(
|
||||
documentId, annotationDTO, userId, fileHash, block.polygon());
|
||||
DocumentAnnotation annotation = annotationService.createOcrAnnotation(
|
||||
documentId, annotationDTO, userId, fileHash, block.polygon());
|
||||
|
||||
transcriptionService.createOcrBlock(documentId, annotation.getId(),
|
||||
block.text(), sortOrder, userId);
|
||||
transcriptionService.createOcrBlock(documentId, annotation.getId(),
|
||||
block.text(), sortOrder, userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,5 +12,6 @@ public record OcrBlockResult(
|
||||
double width,
|
||||
double height,
|
||||
List<List<Double>> polygon,
|
||||
String text
|
||||
String text,
|
||||
String annotationId // null in normal mode; set in guided mode to link back to existing annotation
|
||||
) {}
|
||||
|
||||
@@ -10,6 +10,14 @@ import java.util.function.Consumer;
|
||||
public interface OcrClient {
|
||||
List<OcrBlockResult> extractBlocks(String pdfUrl, ScriptType scriptType);
|
||||
|
||||
/**
|
||||
* A pre-drawn annotation region to use as guidance for OCR.
|
||||
* When regions are provided, the OCR engine crops to each region and
|
||||
* runs recognition only within that area, skipping full-page layout detection.
|
||||
*/
|
||||
record OcrRegion(String annotationId, int pageNumber,
|
||||
double x, double y, double width, double height) {}
|
||||
|
||||
/**
|
||||
* Send a training ZIP to the OCR service for fine-tuning the Kurrent model.
|
||||
*
|
||||
@@ -32,8 +40,12 @@ public interface OcrClient {
|
||||
* Stream OCR results page-by-page via NDJSON. Implementations should override
|
||||
* this method. The default exists only for backward compatibility during migration
|
||||
* — it calls extractBlocks() and synthesizes events from the collected result.
|
||||
*
|
||||
* @param regions optional list of pre-drawn annotation regions; when non-null,
|
||||
* the OCR service runs in guided mode (crop + recognize per region)
|
||||
*/
|
||||
default void streamBlocks(String pdfUrl, ScriptType scriptType, Consumer<OcrStreamEvent> handler) {
|
||||
default void streamBlocks(String pdfUrl, ScriptType scriptType,
|
||||
List<OcrRegion> regions, Consumer<OcrStreamEvent> handler) {
|
||||
List<OcrBlockResult> allBlocks = extractBlocks(pdfUrl, scriptType);
|
||||
|
||||
LinkedHashMap<Integer, List<OcrBlockResult>> byPage = new LinkedHashMap<>();
|
||||
|
||||
@@ -52,6 +52,11 @@ public class OcrService {
|
||||
}
|
||||
|
||||
public UUID startOcr(UUID documentId, ScriptType scriptTypeOverride, UUID userId) {
|
||||
return startOcr(documentId, scriptTypeOverride, userId, false);
|
||||
}
|
||||
|
||||
public UUID startOcr(UUID documentId, ScriptType scriptTypeOverride, UUID userId,
|
||||
boolean useExistingAnnotations) {
|
||||
Document doc = documentService.getDocumentById(documentId);
|
||||
|
||||
if (doc.getStatus() == DocumentStatus.PLACEHOLDER) {
|
||||
@@ -85,7 +90,7 @@ public class OcrService {
|
||||
.build();
|
||||
ocrJobDocumentRepository.save(jobDoc);
|
||||
|
||||
ocrAsyncRunner.runSingleDocument(job.getId(), documentId, userId);
|
||||
ocrAsyncRunner.runSingleDocument(job.getId(), documentId, userId, useExistingAnnotations);
|
||||
return job.getId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,13 +175,18 @@ public class RestClientOcrClient implements OcrClient, OcrHealthClient {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void streamBlocks(String pdfUrl, ScriptType scriptType, Consumer<OcrStreamEvent> handler) {
|
||||
public void streamBlocks(String pdfUrl, ScriptType scriptType,
|
||||
List<OcrRegion> regions, Consumer<OcrStreamEvent> handler) {
|
||||
String body;
|
||||
try {
|
||||
body = NDJSON_MAPPER.writeValueAsString(Map.of(
|
||||
"pdfUrl", pdfUrl,
|
||||
"scriptType", scriptType.name(),
|
||||
"language", "de"));
|
||||
var requestMap = new java.util.LinkedHashMap<String, Object>();
|
||||
requestMap.put("pdfUrl", pdfUrl);
|
||||
requestMap.put("scriptType", scriptType.name());
|
||||
requestMap.put("language", "de");
|
||||
if (regions != null && !regions.isEmpty()) {
|
||||
requestMap.put("regions", regions);
|
||||
}
|
||||
body = NDJSON_MAPPER.writeValueAsString(requestMap);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to serialize OCR request", e);
|
||||
}
|
||||
@@ -199,7 +204,7 @@ public class RestClientOcrClient implements OcrClient, OcrHealthClient {
|
||||
|
||||
if (response.statusCode() == 404) {
|
||||
log.info("OCR service does not support /ocr/stream (404), falling back to /ocr");
|
||||
OcrClient.super.streamBlocks(pdfUrl, scriptType, handler);
|
||||
OcrClient.super.streamBlocks(pdfUrl, scriptType, regions, handler);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -259,10 +264,11 @@ public class RestClientOcrClient implements OcrClient, OcrHealthClient {
|
||||
double width,
|
||||
double height,
|
||||
List<List<Double>> polygon,
|
||||
String text
|
||||
String text,
|
||||
String annotationId
|
||||
) {
|
||||
OcrBlockResult toResult() {
|
||||
return new OcrBlockResult(pageNumber, x, y, width, height, polygon, text);
|
||||
return new OcrBlockResult(pageNumber, x, y, width, height, polygon, text, annotationId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +94,27 @@ public class TranscriptionService {
|
||||
return saved;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsert an OCR transcription block for a pre-existing annotation (guided OCR mode).
|
||||
* If the annotation already has a MANUAL block, it is left unchanged.
|
||||
* If it has an OCR block, the text is updated in-place.
|
||||
* If it has no block yet, a new OCR block is created.
|
||||
*/
|
||||
@Transactional
|
||||
public TranscriptionBlock upsertGuidedBlock(UUID documentId, UUID annotationId,
|
||||
String text, UUID userId) {
|
||||
return blockRepository.findByAnnotationId(annotationId).map(existing -> {
|
||||
if (existing.getSource() == BlockSource.MANUAL) {
|
||||
return existing; // never overwrite manual transcription
|
||||
}
|
||||
existing.setText(sanitizeText(text));
|
||||
existing.setUpdatedBy(userId);
|
||||
TranscriptionBlock saved = blockRepository.save(existing);
|
||||
saveVersion(saved, userId);
|
||||
return saved;
|
||||
}).orElseGet(() -> createOcrBlock(documentId, annotationId, text, 0, userId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TranscriptionBlock updateBlock(UUID documentId, UUID blockId,
|
||||
UpdateTranscriptionBlockDTO dto, UUID userId) {
|
||||
|
||||
Reference in New Issue
Block a user