File size: 2,006 Bytes
2e31e1e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import cv2
import uuid
import os
import subprocess
from music21 import converter
import pygame
# Define paths
captured_image = f"captured_{uuid.uuid4().hex[:6]}.png"
musicxml_output = "output.musicxml"
midi_output = "output.mid"
# Capture image from webcam
def capture_sheet_music_image():
cap = cv2.VideoCapture(0)
print("📷 Press SPACE to capture sheet music, or ESC to exit.")
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow("Sheet Music Capture", frame)
key = cv2.waitKey(1)
if key % 256 == 27: # ESC
print("❌ Capture canceled.")
break
elif key % 256 == 32: # SPACE
cv2.imwrite(captured_image, frame)
print(f"✅ Image saved as {captured_image}")
break
cap.release()
cv2.destroyAllWindows()
# Process with OMR (oemer)
def run_omr(image_path):
command = f'oemer "{image_path}" -o {musicxml_output}'
try:
subprocess.run(command, shell=True, check=True)
print(f"✅ MusicXML generated: {musicxml_output}")
except subprocess.CalledProcessError as e:
print(f"⚠️ OMR failed: {e}")
# Convert to MIDI and play
def convert_and_play_music(musicxml_file, midi_file):
try:
score = converter.parse(musicxml_file)
score.write('midi', fp=midi_file)
print(f"🎼 MIDI generated: {midi_file}")
pygame.mixer.init()
pygame.mixer.music.load(midi_file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
except Exception as e:
print(f"⚠️ Error in MIDI playback: {e}")
# Main execution
if __name__ == "__main__":
capture_sheet_music_image()
if os.path.exists(captured_image):
run_omr(captured_image)
if os.path.exists(musicxml_output):
convert_and_play_music(musicxml_output, midi_output)
|