implement progress bar

This commit is contained in:
Denis-Cosmin Nutiu 2024-03-22 22:21:21 +02:00
parent 88754a798a
commit 8e311a5865
2 changed files with 19 additions and 11 deletions

View file

@ -5,22 +5,25 @@ import com.nuculabs.dev.imagetagger.ui.controls.ImageTagsEntryControl
import javafx.application.Platform
import javafx.fxml.FXML
import javafx.scene.control.Label
import javafx.scene.control.ProgressBar
import javafx.scene.control.Separator
import javafx.scene.layout.VBox
import javafx.stage.FileChooser
import java.io.File
import java.util.logging.Logger
import javax.imageio.ImageIO
import kotlin.math.log
class MainPageController {
private val logger: Logger = Logger.getLogger("MainPageController")
@FXML
private lateinit var verticalBox: VBox
private lateinit var progressBar: ProgressBar
@FXML
private lateinit var statusLabel: Label
private lateinit var verticalBox: VBox
/**
* Prompts the user to select files then predicts tags for the selected image files.
@ -33,12 +36,15 @@ class MainPageController {
fileChooser.title = "Choose images"
val filePaths = fileChooser.showOpenMultipleDialog(null) ?: return
statusLabel.isVisible = true
progressBar.isVisible = true
progressBar.progress = 0.0;
// Create a new thread to predict the images.
val thread = Thread {
try {
for (filePath in filePaths) {
val filePathsTotal = filePaths.count()
logger.info("Analyzing $filePathsTotal files")
filePaths.forEachIndexed { index, filePath ->
try {
// Get predictions for the image.
val imageFile = ImageIO.read(File(filePath.absolutePath))
val tags: List<String> = imageTagsPrediction.predictTags(imageFile)
@ -46,13 +52,15 @@ class MainPageController {
// Add image and prediction to the view.
verticalBox.children.add(ImageTagsEntryControl(filePath.absolutePath, tags))
verticalBox.children.add(Separator())
progressBar.progress = (((index + 1) * 100) / filePathsTotal).toDouble() / 100.0
logger.info("Progress ${progressBar.progress}")
}
} catch (e: Exception) {
logger.warning("Error while predicting images $e")
}
Platform.runLater {
statusLabel.isVisible = false
}
} catch (e: Exception) {
logger.warning("Error while predicting images $e")
}
Platform.runLater {
progressBar.isVisible = false
}
}
thread.start()

View file

@ -25,6 +25,6 @@
</center>
<bottom>
<Label fx:id="statusLabel" visible="false" text="Predicting... please wait" />
<ProgressBar fx:id="progressBar" maxWidth="Infinity" visible="false"/>
</bottom>
</BorderPane>