@@ -292,15 +292,20 @@ def inference_patch(self, model, period, composer, instrumentation,
292
292
293
293
for image_path in png_paths :
294
294
i = Image .open (image_path )
295
- i = ImageOps .exif_transpose (i )
295
+ # 创建一个白色背景的图像
296
+ image = Image .new ("RGB" , i .size , (255 , 255 , 255 ))
297
+
298
+ # 将透明背景的图片粘贴到白色背景上
299
+ image .paste (i , mask = i .split ()[3 ]) # 使用 Alpha 通道作为掩码
300
+ # i = ImageOps.exif_transpose(i) # 翻转图片
296
301
297
302
# 调整宽度为1024,保持宽高比
298
- width , height = i .size
299
- new_width = 1024
300
- new_height = int (height * (new_width / width ))
301
- i = i .resize ((new_width , new_height ), Image .Resampling .LANCZOS )
303
+ # width, height = i.size
304
+ # new_width = 1024
305
+ # new_height = int(height * (new_width / width))
306
+ # i = i.resize((new_width, new_height), Image.Resampling.LANCZOS)
302
307
303
- image = i .convert ("RGB" )
308
+ image = image .convert ("RGB" )
304
309
image = np .array (image ).astype (np .float32 ) / 255.0
305
310
image = torch .from_numpy (image )[None ,]
306
311
images .append (image )
@@ -467,45 +472,144 @@ def wait_for_png_sequence(self, base_path, timeout=15, check_interval=0.3):
467
472
468
473
def xml2mp3 (self , xml_path , musescore4_path ):
469
474
import subprocess
475
+ import sys
476
+ import tempfile
477
+
470
478
mp3_path = xml_path .rsplit ("." , 1 )[0 ] + ".mp3"
471
- try :
472
- subprocess .run (
473
- [musescore4_path , '-o' , mp3_path , xml_path ],
474
- check = True ,
475
- capture_output = True ,
476
- )
477
- # 等待MP3文件生成完成
478
- if self .wait_for_file (mp3_path ):
479
- print (f"Conversion to { mp3_path } completed" )
480
- return mp3_path
481
- else :
482
- print ("MP3 conversion timeout" )
479
+ # 检测操作系统是否为 Linux
480
+ if sys .platform == "linux" :
481
+ try :
482
+ # 使用不同的显示端口
483
+ display_number = 100
484
+ os .environ ["DISPLAY" ] = f":{ display_number } "
485
+
486
+ # 检查并清理旧的 Xvfb 锁文件
487
+ tmp_dir = tempfile .mkdtemp ()
488
+ xvfb_lock_file = os .path .join (tmp_dir , f".X{ display_number } -lock" )
489
+ if os .path .exists (xvfb_lock_file ):
490
+ print (f"清理旧的 Xvfb 锁文件: { xvfb_lock_file } " )
491
+ os .remove (xvfb_lock_file )
492
+
493
+ # 杀死所有残留的 Xvfb 进程
494
+ subprocess .run (["pkill" , "Xvfb" ], stderr = subprocess .DEVNULL ) # 忽略错误
495
+ time .sleep (1 ) # 等待进程终止
496
+
497
+ # 启动 Xvfb
498
+ xvfb_process = subprocess .Popen (["Xvfb" , f":{ display_number } " , "-screen" , "0" , "1024x768x24" ])
499
+ time .sleep (2 ) # 等待 Xvfb 启动
500
+
501
+ # 设置 Qt 插件环境变量
502
+ os .environ ["QT_QPA_PLATFORM" ] = "offscreen"
503
+
504
+ # 运行 mscore 命令
505
+ subprocess .run (
506
+ [musescore4_path , '-o' , mp3_path , xml_path ],
507
+ check = True ,
508
+ capture_output = True ,
509
+ )
510
+
511
+ # 等待MP3文件生成完成
512
+ if self .wait_for_file (mp3_path ):
513
+ print (f"Conversion to { mp3_path } completed" )
514
+ return mp3_path
515
+ else :
516
+ print ("MP3 conversion timeout" )
517
+ return None
518
+ except subprocess .CalledProcessError as e :
519
+ print (f"Conversion failed: { e .stderr } " if e .stderr else "Unknown error" )
520
+ return None
521
+ finally :
522
+ # 关闭 Xvfb
523
+ xvfb_process .terminate ()
524
+ xvfb_process .wait ()
525
+ else :
526
+ try :
527
+ subprocess .run (
528
+ [musescore4_path , '-o' , mp3_path , xml_path ],
529
+ check = True ,
530
+ capture_output = True ,
531
+ )
532
+ # 等待MP3文件生成完成
533
+ if self .wait_for_file (mp3_path ):
534
+ print (f"Conversion to { mp3_path } completed" )
535
+ return mp3_path
536
+ else :
537
+ print ("MP3 conversion timeout" )
538
+ return None
539
+ except subprocess .CalledProcessError as e :
540
+ print (f"Conversion failed: { e .stderr } " if e .stderr else "Unknown error" )
483
541
return None
484
- except subprocess .CalledProcessError as e :
485
- print (f"Conversion failed: { e .stderr } " if e .stderr else "Unknown error" )
486
- return None
487
542
488
543
def xml2png (self , xml_path , musescore4_path ):
489
544
import subprocess
545
+ import sys
546
+ import tempfile
490
547
491
548
base_png_path = xml_path .rsplit ("." , 1 )[0 ]
492
- try :
493
- subprocess .run (
494
- [musescore4_path , '-o' , f"{ base_png_path } .png" , xml_path ],
495
- check = True ,
496
- capture_output = True ,
497
- )
498
- # 等待PNG序列生成完成
499
- png_files = self .wait_for_png_sequence (base_png_path )
500
- if png_files :
501
- print (f"Converted to { len (png_files )} PNG files" )
502
- return png_files
503
- else :
504
- print ("PNG conversion timeout" )
549
+ # 检测操作系统是否为 Linux
550
+ if sys .platform == "linux" :
551
+ try :
552
+ # 使用不同的显示端口
553
+ display_number = 100
554
+ os .environ ["DISPLAY" ] = f":{ display_number } "
555
+
556
+ # 检查并清理旧的 Xvfb 锁文件
557
+ tmp_dir = tempfile .mkdtemp ()
558
+ xvfb_lock_file = os .path .join (tmp_dir , f".X{ display_number } -lock" )
559
+ if os .path .exists (xvfb_lock_file ):
560
+ print (f"清理旧的 Xvfb 锁文件: { xvfb_lock_file } " )
561
+ os .remove (xvfb_lock_file )
562
+
563
+ # 杀死所有残留的 Xvfb 进程
564
+ subprocess .run (["pkill" , "Xvfb" ], stderr = subprocess .DEVNULL ) # 忽略错误
565
+ time .sleep (1 ) # 等待进程终止
566
+
567
+ # 启动 Xvfb
568
+ xvfb_process = subprocess .Popen (["Xvfb" , f":{ display_number } " , "-screen" , "0" , "1024x768x24" ])
569
+ time .sleep (2 ) # 等待 Xvfb 启动
570
+
571
+ # 设置 Qt 插件环境变量
572
+ os .environ ["QT_QPA_PLATFORM" ] = "offscreen"
573
+
574
+ # 运行 mscore 命令
575
+ subprocess .run (
576
+ [musescore4_path , '-o' , f"{ base_png_path } .png" , xml_path ],
577
+ check = True ,
578
+ capture_output = True ,
579
+ )
580
+ # 等待PNG序列生成完成
581
+ png_files = self .wait_for_png_sequence (base_png_path )
582
+ if png_files :
583
+ print (f"Converted to { len (png_files )} PNG files" )
584
+ return png_files
585
+ else :
586
+ print ("PNG conversion timeout" )
587
+ return None
588
+ except subprocess .CalledProcessError as e :
589
+ print (f"Conversion failed: { e .stderr } " if e .stderr else "Unknown error" )
590
+ return None
591
+ finally :
592
+ # 关闭 Xvfb
593
+ xvfb_process .terminate ()
594
+ xvfb_process .wait ()
595
+ else :
596
+ try :
597
+ subprocess .run (
598
+ [musescore4_path , '-o' , f"{ base_png_path } .png" , xml_path ],
599
+ check = True ,
600
+ capture_output = True ,
601
+ )
602
+ # 等待PNG序列生成完成
603
+ png_files = self .wait_for_png_sequence (base_png_path )
604
+ if png_files :
605
+ print (f"Converted to { len (png_files )} PNG files" )
606
+ return png_files
607
+ else :
608
+ print ("PNG conversion timeout" )
609
+ return None
610
+ except subprocess .CalledProcessError as e :
611
+ print (f"Conversion failed: { e .stderr } " if e .stderr else "Unknown error" )
505
612
return None
506
- except subprocess .CalledProcessError as e :
507
- print (f"Conversion failed: { e .stderr } " if e .stderr else "Unknown error" )
508
- return None
509
613
510
614
def abc2xml (self , abc_path , output_dir , python_path ):
511
615
import subprocess
0 commit comments