|
| 1 | +import os |
| 2 | +os.environ["MISTRAL_API_KEY"] = "ruQqe2KV9UTYebSxZVrGDf9tIzcEGpbS" |
| 3 | + |
| 4 | +import sys |
| 5 | +sys.path.insert(0, "/app") |
| 6 | +import cv2 |
| 7 | +import numpy as np |
| 8 | +from chart2csv.core.detection import detect_axes, detect_ticks |
| 9 | +from chart2csv.core.ocr import extract_tick_labels |
| 10 | +from chart2csv.core.transform import build_transform, apply_transform |
| 11 | + |
| 12 | +# Create a simple test chart |
| 13 | +h, w = 400, 600 |
| 14 | +img = np.ones((h, w), dtype=np.uint8) * 255 |
| 15 | + |
| 16 | +# Draw axes |
| 17 | +cv2.line(img, (50, 350), (550, 350), 0, 2) |
| 18 | +cv2.line(img, (50, 50), (50, 350), 0, 2) |
| 19 | + |
| 20 | +# Draw X labels: 0,1,2,3,4,5 |
| 21 | +for i, val in enumerate([0, 1, 2, 3, 4, 5]): |
| 22 | + x = 50 + i * 100 |
| 23 | + cv2.line(img, (x, 345), (x, 355), 0, 2) |
| 24 | + cv2.putText(img, str(val), (x-5, 375), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 0, 1) |
| 25 | + |
| 26 | +# Draw Y labels: 0,10,20,30,40,50 |
| 27 | +for i, val in enumerate([0, 10, 20, 30, 40, 50]): |
| 28 | + y = 350 - i * 60 |
| 29 | + cv2.line(img, (45, y), (55, y), 0, 2) |
| 30 | + cv2.putText(img, str(val), (10, y+5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 0, 1) |
| 31 | + |
| 32 | +axes, conf = detect_axes(img) |
| 33 | +x_ax = axes["x"] |
| 34 | +y_ax = axes["y"] |
| 35 | +print(f"Axes: X at y={x_ax}, Y at x={y_ax}") |
| 36 | + |
| 37 | +ticks, conf = detect_ticks(img, axes) |
| 38 | +print(f"X ticks: {sorted(ticks['x'])}") |
| 39 | +print(f"Y ticks: {sorted(ticks['y'])}") |
| 40 | + |
| 41 | +ticks_data, ocr_conf = extract_tick_labels(img, axes, use_mistral=True, use_cache=False) |
| 42 | +x_ocr = [(t["pixel"], t["value"]) for t in ticks_data["x"]] |
| 43 | +y_ocr = [(t["pixel"], t["value"]) for t in ticks_data["y"]] |
| 44 | +print(f"OCR X: {x_ocr}") |
| 45 | +print(f"OCR Y: {y_ocr}") |
| 46 | + |
| 47 | +if ticks_data["x"] and ticks_data["y"]: |
| 48 | + transform, fit_error = build_transform(ticks=ticks_data) |
| 49 | + xa = transform["x"]["a"] |
| 50 | + xb = transform["x"]["b"] |
| 51 | + ya = transform["y"]["a"] |
| 52 | + yb = transform["y"]["b"] |
| 53 | + print(f"Transform X: a={xa:.6f}, b={xb:.2f}") |
| 54 | + print(f"Transform Y: a={ya:.6f}, b={yb:.2f}") |
| 55 | + print(f"Fit error: {fit_error:.4f}") |
| 56 | + |
| 57 | + # Test pixel (250, 170) should be approximately (2, 30) |
| 58 | + test_px = np.array([[250, 170]]) |
| 59 | + result = apply_transform(test_px, transform) |
| 60 | + rx = result[0,0] |
| 61 | + ry = result[0,1] |
| 62 | + print(f"Test: pixel (250,170) -> ({rx:.1f}, {ry:.1f})") |
| 63 | + print(f"Expected: roughly (2, 30)") |
0 commit comments