Spaces:
Paused
Paused
Improve OCR crop quality and final price cleanup
Browse files
CV-Lenta-product-main/src/lenta_price_tags/cv/enhancement.py
CHANGED
|
@@ -15,6 +15,7 @@ class ImageEnhancer:
|
|
| 15 |
variants = tuple(dict.fromkeys(self.config.variants))
|
| 16 |
try:
|
| 17 |
import cv2
|
|
|
|
| 18 |
except ImportError:
|
| 19 |
return [
|
| 20 |
EnhancedCrop(obs.group_id, obs.crop_id, "original", obs.crop_path, obs.crop_path, obs.quality_score)
|
|
@@ -36,7 +37,7 @@ class ImageEnhancer:
|
|
| 36 |
render_variants = [variant for variant in variants if variant != "original"]
|
| 37 |
if not render_variants:
|
| 38 |
continue
|
| 39 |
-
image =
|
| 40 |
if image is None:
|
| 41 |
continue
|
| 42 |
for variant in render_variants:
|
|
@@ -60,15 +61,24 @@ def _variant_image(image, variant: str, upscale_factor: int, cv2):
|
|
| 60 |
if variant == "upscaled":
|
| 61 |
return cv2.resize(image, None, fx=upscale_factor, fy=upscale_factor, interpolation=cv2.INTER_LANCZOS4)
|
| 62 |
if variant == "contrast":
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return cv2.
|
| 69 |
return image.copy()
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def _write_image(path: Path, image, cv2) -> Path:
|
| 73 |
path.parent.mkdir(parents=True, exist_ok=True)
|
| 74 |
ok, encoded = cv2.imencode(path.suffix or ".jpg", image)
|
|
@@ -76,3 +86,10 @@ def _write_image(path: Path, image, cv2) -> Path:
|
|
| 76 |
raise ValueError(f"Cannot encode image: {path}")
|
| 77 |
encoded.tofile(str(path))
|
| 78 |
return path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
variants = tuple(dict.fromkeys(self.config.variants))
|
| 16 |
try:
|
| 17 |
import cv2
|
| 18 |
+
import numpy as np
|
| 19 |
except ImportError:
|
| 20 |
return [
|
| 21 |
EnhancedCrop(obs.group_id, obs.crop_id, "original", obs.crop_path, obs.crop_path, obs.quality_score)
|
|
|
|
| 37 |
render_variants = [variant for variant in variants if variant != "original"]
|
| 38 |
if not render_variants:
|
| 39 |
continue
|
| 40 |
+
image = _read_image(observation.crop_path, cv2, np)
|
| 41 |
if image is None:
|
| 42 |
continue
|
| 43 |
for variant in render_variants:
|
|
|
|
| 61 |
if variant == "upscaled":
|
| 62 |
return cv2.resize(image, None, fx=upscale_factor, fy=upscale_factor, interpolation=cv2.INTER_LANCZOS4)
|
| 63 |
if variant == "contrast":
|
| 64 |
+
return _clahe_luminance(image, cv2)
|
| 65 |
+
if variant == "ocr_ready":
|
| 66 |
+
scaled = cv2.resize(image, None, fx=upscale_factor, fy=upscale_factor, interpolation=cv2.INTER_CUBIC)
|
| 67 |
+
contrasted = _clahe_luminance(scaled, cv2)
|
| 68 |
+
blurred = cv2.GaussianBlur(contrasted, (0, 0), 1.0)
|
| 69 |
+
return cv2.addWeighted(contrasted, 1.5, blurred, -0.5, 0)
|
| 70 |
return image.copy()
|
| 71 |
|
| 72 |
|
| 73 |
+
def _clahe_luminance(image, cv2):
|
| 74 |
+
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
|
| 75 |
+
l_channel, a_channel, b_channel = cv2.split(lab)
|
| 76 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
| 77 |
+
enhanced_l = clahe.apply(l_channel)
|
| 78 |
+
enhanced = cv2.merge((enhanced_l, a_channel, b_channel))
|
| 79 |
+
return cv2.cvtColor(enhanced, cv2.COLOR_LAB2BGR)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
def _write_image(path: Path, image, cv2) -> Path:
|
| 83 |
path.parent.mkdir(parents=True, exist_ok=True)
|
| 84 |
ok, encoded = cv2.imencode(path.suffix or ".jpg", image)
|
|
|
|
| 86 |
raise ValueError(f"Cannot encode image: {path}")
|
| 87 |
encoded.tofile(str(path))
|
| 88 |
return path
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def _read_image(path: str | Path, cv2, np):
|
| 92 |
+
data = np.fromfile(str(path), dtype=np.uint8)
|
| 93 |
+
if data.size == 0:
|
| 94 |
+
return None
|
| 95 |
+
return cv2.imdecode(data, cv2.IMREAD_COLOR)
|
CV-Lenta-product-main/src/lenta_price_tags/ui/gradio_app.py
CHANGED
|
@@ -42,13 +42,13 @@ def build_demo():
|
|
| 42 |
"fallback_crop_padding": 8,
|
| 43 |
},
|
| 44 |
"crop_selection": {
|
| 45 |
-
"max_crops_per_group":
|
| 46 |
"min_crop_width": 50,
|
| 47 |
"min_crop_height": 25,
|
| 48 |
"deduplicate_phash": False,
|
| 49 |
"prefer_detector_quality": True,
|
| 50 |
},
|
| 51 |
-
"image_enhancement": {"variants": ["original"]},
|
| 52 |
"ocr": {
|
| 53 |
"provider": ocr_provider,
|
| 54 |
"device": "cpu",
|
|
|
|
| 42 |
"fallback_crop_padding": 8,
|
| 43 |
},
|
| 44 |
"crop_selection": {
|
| 45 |
+
"max_crops_per_group": 2,
|
| 46 |
"min_crop_width": 50,
|
| 47 |
"min_crop_height": 25,
|
| 48 |
"deduplicate_phash": False,
|
| 49 |
"prefer_detector_quality": True,
|
| 50 |
},
|
| 51 |
+
"image_enhancement": {"variants": ["original", "ocr_ready"], "upscale_factor": 2},
|
| 52 |
"ocr": {
|
| 53 |
"provider": ocr_provider,
|
| 54 |
"device": "cpu",
|
CV-Lenta-product-main/tests/test_fast_preprocessing.py
CHANGED
|
@@ -2,6 +2,9 @@ from __future__ import annotations
|
|
| 2 |
|
| 3 |
from pathlib import Path
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
from lenta_price_tags.config import CropSelectionConfig, ImageEnhancementConfig
|
| 6 |
from lenta_price_tags.cv.enhancement import ImageEnhancer
|
| 7 |
from lenta_price_tags.cv.quality import CropQualitySelector
|
|
@@ -34,6 +37,28 @@ def test_original_enhancement_reuses_source_crop(tmp_path: Path) -> None:
|
|
| 34 |
assert not (tmp_path / "enhanced").exists()
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def _observation(crop_id: str, crop_path: str, *, quality: float) -> CropObservation:
|
| 38 |
return CropObservation(
|
| 39 |
crop_id=crop_id,
|
|
|
|
| 2 |
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
from lenta_price_tags.config import CropSelectionConfig, ImageEnhancementConfig
|
| 9 |
from lenta_price_tags.cv.enhancement import ImageEnhancer
|
| 10 |
from lenta_price_tags.cv.quality import CropQualitySelector
|
|
|
|
| 37 |
assert not (tmp_path / "enhanced").exists()
|
| 38 |
|
| 39 |
|
| 40 |
+
def test_ocr_ready_enhancement_writes_upscaled_variant(tmp_path: Path) -> None:
|
| 41 |
+
crop_path = tmp_path / "crop.jpg"
|
| 42 |
+
image = np.full((30, 50, 3), 160, np.uint8)
|
| 43 |
+
cv2.putText(image, "699", (2, 22), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (30, 30, 30), 2, cv2.LINE_AA)
|
| 44 |
+
ok, encoded = cv2.imencode(".jpg", image)
|
| 45 |
+
assert ok
|
| 46 |
+
encoded.tofile(str(crop_path))
|
| 47 |
+
observation = _observation("crop", crop_path.as_posix(), quality=0.8)
|
| 48 |
+
|
| 49 |
+
variants = ImageEnhancer(
|
| 50 |
+
ImageEnhancementConfig(variants=("ocr_ready",), upscale_factor=2),
|
| 51 |
+
output_root=tmp_path,
|
| 52 |
+
).make_variants([observation])
|
| 53 |
+
|
| 54 |
+
assert len(variants) == 1
|
| 55 |
+
assert variants[0].image_variant == "ocr_ready"
|
| 56 |
+
enhanced = cv2.imdecode(np.fromfile(variants[0].image_path, dtype=np.uint8), cv2.IMREAD_COLOR)
|
| 57 |
+
assert enhanced is not None
|
| 58 |
+
assert enhanced.shape[0] == 60
|
| 59 |
+
assert enhanced.shape[1] == 100
|
| 60 |
+
|
| 61 |
+
|
| 62 |
def _observation(crop_id: str, crop_path: str, *, quality: float) -> CropObservation:
|
| 63 |
return CropObservation(
|
| 64 |
crop_id=crop_id,
|