ripeness_results_panel.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. """
  2. Ripeness Results Panel
  3. Panel for displaying ripeness classification results with confidence bars.
  4. """
  5. from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QProgressBar, QSizePolicy
  6. from PyQt5.QtCore import Qt
  7. from PyQt5.QtGui import QFont
  8. from ui.widgets.panel_header import PanelHeader
  9. from ui.widgets.confidence_bar import ConfidenceBar
  10. class RipenessResultsPanel(QWidget):
  11. """
  12. Panel for displaying ripeness analysis results.
  13. """
  14. def __init__(self, parent=None):
  15. super().__init__(parent)
  16. self.init_ui()
  17. def init_ui(self):
  18. """Initialize the panel UI."""
  19. # Set size policy to expand equally
  20. self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
  21. layout = QVBoxLayout(self)
  22. layout.setContentsMargins(0, 0, 0, 0)
  23. layout.setSpacing(0)
  24. # Main panel container with card styling
  25. self.setStyleSheet("""
  26. QWidget {
  27. background-color: white;
  28. border: 1px solid #ddd;
  29. }
  30. """)
  31. # Header
  32. header = QWidget()
  33. header.setFixedHeight(25)
  34. header.setStyleSheet("background-color: #27ae60;")
  35. header_layout = QHBoxLayout(header)
  36. header_layout.setContentsMargins(10, 0, 0, 0)
  37. title = QLabel("Ripeness Analysis Results")
  38. title.setStyleSheet("color: white; font-weight: bold; font-size: 16px;")
  39. header_layout.addWidget(title)
  40. # Current Classification
  41. classification_label = QLabel("Current Classification:")
  42. classification_label.setStyleSheet("font-weight: bold; font-size: 12px; margin: 8px 10px 5px 10px; color: #2c3e50;")
  43. classification_frame = QWidget()
  44. classification_frame.setStyleSheet("background-color: #95a5a6;")
  45. classification_frame.setMinimumHeight(45)
  46. classification_layout = QVBoxLayout(classification_frame)
  47. classification_layout.setAlignment(Qt.AlignCenter)
  48. classification_layout.setContentsMargins(5, 5, 5, 5)
  49. self.class_display = QLabel("—")
  50. self.class_display.setAlignment(Qt.AlignCenter)
  51. self.class_display.setStyleSheet("color: white; font-weight: bold; font-size: 18px;")
  52. classification_layout.addWidget(self.class_display)
  53. # Confidence Scores
  54. confidence_label = QLabel("Confidence Scores:")
  55. confidence_label.setStyleSheet("font-weight: bold; font-size: 11px; margin: 8px 10px 5px 10px; color: #2c3e50;")
  56. # Create progress bars for each category
  57. categories = [
  58. ("Unripe", 0, "#95a5a6"),
  59. ("Ripe", 0, "#27ae60"),
  60. ("Overripe", 0, "#e74c3c")
  61. ]
  62. confidence_layout = QVBoxLayout()
  63. confidence_layout.setSpacing(4)
  64. self.confidence_bars = {}
  65. for name, value, color in categories:
  66. category_frame = QWidget()
  67. category_layout = QHBoxLayout(category_frame)
  68. category_layout.setContentsMargins(10, 0, 10, 0)
  69. category_layout.setSpacing(8)
  70. name_label = QLabel(f"{name}:")
  71. name_label.setFixedWidth(60)
  72. name_label.setStyleSheet("font-size: 10px; color: #2c3e50;")
  73. progress = QProgressBar()
  74. progress.setRange(0, 100)
  75. progress.setValue(int(value))
  76. progress.setTextVisible(False)
  77. progress.setStyleSheet(f"""
  78. QProgressBar {{
  79. background-color: #ecf0f1;
  80. border: 1px solid #bdc3c7;
  81. border-radius: 0px;
  82. height: 15px;
  83. }}
  84. QProgressBar::chunk {{
  85. background-color: {color};
  86. }}
  87. """)
  88. value_label = QLabel(f"{value}%")
  89. value_label.setFixedWidth(45)
  90. value_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
  91. value_label.setStyleSheet("font-size: 12px; color: #2c3e50;")
  92. category_layout.addWidget(name_label)
  93. category_layout.addWidget(progress)
  94. category_layout.addWidget(value_label)
  95. confidence_layout.addWidget(category_frame)
  96. self.confidence_bars[name] = (progress, value_label)
  97. # Processing time
  98. self.info_label = QLabel("")
  99. self.info_label.setStyleSheet("color: #7f8c8d; font-size: 12px; margin: 8px 10px 5px 10px;")
  100. layout.addWidget(header)
  101. layout.addWidget(classification_label)
  102. layout.addWidget(classification_frame)
  103. layout.addWidget(confidence_label)
  104. layout.addLayout(confidence_layout)
  105. layout.addWidget(self.info_label)
  106. layout.addStretch()
  107. def update_results(self, classification: str, probabilities: dict,
  108. processing_time: float = 0, model_version: str = "RipeNet v3.2"):
  109. """
  110. Update the results display.
  111. Args:
  112. classification: Predicted class name
  113. probabilities: Dictionary of class probabilities (0-1)
  114. processing_time: Processing time in seconds
  115. model_version: Model version string
  116. """
  117. # Update classification display
  118. self.class_display.setText(classification.upper())
  119. # Set color based on classification
  120. class_colors = {
  121. "Unripe": "#95a5a6",
  122. "Ripe": "#27ae60",
  123. "Overripe": "#e74c3c"
  124. }
  125. bg_color = class_colors.get(classification, "#95a5a6")
  126. self.class_display.parent().setStyleSheet(f"background-color: {bg_color};")
  127. self.class_display.setStyleSheet("""
  128. color: white;
  129. font-weight: bold;
  130. font-size: 18px;
  131. """)
  132. # Update confidence bars
  133. for class_name, (progress_bar, value_label) in self.confidence_bars.items():
  134. prob = probabilities.get(class_name, 0)
  135. percentage = prob * 100
  136. # Update progress bar
  137. progress_bar.setValue(int(percentage))
  138. # Update value label
  139. value_label.setText(f"{percentage:.1f}%")
  140. # Highlight primary class
  141. if class_name == classification:
  142. value_label.setStyleSheet("font-size: 12px; font-weight: bold; color: #2c3e50;")
  143. else:
  144. value_label.setStyleSheet("font-size: 12px; color: #2c3e50;")
  145. # Update info label
  146. self.info_label.setText(
  147. f"Processing Time: {processing_time:.2f}s | Model: {model_version}"
  148. )
  149. def clear_results(self):
  150. """Clear all results."""
  151. self.class_display.setText("—")
  152. self.class_display.setStyleSheet("""
  153. color: white;
  154. font-weight: bold;
  155. font-size: 18px;
  156. """)
  157. for progress_bar, value_label in self.confidence_bars.values():
  158. progress_bar.setValue(0)
  159. value_label.setText("0%")
  160. value_label.setStyleSheet("font-size: 12px; color: #2c3e50;")
  161. self.info_label.setText("")