import cv2
import numpy as np
from youtube_tab_to_pdf import _find_white_tab_strip, _detect_measure_bars, _extract_print_channel

def get_clean_binary(img):
    gray = np.max(img, axis=2)
    _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
    return binary

cap = cv2.VideoCapture(r"output/サカナクション／新宝島(エレキギターTAB) 難易度★★★ sakanaction shintakarajima.mp4")
cap.set(cv2.CAP_PROP_POS_FRAMES, 50)
ret, f1 = cap.read()
cap.set(cv2.CAP_PROP_POS_FRAMES, 65) # Next second
ret, f2 = cap.read()
cap.release()

def process(frame):
    s = _find_white_tab_strip(frame)
    crop = frame[s[0]:s[1], :]
    gray = _extract_print_channel(crop)
    bars = _detect_measure_bars(gray)
    coords = [0] + bars + [crop.shape[1]]
    m = crop[:, coords[1]:coords[2]] # Get M2 just in case M1 is a clef
    return m

m1 = process(f1)
m2 = process(f2)

cv2.imwrite("test_m1.png", m1)
cv2.imwrite("test_m2.png", m2)

bin1 = get_clean_binary(m1)
bin2 = get_clean_binary(m2)

h = min(bin1.shape[0], bin2.shape[0])
w = min(bin1.shape[1], bin2.shape[1])
s1 = bin1[:h, :w]
s2 = bin2[:h, :w]

diff = cv2.absdiff(s1, s2)
error_ratio = np.sum(diff > 0) / s1.size

print(f"Error Ratio: {error_ratio:.4f}")
