File size: 1,701 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 | from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from src.prototype import *
import os
app = FastAPI()
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def read_root():
return {"message": "Welcome to the OMR API!"}
@app.post("/process-sheet-music")
async def process_sheet_music(image: UploadFile = File(...)):
"""
Endpoint to process a sheet music image and return the MusicXML path.
"""
# Save the uploaded file temporarily
temp_image_path = os.path.join(UPLOAD_FOLDER, "temp_upload.png")
with open(temp_image_path, "wb") as buffer:
content = await image.read()
buffer.write(content)
# Preprocess the image if necessary
preprocessed_path = preprocess_image(temp_image_path)
# Process sheet music image to generate MusicXML
process_sheet_music_image(preprocessed_path)
output_xml_path = "output.musicxml"
output_midi_path = "output.mid"
# Check if the MusicXML file was created successfully
if os.path.exists(output_xml_path):
convert_musicxml_to_midi(output_xml_path, output_midi_path)
play_midi(output_midi_path)
return {"message": "MusicXML generated successfully", "musicxml_path": output_xml_path}
else:
return {"error": "MusicXML generation failed"}
if __name__ == "__main__":
import uvicorn
# Run the FastAPI app
uvicorn.run(app, host="0.0.0.0", port=8000)
print("Starting FastAPI server on http://127.0.0.1:8000/") |