Skip to content

Commit

Permalink
some changes...
Browse files Browse the repository at this point in the history
  • Loading branch information
electro199 committed Jun 24, 2024
1 parent 9700c25 commit f1312ab
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ output/
compile.py
__old_code.py
test.py
output*
test.ipynb
Binary file added icons/broken-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 86 additions & 43 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import sys
from typing import Dict, List

from PySide6.QtCore import (
Qt,
QTimer
)
from PySide6.QtCore import Qt#, QTimer
from PySide6.QtGui import QKeySequence, QPixmap, QIcon
from PySide6.QtWidgets import (
QApplication,
Expand All @@ -19,7 +16,7 @@
)

from PySide6.QtGui import QImageReader, QPixmap
from PySide6.QtCore import QDir, Qt
from PySide6.QtCore import Qt
from ui.app_ui import Ui_MainWindow
from ui.custom_input_iu import InputDialog
from glob import glob
Expand All @@ -31,26 +28,32 @@ def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowIcon(QIcon(r"icons\empty-icon copy.jpg"))
self.current_image = ""
self.all_images_path = []
self.setWindowIcon(QIcon("icons/empty-icon copy.jpg"))
self.current_image: str = ""
self.all_images_path: list[str] = []
self.current_image_index = -1
self.label2img: Dict[str, List[str]] = {}
self.labels_count = -1
self._defaults = {}

self.is_dataset_changed = False
self.progress_file_path = ""
self.save_defaults()

self.ui.label_holder.setWidgetResizable(True)

# self.autosave_timer = QTimer(self)
# self.autosave_timer.timeout.connect(self.autosave)
# self.autosave_timer.start(5000)

self.ui.actionopen_folder.triggered.connect(self.open_folder)
self.ui.actionOpen_Labels_file.triggered.connect(self.get_labels)
self.ui.add_Label.pressed.connect(self._add_label_manual)
self.ui.actionSave_project.triggered.connect(self.savedataset)
self.ui.actionOpen_img_csv.triggered.connect(self.open_img_csv)
self.ui.actionsave_dataset.triggered.connect(self.save_progress_file)
self.ui.actionload_from_progress_file.triggered.connect(self.load_from_progress_file)

self.ui.actionload_from_progress_file.triggered.connect(
self.load_from_progress_file
)

def _add_label_manual(self) -> None:
self._add_label(self.labels_count + 1, self.open_input_dialog())
Expand All @@ -73,6 +76,10 @@ def open_folder(self) -> None:

def open_img_csv(self):
file_path, _ = QFileDialog.getOpenFileUrl(self, "Select image csv", ".")

if not file_path:
return

with open(str(file_path.toLocalFile()), encoding="utf-8") as f:

self.all_images_path = [i["path"] for i in csv.DictReader(f)]
Expand All @@ -97,14 +104,19 @@ def show_next_image(self) -> None:
self.current_image = self.all_images_path[self.current_image_index + 1]
self.ui.current_image_path_label.setText(self.current_image)
image_reader = QImageReader(self.current_image)

print(self.current_image)

pixmap = QPixmap.fromImageReader(image_reader)
if not pixmap.isNull():
self.display_image(pixmap)
self.current_image_index += 1

if pixmap.isNull():
print("found broken image", self.current_image)
pixmap = QPixmap.fromImageReader(QImageReader("icons/broken-image.png"))
return self.display_image(pixmap)

self.display_image(pixmap)

def display_image(self, pixmap: QPixmap) -> None:
self.ui.image_area.setPixmap(
pixmap.scaled(
Expand Down Expand Up @@ -151,6 +163,7 @@ def on_button_clicked(self):
print(f"Button with label '{label}' clicked!")
# TODO do somthing...
self.label2img[label].append(self.current_image)
self.is_dataset_changed = True

self.show_next_image()

Expand All @@ -163,6 +176,25 @@ def resizeEvent(self, event):
super().resizeEvent(event)

def savedataset(self):
"Open dialog to get file info and extension is the imtag"

self.save_defaults()

file_path, _ = QFileDialog.getSaveFileName(
self, caption="Save Dataset", filter="*.csv"
)

if not file_path: # Check if a file path was selected
print("empty file path detected for savedataset NOT SAVING ")
return
# Ensure the file has the correct extension
if not file_path.endswith(".csv"):
file_path += ".csv"

print(file_path)

self.progress_file_path = file_path

with open("dataset.csv", "w", encoding="utf-8") as f:
w = csv.DictWriter(f, {"label", "path"}, lineterminator="\n")
w.writeheader()
Expand All @@ -173,37 +205,45 @@ def savedataset(self):
w.writerows(b)

def save_defaults(self):
self._defaults["current_image "] = self.current_image
self._defaults["all_images_path "] = self.all_images_path
self._defaults["current_image_index "] = self.current_image_index
self._defaults["label2img "] = self.label2img
self._defaults["labels_count "] = self.labels_count
self._defaults["current_image"] = self.current_image
self._defaults["all_images_path"] = self.all_images_path
self._defaults["current_image_index"] = self.current_image_index
self._defaults["label2img"] = self.label2img
self._defaults["labels_count"] = self.labels_count

def to_defaults(self):
self.current_image = self._defaults["current_image "]
self.all_images_path = self._defaults["all_images_path "]
self.current_image_index = self._defaults["current_image_index "]
self.label2img = self._defaults["label2img "]
self.labels_count = self._defaults["labels_count "]
self.current_image = self._defaults["current_image"]
self.all_images_path = self._defaults["all_images_path"]
self.current_image_index = self._defaults["current_image_index"]
self.label2img = self._defaults["label2img"]
self.labels_count = self._defaults["labels_count"]

def save_progress_file(self) -> None:
self.progress_file_path, _ = QFileDialog.getSaveFileName(
self, caption="Save Progress File", dir="/", filter="CSV files (*.imatag)"
)

if not self.progress_file_path:
print("empty file path detected for save progress NOT SAVING ")
return

def save_progress_file(self):
app_state = {
"current_image" : self.current_image,
"all_images_path" : self.all_images_path,
"current_image_index" : self.current_image_index,
"label2img" : self.label2img,
"labels_count" : self.labels_count,
}
with open("op.imtag", "w", -1, "utf-8") as f:
"current_image": self.current_image,
"all_images_path": self.all_images_path,
"current_image_index": self.current_image_index,
"label2img": self.label2img,
"labels_count": self.labels_count,
}
with open(self.progress_file_path, "w", -1, "utf-8") as f:
json.dump(app_state, f, indent=2)

def load_from_progress_file(self):
dir ,_= QFileDialog.getOpenFileUrl(self)

if not dir.toLocalFile():
return
with open(dir.toLocalFile(), "r", -1, "utf-8") as f:
dir, _ = QFileDialog.getOpenFileUrl(self, filter="IMTAG files (*.imtag)")
self.progress_file_path = dir.toLocalFile()
if not self.progress_file_path:
return

with open(self.progress_file_path, "r", -1, "utf-8") as f:
app_state = json.load(f)

self.current_image = app_state["current_image"]
Expand All @@ -213,13 +253,16 @@ def load_from_progress_file(self):
self.add_labels(app_state["label2img"].keys())
self.label2img = app_state["label2img"]
self.labels_count = app_state["labels_count"]

self.display_image(QPixmap.fromImageReader(QImageReader(self.current_image)))

def auto_save(self):
self.autosave_timer = QTimer(self)
self.autosave_timer.timeout.connect(self.autosave)
self.autosave_timer.start(5000)
# def autosave(self):
# if not self.is_dataset_changed:
# return

# def on_closing(self):
# pass


if __name__ == "__main__":
app = QApplication(sys.argv)
Expand Down

0 comments on commit f1312ab

Please sign in to comment.