import sys
sys.path.append(r"C:\Users\Certes\Desktop\guitar_score")
import cv2
import easyocr
import numpy as np
import os
from youtube_tab_to_pdf import extract_frames, extract_unique_scroll

video_file = r"C:\Users\Certes\Desktop\guitar_score\output\サカナクション／新宝島(エレキギターTAB) 難易度★★★ sakanaction shintakarajima.mp4"
print("Extracting frames...")
frames = extract_frames(video_file, fps=2)

print("Running pipeline extraction...")
unique = extract_unique_scroll(frames, threshold=0.95)

print("Initializing OCR...")
reader = easyocr.Reader(['en'])

print(f"Generated {len(unique)} chunks.")
detect_log = []

for i, page in enumerate(unique):
    # Image is A4 width
    # We want to OCR the top 150 pixels of the whole chunk to find measure numbers
    h, w = page.shape[:2]
    top_area = page[:min(200, h), :]
    
    results = reader.readtext(top_area)
    # filter for numbers
    nums = []
    for (bbox, text, prob) in results:
        t = ''.join(filter(str.isdigit, text))
        if t:
            nums.append(int(t))
            
    print(f"Page {i} measure numbers detected: {nums}")
    detect_log.append(nums)
    
    cv2.imwrite(f"output/verify_chunk_{i}.jpg", page)
    if i > 5:
        break
