feat: enable document orientation detection for scanned PDFs
- Enable PP-StructureV3's use_doc_orientation_classify feature - Detect rotation angle from doc_preprocessor_res.angle - Swap page dimensions (width <-> height) for 90°/270° rotations - Output PDF now correctly displays landscape-scanned content Also includes: - Archive completed openspec proposals - Add simplify-frontend-ocr-config proposal (pending) - Code cleanup and frontend simplification 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,358 +0,0 @@
|
||||
import { useState } from 'react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { Check, ChevronDown, ChevronUp, FileText, Table, Settings, FileEdit, Layers, Cog } from 'lucide-react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import type { OCRPreset, OCRConfig, TableParsingMode, OCRPresetInfo } from '@/types/apiV2'
|
||||
|
||||
interface OCRPresetSelectorProps {
|
||||
value: OCRPreset
|
||||
onChange: (preset: OCRPreset) => void
|
||||
customConfig?: OCRConfig
|
||||
onCustomConfigChange?: (config: OCRConfig) => void
|
||||
disabled?: boolean
|
||||
className?: string
|
||||
}
|
||||
|
||||
// Preset icons mapping
|
||||
const PRESET_ICONS: Record<OCRPreset, React.ReactNode> = {
|
||||
text_heavy: <FileText className="w-5 h-5" />,
|
||||
datasheet: <Table className="w-5 h-5" />,
|
||||
table_heavy: <Layers className="w-5 h-5" />,
|
||||
form: <FileEdit className="w-5 h-5" />,
|
||||
mixed: <Settings className="w-5 h-5" />,
|
||||
custom: <Cog className="w-5 h-5" />,
|
||||
}
|
||||
|
||||
// Preset configurations (matching backend OCR_PRESET_CONFIGS)
|
||||
const PRESET_CONFIGS: Record<OCRPreset, OCRConfig> = {
|
||||
text_heavy: {
|
||||
table_parsing_mode: 'disabled',
|
||||
enable_wired_table: false,
|
||||
enable_wireless_table: false,
|
||||
enable_chart_recognition: false,
|
||||
enable_formula_recognition: false,
|
||||
},
|
||||
datasheet: {
|
||||
table_parsing_mode: 'conservative',
|
||||
enable_wired_table: true,
|
||||
enable_wireless_table: false,
|
||||
},
|
||||
table_heavy: {
|
||||
table_parsing_mode: 'full',
|
||||
enable_wired_table: true,
|
||||
enable_wireless_table: true,
|
||||
},
|
||||
form: {
|
||||
table_parsing_mode: 'conservative',
|
||||
enable_wired_table: true,
|
||||
enable_wireless_table: false,
|
||||
},
|
||||
mixed: {
|
||||
table_parsing_mode: 'classification_only',
|
||||
enable_wired_table: true,
|
||||
enable_wireless_table: false,
|
||||
},
|
||||
custom: {},
|
||||
}
|
||||
|
||||
// Preset info for display
|
||||
const PRESET_INFO: Record<OCRPreset, { displayName: string; description: string }> = {
|
||||
text_heavy: {
|
||||
displayName: '純文字文件',
|
||||
description: '報告、文章、手冊等以文字為主的文件。禁用表格識別以提高處理速度。',
|
||||
},
|
||||
datasheet: {
|
||||
displayName: '技術規格書',
|
||||
description: '產品規格書、技術數據表 (TDS)。使用保守模式避免過度分割。',
|
||||
},
|
||||
table_heavy: {
|
||||
displayName: '表格密集文件',
|
||||
description: '財務報表、試算表。啟用完整表格識別以捕捉所有表格。',
|
||||
},
|
||||
form: {
|
||||
displayName: '表單',
|
||||
description: '申請表、問卷調查。識別表單欄位但避免過度分割。',
|
||||
},
|
||||
mixed: {
|
||||
displayName: '混合內容',
|
||||
description: '一般文件。只做表格區域分類,不做細胞分割。',
|
||||
},
|
||||
custom: {
|
||||
displayName: '自訂設定',
|
||||
description: '進階使用者可自行調整所有 PP-Structure 參數。',
|
||||
},
|
||||
}
|
||||
|
||||
export default function OCRPresetSelector({
|
||||
value,
|
||||
onChange,
|
||||
customConfig,
|
||||
onCustomConfigChange,
|
||||
disabled = false,
|
||||
className,
|
||||
}: OCRPresetSelectorProps) {
|
||||
const { t } = useTranslation()
|
||||
const [showAdvanced, setShowAdvanced] = useState(false)
|
||||
const presets: OCRPreset[] = ['datasheet', 'text_heavy', 'table_heavy', 'form', 'mixed', 'custom']
|
||||
|
||||
const getPresetInfo = (preset: OCRPreset) => PRESET_INFO[preset]
|
||||
|
||||
// Get effective config (preset config merged with custom overrides)
|
||||
const getEffectiveConfig = (): OCRConfig => {
|
||||
if (value === 'custom') {
|
||||
return customConfig || {}
|
||||
}
|
||||
return { ...PRESET_CONFIGS[value], ...customConfig }
|
||||
}
|
||||
|
||||
const handleCustomConfigChange = (key: keyof OCRConfig, val: any) => {
|
||||
if (onCustomConfigChange) {
|
||||
onCustomConfigChange({
|
||||
...customConfig,
|
||||
[key]: val,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn('border rounded-lg p-4 bg-white', className)}>
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Settings className="w-5 h-5 text-gray-600" />
|
||||
<h3 className="text-lg font-semibold text-gray-900">OCR 處理預設</h3>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowAdvanced(!showAdvanced)}
|
||||
className="text-sm text-blue-600 hover:text-blue-800 flex items-center gap-1"
|
||||
disabled={disabled}
|
||||
>
|
||||
{showAdvanced ? (
|
||||
<>
|
||||
<ChevronUp className="w-4 h-4" />
|
||||
隱藏進階設定
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<ChevronDown className="w-4 h-4" />
|
||||
顯示進階設定
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Preset Grid */}
|
||||
<div className="grid grid-cols-2 md:grid-cols-3 gap-3">
|
||||
{presets.map((preset) => {
|
||||
const info = getPresetInfo(preset)
|
||||
const isSelected = value === preset
|
||||
|
||||
return (
|
||||
<button
|
||||
key={preset}
|
||||
type="button"
|
||||
disabled={disabled}
|
||||
onClick={() => onChange(preset)}
|
||||
className={cn(
|
||||
'flex flex-col items-center gap-2 p-4 rounded-lg border-2 transition-all text-center',
|
||||
isSelected
|
||||
? 'border-blue-500 bg-blue-50'
|
||||
: 'border-gray-200 hover:border-gray-300 hover:bg-gray-50',
|
||||
disabled && 'opacity-50 cursor-not-allowed'
|
||||
)}
|
||||
>
|
||||
{/* Icon */}
|
||||
<div
|
||||
className={cn(
|
||||
'p-3 rounded-lg',
|
||||
isSelected ? 'bg-blue-100 text-blue-600' : 'bg-gray-100 text-gray-500'
|
||||
)}
|
||||
>
|
||||
{PRESET_ICONS[preset]}
|
||||
</div>
|
||||
|
||||
{/* Label */}
|
||||
<div className="flex items-center gap-1">
|
||||
<span
|
||||
className={cn(
|
||||
'font-medium text-sm',
|
||||
isSelected ? 'text-blue-700' : 'text-gray-900'
|
||||
)}
|
||||
>
|
||||
{info.displayName}
|
||||
</span>
|
||||
{isSelected && <Check className="w-4 h-4 text-blue-600" />}
|
||||
</div>
|
||||
|
||||
{/* Recommended badge */}
|
||||
{preset === 'datasheet' && (
|
||||
<span className="text-xs bg-green-100 text-green-700 px-2 py-0.5 rounded-full">
|
||||
推薦
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* Selected Preset Description */}
|
||||
<div className="mt-4 p-3 bg-gray-50 border border-gray-200 rounded-md">
|
||||
<p className="text-sm text-gray-700">
|
||||
<span className="font-medium">{PRESET_INFO[value].displayName}:</span>
|
||||
{PRESET_INFO[value].description}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Advanced Settings Panel */}
|
||||
{showAdvanced && (
|
||||
<div className="mt-4 p-4 bg-gray-50 border border-gray-200 rounded-lg space-y-4">
|
||||
<h4 className="font-medium text-gray-900 flex items-center gap-2">
|
||||
<Cog className="w-4 h-4" />
|
||||
進階參數設定
|
||||
</h4>
|
||||
|
||||
{/* Table Parsing Mode */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
表格解析模式
|
||||
</label>
|
||||
<select
|
||||
value={getEffectiveConfig().table_parsing_mode || 'conservative'}
|
||||
onChange={(e) => handleCustomConfigChange('table_parsing_mode', e.target.value as TableParsingMode)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className={cn(
|
||||
'w-full px-3 py-2 border border-gray-300 rounded-md text-sm',
|
||||
(disabled || value !== 'custom') && 'bg-gray-100 cursor-not-allowed'
|
||||
)}
|
||||
>
|
||||
<option value="full">完整模式 (積極)</option>
|
||||
<option value="conservative">保守模式 (推薦)</option>
|
||||
<option value="classification_only">僅分類</option>
|
||||
<option value="disabled">禁用</option>
|
||||
</select>
|
||||
<p className="mt-1 text-xs text-gray-500">
|
||||
{value !== 'custom' && '選擇「自訂設定」預設以調整此參數'}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Table Detection Options */}
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={getEffectiveConfig().enable_wired_table ?? true}
|
||||
onChange={(e) => handleCustomConfigChange('enable_wired_table', e.target.checked)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className="rounded border-gray-300"
|
||||
/>
|
||||
<span className="text-sm text-gray-700">有框線表格</span>
|
||||
</label>
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={getEffectiveConfig().enable_wireless_table ?? false}
|
||||
onChange={(e) => handleCustomConfigChange('enable_wireless_table', e.target.checked)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className="rounded border-gray-300"
|
||||
/>
|
||||
<span className="text-sm text-gray-700">無框線表格</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{/* Recognition Modules */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
識別模組
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={getEffectiveConfig().enable_chart_recognition ?? true}
|
||||
onChange={(e) => handleCustomConfigChange('enable_chart_recognition', e.target.checked)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className="rounded border-gray-300"
|
||||
/>
|
||||
<span className="text-sm text-gray-700">圖表識別</span>
|
||||
</label>
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={getEffectiveConfig().enable_formula_recognition ?? true}
|
||||
onChange={(e) => handleCustomConfigChange('enable_formula_recognition', e.target.checked)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className="rounded border-gray-300"
|
||||
/>
|
||||
<span className="text-sm text-gray-700">公式識別</span>
|
||||
</label>
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={getEffectiveConfig().enable_seal_recognition ?? false}
|
||||
onChange={(e) => handleCustomConfigChange('enable_seal_recognition', e.target.checked)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className="rounded border-gray-300"
|
||||
/>
|
||||
<span className="text-sm text-gray-700">印章識別</span>
|
||||
</label>
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={getEffectiveConfig().enable_region_detection ?? true}
|
||||
onChange={(e) => handleCustomConfigChange('enable_region_detection', e.target.checked)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className="rounded border-gray-300"
|
||||
/>
|
||||
<span className="text-sm text-gray-700">區域偵測</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Preprocessing Options */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
預處理選項
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={getEffectiveConfig().use_doc_orientation_classify ?? true}
|
||||
onChange={(e) => handleCustomConfigChange('use_doc_orientation_classify', e.target.checked)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className="rounded border-gray-300"
|
||||
/>
|
||||
<span className="text-sm text-gray-700">自動旋轉校正</span>
|
||||
</label>
|
||||
<label className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={getEffectiveConfig().use_doc_unwarping ?? false}
|
||||
onChange={(e) => handleCustomConfigChange('use_doc_unwarping', e.target.checked)}
|
||||
disabled={disabled || value !== 'custom'}
|
||||
className="rounded border-gray-300"
|
||||
/>
|
||||
<span className="text-sm text-gray-700">文件反翹曲</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{value !== 'custom' && (
|
||||
<p className="text-xs text-amber-600 bg-amber-50 p-2 rounded">
|
||||
提示:選擇「自訂設定」預設以啟用手動調整參數
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Info Note */}
|
||||
<div className="mt-4 p-3 bg-blue-50 border border-blue-200 rounded-md">
|
||||
<p className="text-sm text-blue-800">
|
||||
預設配置會根據文件類型優化 PP-Structure 的表格識別和版面分析參數。
|
||||
選擇錯誤的預設可能導致表格過度分割或識別失敗。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import { useState, useCallback, useMemo, useRef, useEffect } from 'react'
|
||||
import { Document, Page, pdfjs } from 'react-pdf'
|
||||
import type { PDFDocumentProxy } from 'pdfjs-dist'
|
||||
|
||||
// Type alias for PDFDocumentProxy to avoid direct pdfjs-dist import issues
|
||||
type PDFDocumentProxy = ReturnType<typeof pdfjs.getDocument> extends Promise<infer T> ? T : never
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { ChevronLeft, ChevronRight, ZoomIn, ZoomOut, Loader2 } from 'lucide-react'
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
import { cn } from '@/lib/utils'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Table, Grid3X3, Rows3 } from 'lucide-react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import type { TableDetectionConfig } from '@/types/apiV2'
|
||||
|
||||
interface TableDetectionSelectorProps {
|
||||
value: TableDetectionConfig
|
||||
onChange: (config: TableDetectionConfig) => void
|
||||
disabled?: boolean
|
||||
className?: string
|
||||
}
|
||||
|
||||
interface DetectionOption {
|
||||
key: keyof TableDetectionConfig
|
||||
icon: React.ReactNode
|
||||
labelKey: string
|
||||
descKey: string
|
||||
}
|
||||
|
||||
const DETECTION_OPTIONS: DetectionOption[] = [
|
||||
{
|
||||
key: 'enable_wired_table',
|
||||
icon: <Grid3X3 className="w-5 h-5" />,
|
||||
labelKey: 'processing.tableDetection.wired',
|
||||
descKey: 'processing.tableDetection.wiredDesc',
|
||||
},
|
||||
{
|
||||
key: 'enable_wireless_table',
|
||||
icon: <Rows3 className="w-5 h-5" />,
|
||||
labelKey: 'processing.tableDetection.wireless',
|
||||
descKey: 'processing.tableDetection.wirelessDesc',
|
||||
},
|
||||
{
|
||||
key: 'enable_region_detection',
|
||||
icon: <Table className="w-5 h-5" />,
|
||||
labelKey: 'processing.tableDetection.region',
|
||||
descKey: 'processing.tableDetection.regionDesc',
|
||||
},
|
||||
]
|
||||
|
||||
export default function TableDetectionSelector({
|
||||
value,
|
||||
onChange,
|
||||
disabled = false,
|
||||
className,
|
||||
}: TableDetectionSelectorProps) {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const handleOptionChange = (key: keyof TableDetectionConfig, checked: boolean) => {
|
||||
onChange({
|
||||
...value,
|
||||
[key]: checked,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn('border rounded-lg p-4 bg-white', className)}>
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-2 mb-4">
|
||||
<Table className="w-5 h-5 text-gray-600" />
|
||||
<h3 className="text-lg font-semibold text-gray-900">{t('processing.tableDetection.title')}</h3>
|
||||
</div>
|
||||
|
||||
{/* Detection Options */}
|
||||
<div className="space-y-3">
|
||||
{DETECTION_OPTIONS.map((option) => {
|
||||
const isChecked = value[option.key]
|
||||
|
||||
return (
|
||||
<label
|
||||
key={option.key}
|
||||
className={cn(
|
||||
'flex items-start gap-4 p-4 rounded-lg border-2 transition-all cursor-pointer',
|
||||
isChecked
|
||||
? 'border-blue-500 bg-blue-50'
|
||||
: 'border-gray-200 hover:border-gray-300 hover:bg-gray-50',
|
||||
disabled && 'opacity-50 cursor-not-allowed'
|
||||
)}
|
||||
>
|
||||
{/* Checkbox */}
|
||||
<Checkbox
|
||||
checked={isChecked}
|
||||
onCheckedChange={(checked) => handleOptionChange(option.key, checked)}
|
||||
disabled={disabled}
|
||||
className="mt-0.5"
|
||||
/>
|
||||
|
||||
{/* Icon */}
|
||||
<div
|
||||
className={cn(
|
||||
'p-2 rounded-lg flex-shrink-0',
|
||||
isChecked ? 'bg-blue-100 text-blue-600' : 'bg-gray-100 text-gray-500'
|
||||
)}
|
||||
>
|
||||
{option.icon}
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="flex-1 min-w-0">
|
||||
<span
|
||||
className={cn(
|
||||
'font-medium',
|
||||
isChecked ? 'text-blue-700' : 'text-gray-900'
|
||||
)}
|
||||
>
|
||||
{t(option.labelKey)}
|
||||
</span>
|
||||
<p className="text-sm text-gray-500 mt-1">{t(option.descKey)}</p>
|
||||
</div>
|
||||
</label>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* Info Note */}
|
||||
<div className="mt-4 p-3 bg-amber-50 border border-amber-200 rounded-md">
|
||||
<p className="text-sm text-amber-800">
|
||||
{t('processing.tableDetection.note')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user