File: frame_selector.py

package info (click to toggle)
planetary-system-stacker 0.8.32~git20230901.01f3625-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 50,468 kB
  • sloc: python: 14,055; makefile: 3
file content (633 lines) | stat: -rw-r--r-- 25,607 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# -*- coding: utf-8; -*-
"""
Copyright (c) 2019 Rolf Hempel, rolf6419@gmx.de

This file is part of the PlanetarySystemStacker tool (PSS).
https://github.com/Rolf-Hempel/PlanetarySystemStacker

PSS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PSS.  If not, see <http://www.gnu.org/licenses/>.

Part of this module (in class "AlignmentPointEditor" was copied from
https://stackoverflow.com/questions/35508711/how-to-enable-pan-and-zoom-in-a-qgraphicsview

"""
# The following PyQt5 imports must precede any matplotlib imports. This is a workaround
# for a Matplotlib 2.2.2 bug.

from glob import glob
from sys import argv, exit
from time import sleep

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt

from configuration import Configuration
from exceptions import Error
from frame_selector_gui import Ui_frame_selector
from frame_viewer import FrameViewer
from frames import Frames
from miscellaneous import Miscellaneous
from rank_frames import RankFrames


class VideoFrameSelector(FrameViewer):
    """
    This widget implements a frame viewer for frames in a video file. Panning and zooming is
    implemented by using the mouse and scroll wheel.

    """

    def __init__(self, frames, index_included, frame_index=0):
        super(VideoFrameSelector, self).__init__()
        self.frames = frames
        self.index_included = index_included
        self.frame_index = frame_index

        self.setPhoto(self.frame_index)

    def setPhoto(self, index):
        """
        Convert a grayscale image to a pixmap and assign it to the photo object. If the image is
        marked as excluded from the stacking workflow, place a crossed-out red circle in the
        upper left image corner.

        :param index: Index into the frame list. Frames are assumed to be grayscale image in format
                      float32.
        :return: -
        """

        # Indicate that an image is being loaded.
        self.image_loading_busy = True

        image = self.frames.frames_mono(index)

        super(VideoFrameSelector, self).setPhoto(image,
                                                overlay_exclude_mark=not self.index_included[index])


class FrameSelectorWidget(QtWidgets.QFrame, Ui_frame_selector):
    """
    This widget implements a frame viewer together with control elements to visualize frame
    qualities, and to manipulate the stack limits.
    """

    frame_player_start_signal = QtCore.pyqtSignal()

    def __init__(self, parent_gui, configuration, frames, rank_frames, stacked_image_log_file,
                 signal_finished):
        """
        Initialization of the widget.

        :param parent_gui: Parent GUI object
        :param configuration: Configuration object with parameters
        :param frames: Frames object with all video frames
        :param rank_frames: RankFrames object with global quality ranks (between 0. and 1.,
                            1. being optimal) for all frames
        :param stacked_image_log_file: Log file to be stored with results, or None.
        :param signal_finished: Qt signal to trigger the next activity when the viewer exits.
        """

        super(FrameSelectorWidget, self).__init__(parent_gui)
        self.setupUi(self)

        # Keep references to upper level objects.
        self.parent_gui = parent_gui
        self.configuration = configuration
        self.stacked_image_log_file = stacked_image_log_file
        self.signal_finished = signal_finished
        self.frames = frames
        self.index_included = frames.index_included.copy()
        self.quality_sorted_indices = rank_frames.quality_sorted_indices
        self.rank_indices = rank_frames.rank_indices

        # Start with ordering frames by quality. This can be changed by the user using a radio
        # button.
        self.frame_ordering = "quality"

        # Initialize the frame list selection.
        self.items_selected = None
        self.indices_selected = None

        # Set colors for the frame list.
        self.background_included = QtGui.QColor(130, 255, 130)
        self.foreground_included = QtGui.QColor(0, 0, 0)
        self.background_excluded = QtGui.QColor(120, 120, 120)
        self.foreground_excluded = QtGui.QColor(255, 255, 255)

        self.addButton.clicked.connect(self.use_triggered)
        self.removeButton.clicked.connect(self.not_use_triggered)

        # Be careful: Indices are counted from 0, while widget contents are counted from 1 (to make
        # it easier for the user.
        self.quality_index = 0
        self.frame_index = self.quality_sorted_indices[self.quality_index]

        # Set up the frame selector and put it in the upper left corner.
        self.frame_selector = VideoFrameSelector(self.frames, self.index_included, self.frame_index)
        self.frame_selector.setObjectName("frame_selector")
        self.gridLayout.addWidget(self.frame_selector, 0, 0, 2, 3)

        # Initialize the list widget.
        self.fill_list_widget()
        self.listWidget.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.listWidget.installEventFilter(self)
        self.listWidget.itemClicked.connect(self.select_items)
        self.listWidget.currentRowChanged.connect(self.synchronize_slider)

        # Group widget elements which are to be blocked during player execution in a list.
        self.widget_elements = [self.listWidget,
                                self.slider_frames,
                                self.addButton,
                                self.removeButton,
                                self.pushButton_play,
                                self.GroupBox_frame_sorting]

        # Initialize a variable for communication with the frame_player object later.
        self.run_player = False

        # Create the frame player thread and start it. The player displays frames in succession.
        # It is pushed on a different thread because otherwise the user could not stop it before it
        # finishes.
        self.player_thread = QtCore.QThread()
        self.frame_player = FramePlayer(self)
        self.frame_player.moveToThread(self.player_thread)
        self.frame_player.block_widgets_signal.connect(self.block_widgets)
        self.frame_player.unblock_widgets_signal.connect(self.unblock_widgets)
        self.frame_player.set_photo_signal.connect(self.frame_selector.setPhoto)
        self.frame_player.set_slider_value.connect(self.slider_frames.setValue)
        self.frame_player_start_signal.connect(self.frame_player.play)
        self.player_thread.start()

        # Initialization of GUI elements
        self.slider_frames.setMinimum(1)
        self.slider_frames.setMaximum(self.frames.number)
        self.slider_frames.setValue(self.quality_index + 1)
        self.radioButton_quality.setChecked(True)

        self.gridLayout.setColumnStretch(0, 7)
        self.gridLayout.setColumnStretch(1, 0)
        self.gridLayout.setColumnStretch(2, 0)
        self.gridLayout.setColumnStretch(3, 0)
        self.gridLayout.setColumnStretch(4, 1)
        self.gridLayout.setRowStretch(0, 0)
        self.gridLayout.setRowStretch(1, 0)

        # Connect signals with slots.
        self.buttonBox.accepted.connect(self.done)
        self.buttonBox.rejected.connect(self.reject)
        self.slider_frames.valueChanged.connect(self.slider_frames_changed)
        self.pushButton_play.clicked.connect(self.pushbutton_play_clicked)
        self.pushButton_stop.clicked.connect(self.pushbutton_stop_clicked)
        self.radioButton_quality.toggled.connect(self.radiobutton_quality_changed)

        if self.configuration.global_parameters_protocol_level > 0:
            Miscellaneous.protocol("+++ Start selecting frames +++", self.stacked_image_log_file)

    def block_widgets(self):
        """
        Disable GUI elements to prevent unwanted user interaction while the frame player is running.

        :return: -
        """

        # De-select all items in the listWidget when the player starts.
        self.listWidget.clearSelection()

        for element in self.widget_elements:
            element.setDisabled(True)

    def unblock_widgets(self):
        """
        Enable GUI elements which were temporarily blocked when the player stops.

        :return: -
        """

        for element in self.widget_elements:
            element.setDisabled(False)

        self.listWidget.setFocus()

    def fill_list_widget(self):
        """
        Initialize the list widget with frames, ordered as selected (by rank or chronological).
        Set the colors of each item according to its current inclusion / exclusion state.

        :return: -
        """

        # Initialize the inclusion / exclusion state of frames in the frame list.
        self.listWidget.clear()
        for i in range(self.frames.number_original):
            if self.frame_ordering == "quality":
                frame_number = self.quality_sorted_indices[i]
            else:
                frame_number = i

            if self.index_included[frame_number]:
                item = QtWidgets.QListWidgetItem("Frame %i included" % (frame_number + 1))
                item.setBackground(self.background_included)
                item.setForeground(self.foreground_included)
            else:
                item = QtWidgets.QListWidgetItem("Frame %i excluded" % (frame_number + 1))
                item.setBackground(self.background_excluded)
                item.setForeground(self.foreground_excluded)
            self.listWidget.addItem(item)

        # Set the list widget to the current position.
        if self.frame_ordering == "quality":
            self.listWidget.setCurrentRow(self.quality_index,
                                          QtCore.QItemSelectionModel.SelectCurrent)
        else:
            self.listWidget.setCurrentRow(self.frame_index,
                                          QtCore.QItemSelectionModel.SelectCurrent)

    def select_items(self):
        """
        If a list item or a range of items is selected, store the items and corresponding indices.
        Synchronize the frame slider and frame viewer with the the current list position.

        :return: -
        """

        self.listWidget.currentItem().setSelected(True)
        self.items_selected = self.listWidget.selectedItems()

        if self.frame_ordering == "quality":
            self.indices_selected = [self.quality_sorted_indices[self.listWidget.row(item)] for item
                                     in self.items_selected]
            self.frame_index = self.indices_selected[0]
            self.quality_index = self.rank_indices[self.frame_index]
        else:
            self.indices_selected = [self.listWidget.row(item) for item in self.items_selected]
            self.frame_index = self.indices_selected[0]
            self.quality_index = self.rank_indices[self.frame_index]

        self.synchronize_slider()

    def synchronize_slider(self):
        """
        Set the frame slider to the value currently selected in the listWidget. Update the photo
        displayed in the viewer.

        :return: -
        """

        # Block slider signals to avoid a shortcut.
        self.slider_frames.blockSignals(True)

        if self.frame_ordering == "quality":
            self.quality_index = self.listWidget.currentRow()
            self.frame_index = self.quality_sorted_indices[self.quality_index]
            self.slider_frames.setValue(self.quality_index + 1)
        else:
            self.frame_index = self.listWidget.currentRow()
            self.quality_index = self.rank_indices[self.frame_index]
            self.slider_frames.setValue(self.frame_index + 1)

        # Unblock the slider signals again.
        self.slider_frames.blockSignals(False)

        # Update the image in the viewer.
        self.frame_selector.setPhoto(self.frame_index)

    def eventFilter(self, source, event):
        """
        This eventFilter is listening for events on the listWidget. List items can be marked as
        included / excluded by either using a context menu, by pressing the "+" or "-" buttons, or
        by pressing the keyboard keys "+" or "-".

        :param source: Source object to listen on.
        :param event: Event found
        :return: -
        """

        if source is self.listWidget:

            # Open a context menu with two choices. Depending on the user's choice, either
            # trigger the "use_triggered" or "not_use_triggered" method below.
            if event.type() == QtCore.QEvent.ContextMenu:
                menu = QtWidgets.QMenu()
                action1 = QtWidgets.QAction('Use for stacking', menu)
                action1.triggered.connect(self.use_triggered)
                menu.addAction((action1))
                action2 = QtWidgets.QAction("Don't use for stacking", menu)
                action2.triggered.connect(self.not_use_triggered)
                menu.addAction((action2))
                menu.exec_(event.globalPos())

            # Do the same as above if the user prefers to use the keyboard keys "+" or "-".
            elif event.type() == QtCore.QEvent.KeyPress:
                if event.key() == Qt.Key_Plus:
                    self.use_triggered()
                elif event.key() == Qt.Key_Minus:
                    self.not_use_triggered()
                elif event.key() == Qt.Key_Escape:
                    self.items_selected = []
                    self.indices_selected = []
                    self.listWidget.clearSelection()

        return super(FrameSelectorWidget, self).eventFilter(source, event)

    def use_triggered(self):
        """
        The user has selected a list item or a range of items to be included in the stacking
        workflow. Change the appearance of the list entry, update the "index_included" values for
        the corresponding frames, and reload the image of the current index. The latter step is
        important to update the overlay mark in the upper left image corner.

        :return: -
        """

        self.select_items()
        if self.items_selected:
            for index, item in enumerate(self.items_selected):
                index_selected = self.indices_selected[index]
                frame_selected = index_selected + 1
                item.setText("Frame %i included" % frame_selected)
                item.setBackground(self.background_included)
                item.setForeground(QtGui.QColor(0, 0, 0))
                self.index_included[index_selected] = True
                self.frame_selector.setPhoto(self.frame_index)

    def not_use_triggered(self):
        """
        Same as above in case the user has de-selected a list item or a range of items.
        :return: -
        """

        self.select_items()
        if self.items_selected:
            for index, item in enumerate(self.items_selected):
                index_selected = self.indices_selected[index]
                frame_selected = index_selected + 1
                item.setText("Frame %i excluded" % frame_selected)
                item.setBackground(self.background_excluded)
                item.setForeground(QtGui.QColor(255, 255, 255))
                self.index_included[index_selected] = False
                self.frame_selector.setPhoto(self.frame_index)

    def slider_frames_changed(self):
        """
        The frames slider is changed by the user. Update the frame in the viewer and scroll the
        frame list to show the current frame index.

        :return: -
        """

        # Again, please note the difference between indexing and GUI displays.
        index = self.slider_frames.value() - 1

        # Differentiate between frame ordering (by quality or chronologically).
        if self.frame_ordering == "quality":
            self.frame_index = self.quality_sorted_indices[index]
            self.quality_index = index

        else:
            self.frame_index = index
            self.quality_index = self.rank_indices[self.frame_index]

        # Adjust the frame list and select the current frame.

        self.listWidget.setCurrentRow(index, QtCore.QItemSelectionModel.SelectCurrent)

        # Update the image in the viewer.
        self.frame_selector.setPhoto(self.frame_index)
        self.listWidget.setFocus()

    def radiobutton_quality_changed(self):
        """
        Toggle back and forth between frame ordering modes. The frame slider is updated to reflect
        the index of the current frame in the new ordering scheme. The frame ordering in the list
        widget is changed.

        :return: -
        """

        # Block listWidget signals. Otherwise, changes to the widget triggered by changing the
        # slider would cause trouble.
        self.listWidget.blockSignals(True)

        if self.frame_ordering == "quality":
            self.frame_ordering = "chronological"
            self.slider_frames.setValue(self.frame_index + 1)
        else:
            self.frame_ordering = "quality"
            self.slider_frames.setValue(self.quality_index + 1)

        self.fill_list_widget()

        # Unblock listWidget signals again.
        self.listWidget.blockSignals(False)

    def pushbutton_play_clicked(self):
        """
        Start the frame player if it is not running already.

        :return: -
        """

        self.frame_player_start_signal.emit()

    def pushbutton_stop_clicked(self):
        """
        When the frame player is running, it periodically checks this variable. If it is set to
        False, the player stops.

        :return: -
        """

        if self.frame_player.run_player:
            self.frame_player.run_player = False

    def done(self):
        """
        On exit from the frame viewer, update the selection status of all frames and send a
        completion signal.

        :return: -
        """

        # Check if the status of frames has changed.
        indices_included = []
        indices_excluded = []
        for index in range(self.frames.number_original):
            if self.index_included[index] and not self.frames.index_included[index]:
                indices_included.append(index)
                self.frames.index_included[index] = True
            elif not self.index_included[index] and self.frames.index_included[index]:
                indices_excluded.append(index)
                self.frames.index_included[index] = False

        # Write the changes in frame selection to the protocol.
        if self.configuration.global_parameters_protocol_level > 1:
            if indices_included:
                Miscellaneous.protocol(
                    "           The user has included the following frames into the stacking "
                    "workflow: " + str(
                        [item + 1 for item in indices_included]), self.stacked_image_log_file,
                    precede_with_timestamp=False)
            if indices_excluded:
                Miscellaneous.protocol(
                    "           The user has excluded the following frames from the stacking "
                    "workflow: " + str(
                        [item + 1 for item in indices_excluded]), self.stacked_image_log_file,
                    precede_with_timestamp=False)
            frames_remaining = sum(self.frames.index_included)
            if frames_remaining != self.frames.number:
                Miscellaneous.protocol("           " + str(
                    frames_remaining) + " frames will be used in the stacking workflow.",
                                       self.stacked_image_log_file, precede_with_timestamp=False)

        # Send a completion message. The "execute_rank_frames" method is triggered on the workflow
        # thread. The signal payload is True if the status was changed for at least one frame.
        # In this case, the index translation table is updated before the frame ranking starts.
        if self.parent_gui is not None:
            self.signal_finished.emit()

        # Close the Window.
        self.player_thread.quit()
        self.close()

    def reject(self):
        """
        If the "cancel" button is pressed, the coordinates are not stored.

        :return: -
        """

        # Send a completion message.
        if self.parent_gui is not None:
            self.signal_finished.emit()

        # Close the Window.
        self.player_thread.quit()
        self.close()


class FramePlayer(QtCore.QObject):
    """
    This class implements a video player using the FrameViewer and the control elements of the
    FrameViewerWidget. The player is started by the widget on a separate thread. This way the user
    can instruct the GUI to stop the running player.

    """
    block_widgets_signal = QtCore.pyqtSignal()
    unblock_widgets_signal = QtCore.pyqtSignal()
    set_photo_signal = QtCore.pyqtSignal(int)
    set_slider_value = QtCore.pyqtSignal(int)

    def __init__(self, frame_selector_widget):
        super(FramePlayer, self).__init__()

        # Store a reference of the frame selector widget and create a list of GUI elements. This
        # makes it easier to perform the same operation on all elements.
        self.frame_selector_widget = frame_selector_widget

        # Set the delay time between frames.
        self.delay_between_frames = 0.1

        # Initialize a variable used to stop the player in the GUI thread.
        self.run_player = False

    def play(self):
        """
        Start the player.
        :return: -
        """

        # Block signals from GUI elements to avoid cross-talk, and disable them to prevent unwanted
        # user interaction.
        self.block_widgets_signal.emit()

        # Set the player running.
        self.run_player = True

        # The frames are ordered by their quality.
        if self.frame_selector_widget.frame_ordering == "quality":

            # The player stops when the end of the video is reached, or when the "run_player"
            # variable is set to False in the GUI thread.
            while self.frame_selector_widget.quality_index < self.frame_selector_widget.frames.number_original \
                    - 1 and self.run_player:

                if not self.frame_selector_widget.frame_selector.image_loading_busy:
                    self.frame_selector_widget.quality_index += 1
                    self.frame_selector_widget.frame_index = \
                        self.frame_selector_widget.quality_sorted_indices[
                            self.frame_selector_widget.quality_index]

                    self.set_slider_value.emit(self.frame_selector_widget.quality_index + 1)
                    self.set_photo_signal.emit(self.frame_selector_widget.frame_index)

                # Insert a short pause to keep the video from running too fast.
                sleep(self.delay_between_frames)

        else:
            # The same for chronological frame ordering.
            while self.frame_selector_widget.frame_index < self.frame_selector_widget.frames.number_original \
                    - 1 and self.run_player:
                if not self.frame_selector_widget.frame_selector.image_loading_busy:
                    self.frame_selector_widget.frame_index += 1
                    self.frame_selector_widget.quality_index = \
                        self.frame_selector_widget.quality_sorted_indices.index(
                            self.frame_selector_widget.frame_index)
                    self.set_slider_value.emit(self.frame_selector_widget.frame_index + 1)
                    self.set_photo_signal.emit(self.frame_selector_widget.frame_index)

                sleep(self.delay_between_frames)

        self.run_player = False

        # Re-set the GUI elements to their normal state.
        self.unblock_widgets_signal.emit()


if __name__ == '__main__':
    # Images can either be extracted from a video file or a batch of single photographs. Select
    # the example for the test run.
    type = 'video'
    if type == 'image':
        names = glob(
            'Images/2012*.tif')  # names = glob.glob('Images/Moon_Tile-031*ap85_8b.tif')  # names
        # = glob.glob('Images/Example-3*.jpg')
    else:
        names = 'Videos/another_short_video.avi'
    print(names)

    # Get configuration parameters.
    configuration = Configuration()
    configuration.initialize_configuration()
    try:
        frames = Frames(configuration, names, type=type)
        print("Number of images: " + str(frames.number))
        print("Image shape: " + str(frames.shape))
    except Error as e:
        print("Error: " + e.message)
        exit()

    # Rank the frames by their overall local contrast.
    rank_frames = RankFrames(frames, configuration)
    rank_frames.frame_score()

    print("Best frame index: " + str(rank_frames.frame_ranks_max_index))

    app = QtWidgets.QApplication(argv)
    window = FrameSelectorWidget(None, configuration, frames, rank_frames, None, None)
    window.setMinimumSize(800, 600)
    # window.showMaximized()
    window.show()
    app.exec_()

    exit()