run-and-show-of-real_ESRGAN.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- encoding:utf-8 -*-
  2. import sys
  3. import cv2
  4. import numpy as np
  5. import os
  6. import subprocess
  7. from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QSlider, QLabel
  8. from PyQt5.QtCore import Qt
  9. from PyQt5.QtGui import QPixmap, QImage
  10. class ImageComparisonApp(QWidget):
  11. def __init__(self, inpath, outpath):
  12. self.inpath = inpath
  13. self.outpath = outpath
  14. super().__init__()
  15. # 初始化变量
  16. self.original_img = cv2.imread(self.inpath)
  17. self.processed_img = cv2.imread(self.outpath)
  18. # 检查图像是否成功加载
  19. if self.original_img is None:
  20. print(f"Error: Unable to load original image: {self.inpath}")
  21. sys.exit(1)
  22. if self.processed_img is None:
  23. print(f"Error: Unable to load processed image: {self.outpath}")
  24. sys.exit(1)
  25. # 调整两张图像大小以匹配
  26. target_height = max(self.original_img.shape[0], self.processed_img.shape[0])
  27. self.original_resized = cv2.resize(
  28. self.original_img,
  29. (
  30. int(
  31. self.original_img.shape[1]
  32. * target_height
  33. / self.original_img.shape[0]
  34. ),
  35. target_height,
  36. ),
  37. )
  38. self.processed_resized = cv2.resize(
  39. self.processed_img,
  40. (
  41. int(
  42. self.processed_img.shape[1]
  43. * target_height
  44. / self.processed_img.shape[0]
  45. ),
  46. target_height,
  47. ),
  48. )
  49. self.initUI()
  50. def initUI(self):
  51. self.setWindowTitle("Image Comparison")
  52. self.setGeometry(100, 100, 800, 600)
  53. self.slider = QSlider(Qt.Horizontal)
  54. self.slider.setRange(0, self.original_resized.shape[1])
  55. self.slider.setValue(self.original_resized.shape[1] // 2)
  56. self.slider.setTickPosition(QSlider.TicksBelow)
  57. self.slider.setTickInterval(1)
  58. self.slider.valueChanged.connect(self.update_image)
  59. layout = QVBoxLayout()
  60. self.image_label = QLabel()
  61. layout.addWidget(self.image_label)
  62. layout.addWidget(self.slider)
  63. self.setLayout(layout)
  64. def update_image(self):
  65. split_pos = self.slider.value()
  66. left_img = self.original_resized[:, :split_pos]
  67. right_img = self.processed_resized[:, split_pos:]
  68. combined_img = np.hstack((left_img, right_img))
  69. height, width, channel = combined_img.shape
  70. bytes_per_line = 3 * width
  71. q_image = QImage(
  72. combined_img.data, width, height, bytes_per_line, QImage.Format_BGR888
  73. )
  74. pixmap = QPixmap(q_image)
  75. self.image_label.setPixmap(pixmap)
  76. if __name__ == "__main__":
  77. inpath = input("Enter input image path: ")
  78. outpath = input("Enter output image path: ")
  79. output_image = os.path.join(outpath, "output.png")
  80. model_path = "models"
  81. command = [
  82. "realesrgan-ncnn-vulkan.exe",
  83. "-i",
  84. inpath,
  85. "-o",
  86. output_image,
  87. "-s",
  88. "4",
  89. "-m",
  90. model_path,
  91. "-n",
  92. "realesrgan-x4plus",
  93. ]
  94. try:
  95. subprocess.run(command, check=True)
  96. print(f"Image processed successfully. Output saved to: {output_image}")
  97. app = QApplication(sys.argv)
  98. ex = ImageComparisonApp(inpath, output_image)
  99. ex.show()
  100. sys.exit(app.exec_())
  101. except subprocess.CalledProcessError as e:
  102. print(f"Error occurred during processing: {e}")
  103. sys.exit(1)