import cv2
import numpy as np
from youtube_tab_to_pdf import extract_unique_scroll

# We will read fast_test_pano.jpg
img = cv2.imread('fast_test_pano.jpg', cv2.IMREAD_GRAYSCALE)

# We want to find staff lines and number band
_, bin_inv = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY_INV)
row_sums = np.sum(bin_inv, axis=1) / 255.0
staff_rows = np.where(row_sums > img.shape[1] * 0.4)[0]

if len(staff_rows) >= 6:
    staff_y_top = staff_rows[0]
else:
    staff_y_top = int(img.shape[0] * 0.3)

# Number band
band_y_top = max(0, staff_y_top - 25)
band_y_bottom = staff_y_top

band = img[band_y_top:band_y_bottom, :]

# Save it to see if it correctly contains the numbers
cv2.imwrite('debug_band.png', band)
print(f"Band shape: {band.shape}")

# Let's see if we can extract number boxes!
band_inv = cv2.bitwise_not(band)
col_sums = np.sum(band_inv, axis=0) / 255.0
number_xs = np.where(col_sums > 5)[0] # at least 5 pixels of ink vertically

print(f"Pixels with numbers: {len(number_xs)}")
