-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
190 lines (152 loc) · 6.85 KB
/
ui.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import sys
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QPushButton, QFileDialog, QDoubleSpinBox, QSizePolicy
)
from PySide6.QtGui import QImage, QPixmap
from PySide6.QtCore import Qt
from PIL import Image
import numpy as np
from config import *
import torch
from src.big_image import process_image
from src.data.utils import get_color_mask, calc_percentage
from src.models.DeepLabv3.model import get_model
model = get_model(SAVE_DIR + 'DeepLabv3_epoch_24.pth')
class ImageProcessorWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Field Segmentation")
self.setGeometry(100, 100, 800, 600)
main_widget = QWidget()
self.setCentralWidget(main_widget)
main_layout = QHBoxLayout(main_widget)
left_panel = QVBoxLayout()
dimensions_layout = QVBoxLayout()
width_layout = QHBoxLayout()
width_layout.addWidget(QLabel("Width:"))
self.width_input = QDoubleSpinBox()
self.width_input.setRange(0.1, 10000.0)
self.width_input.setValue(10.0)
width_layout.addWidget(self.width_input)
width_layout.addWidget(QLabel("km"))
dimensions_layout.addLayout(width_layout)
height_layout = QHBoxLayout()
height_layout.addWidget(QLabel("Height:"))
self.height_input = QDoubleSpinBox()
self.height_input.setRange(0.1, 10000.0)
self.height_input.setValue(5.0)
height_layout.addWidget(self.height_input)
height_layout.addWidget(QLabel("km"))
dimensions_layout.addLayout(height_layout)
image_input_layout = QVBoxLayout()
self.image_path = QLineEdit()
browse_btn = QPushButton("Browse Image")
browse_btn.clicked.connect(self.load_image)
process_btn = QPushButton("Process Image")
process_btn.clicked.connect(self.process_image)
self.original_label = QLabel("Original Image")
self.original_label.setAlignment(Qt.AlignCenter)
self.original_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
left_panel.addLayout(dimensions_layout)
left_panel.addWidget(QLabel("Image Path:"))
left_panel.addWidget(self.image_path)
left_panel.addWidget(browse_btn)
left_panel.addWidget(process_btn)
left_panel.addWidget(self.original_label)
right_panel = QVBoxLayout()
self.processed_label = QLabel("Processed Image")
self.processed_label.setAlignment(Qt.AlignCenter)
self.processed_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
# Compact results display
results_layout = QHBoxLayout()
self.error_label = QLabel("NoErrors")
self.growing_land_label = QLabel("Growing area: -- KM^2")
self.resting_land_label = QLabel("Resting area: -- KM^2")
results_layout.addWidget(self.error_label)
results_layout.addWidget(QLabel("|"))
results_layout.addWidget(self.growing_land_label)
results_layout.addWidget(QLabel("|"))
results_layout.addWidget(self.resting_land_label)
results_layout.addStretch()
self.save_btn = QPushButton("Save Processed Image")
self.save_btn.clicked.connect(self.save_image)
self.save_btn.setEnabled(False)
right_panel.addWidget(self.processed_label)
right_panel.addLayout(results_layout)
right_panel.addWidget(self.save_btn)
main_layout.addLayout(left_panel, 40)
main_layout.addLayout(right_panel, 60)
self.original_image = None
self.processed_image = None
def load_image(self):
path, _ = QFileDialog.getOpenFileName(
self, "Open Image", "", "Image Files (*.png *.jpg *.jpeg)"
)
if path:
self.image_path.setText(path)
self.show_image(path, self.original_label)
def show_image(self, path, label):
image = QImage(path)
if not image.isNull():
scaled = image.scaled(
label.width(), label.height(),
Qt.KeepAspectRatio, Qt.SmoothTransformation
)
label.setPixmap(QPixmap.fromImage(scaled))
def process_image(self):
width = self.width_input.value()
height = self.height_input.value()
image_path = self.image_path.text()
if not image_path:
return
try:
with Image.open(image_path) as img:
if img.mode != 'RGB':
img = img.convert('RGB')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
mask = process_image(img, (width, height), model.to(device), device)
class_percentage = calc_percentage(mask)
img_np = get_color_mask(mask)
if img_np.dtype != np.uint8:
img_np = img_np.astype(np.uint8)
if not img_np.flags['C_CONTIGUOUS']:
img_np = np.ascontiguousarray(img_np)
height, width, channel = img_np.shape
bytes_per_line = 3 * width
processed_qimage = QImage(
img_np.data,
width,
height,
bytes_per_line,
QImage.Format_RGB888
)
growing = width * height * class_percentage[1].item()
resting = width * height * class_percentage[0].item()
self.processed_image = Image.fromarray(img_np)
self.show_processed_image(processed_qimage)
self.growing_land_label.setText(f"Growing: {growing:.2f} KM^2")
self.resting_land_label.setText(f"Resting: {resting:.2f} KM^2")
self.error_label.setText(f"NoErrors")
self.save_btn.setEnabled(True)
except Exception as e:
self.error_label.setText(f"Ooops: {e}")
print(f"Error processing image: {e}")
def show_processed_image(self, qimage):
scaled = qimage.scaled(
self.processed_label.width(), self.processed_label.height(),
Qt.KeepAspectRatio, Qt.SmoothTransformation
)
self.processed_label.setPixmap(QPixmap.fromImage(scaled))
def save_image(self):
if self.processed_image is not None:
path, _ = QFileDialog.getSaveFileName(
self, "Save Image", "", "PNG Image (*.png);;JPEG Image (*.jpg *.jpeg)"
)
if path:
self.processed_image.save(path)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ImageProcessorWindow()
window.show()
sys.exit(app.exec())