score_img.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QFileDialog
  3. import os
  4. import numpy as np
  5. import cv2
  6. from skimage import filters
  7. class ImageFileViewer(QWidget):
  8. def __init__(self):
  9. super().__init__()
  10. self.setGeometry(100, 100, 500, 500) # 设置窗口大小和位置
  11. self.setWindowTitle("图片打分工具")
  12. self.layout = QVBoxLayout()
  13. self.label = QLabel("暂无图片")
  14. self.button = QPushButton("请选择图片")
  15. self.button.clicked.connect(self.select_image)
  16. self.layout.addWidget(self.label)
  17. self.layout.addWidget(self.button)
  18. self.setLayout(self.layout)
  19. def _imageToMatrix(self, image):
  20. """
  21. 根据名称读取图片对象转化矩阵
  22. :param strName:
  23. :return: 返回矩阵
  24. """
  25. imgMat = np.matrix(image)
  26. return imgMat
  27. def preImgOps(self, img):
  28. """
  29. 图像的预处理操作
  30. :param img: 图像的而明朝
  31. :return: 灰度化和resize之后的图片对象
  32. """
  33. # 预处理操作
  34. try:
  35. reImg = cv2.resize(img, (300, 400), interpolation=cv2.INTER_CUBIC) #
  36. img2gray = cv2.cvtColor(reImg, cv2.COLOR_BGR2GRAY) # 将图片压缩为单通道的灰度图
  37. return reImg,img2gray, True
  38. except Exception as e:
  39. return None, None, False
  40. def ReadImage(self, im_file):
  41. """
  42. 读取图片
  43. :param im_file: 图像的路径
  44. :return: 利用opencv读取的完整图像img 托虫板内接四边形图像cutimg 内接四边形的坐标c[xmin,ymin,xmax,ymax]
  45. """
  46. try:
  47. img = cv2.imdecode(np.fromfile(im_file, dtype=np.uint8), cv2.IMREAD_COLOR)
  48. ImgShape = img.shape
  49. center = [ImgShape[1]/2,ImgShape[0]/2] #圆盘中心
  50. radius = int(ImgShape[0]/2) #圆盘半径
  51. length = pow(2,0.5)*radius #圆的内接正方形边长
  52. Size = int(length/2)
  53. xmin = int(center[0] - Size)
  54. xmax = int(center[0] + Size)
  55. ymin = int((center[1] - Size)*1.1)
  56. ymax = int(center[1] + Size)
  57. c = [xmin,ymin,xmax,ymax]
  58. cutimg = img[ymin:ymax,xmin:xmax]
  59. return img,cutimg,c, True
  60. except Exception as e:
  61. return None, None, None, False
  62. def getFileName(self, imageFile):
  63. url, FileName = os.path.split(imageFile)
  64. tamp = []
  65. tamp.append(FileName)
  66. return tamp
  67. #图像清晰度检测
  68. def predict(self, imageFile):
  69. """
  70. 图像清晰度检测
  71. Args:
  72. imageFile(dir or file):单张图片或者存放图片的文件夹
  73. """
  74. result = [] #存放清晰度评分
  75. imgList = [] #存放识别后图片
  76. if os.path.isfile(imageFile):
  77. img,cutimg,c, is_get = self.ReadImage(imageFile)
  78. if is_get:
  79. reImg, img2gray, is_imgs= self.preImgOps(cutimg)
  80. if is_imgs:
  81. f = self._imageToMatrix(img2gray)
  82. tmp = filters.sobel(f)
  83. source=np.sum(tmp**2)
  84. source=np.sqrt(source)
  85. result.append(source)
  86. imgList.append(img)
  87. return result
  88. def select_image(self):
  89. file_dialog = QFileDialog()
  90. image_file, _ = file_dialog.getOpenFileName(self, "选择图片", "", "支持图片格式 (*.png *.jpg *.jpeg)")
  91. if image_file:
  92. result = self.predict(image_file)
  93. self.label.setText(f"图片 {image_file.split('/')[-1]}:\n得分 {result[0]}")
  94. if __name__ == "__main__":
  95. app = QApplication(sys.argv)
  96. viewer = ImageFileViewer()
  97. viewer.show()
  98. sys.exit(app.exec_())