import cv2
import numpy as np
import os
from glob import glob

video_path = glob('output/*.mp4')[0]
cap = cv2.VideoCapture(video_path)

def _find_white_tab_strip(frame):
    h, w = frame.shape[:2]
    gray = np.max(frame, axis=2)
    _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
    row_white_counts = np.sum(binary > 0, axis=1)
    
    threshold = w * 0.1
    white_rows = np.where(row_white_counts > threshold)[0]
    if len(white_rows) < 5: return None
    return white_rows[0], white_rows[-1]

def get_number_sprite(m_img):
    gray = np.max(m_img, axis=2)
    _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
    row_sums = np.sum(thresh, axis=1) / 255
    staff_lines = np.where(row_sums > m_img.shape[1] * 0.5)[0]
    y_staff = staff_lines[0] if len(staff_lines) > 0 else 50
    
    crop_y1 = max(0, y_staff - 35)
    crop_y2 = max(0, y_staff - 2)
    crop_x1 = 0
    crop_x2 = min(60, m_img.shape[1])
    
    if crop_y2 <= crop_y1 or crop_x2 <= crop_x1: return None
    return thresh[crop_y1:crop_y2, crop_x1:crop_x2]

frame_count = 0
found = 0
while True:
    ret, frame = cap.read()
    if not ret: break
    if frame_count % 30 == 0:
        strip = _find_white_tab_strip(frame)
        if strip:
            tab_crop = frame[max(0, strip[0]):min(frame.shape[0], strip[1]), :]
            
            # _detect_measure_bars inline
            b_gray = np.max(tab_crop, axis=2)
            _, b_bin = cv2.threshold(b_gray, 180, 255, cv2.THRESH_BINARY)
            col_sums = np.sum(b_bin, axis=0) / 255
            bars = np.where(col_sums > tab_crop.shape[0] * 0.8)[0]
            
            if len(bars) > 1:
                x_start = bars[0]
                x_end = bars[1]
                if x_end - x_start > 40:
                    first_m = tab_crop[:, x_start:x_end]
                    sprite = get_number_sprite(first_m)
                    if sprite is not None:
                        pixels = np.count_nonzero(sprite > 127)
                        cv2.imwrite(f"C:/Users/Certes/Desktop/guitar_score/debug_s_{frame_count}_{pixels}.png", sprite)
                        print(f"Dumped sprite frame {frame_count} with {pixels} pixels")
                        found += 1
                        if found > 15: break
    frame_count += 1
cap.release()
