Skip to content

Commit

Permalink
Custom Export Path Supported
Browse files Browse the repository at this point in the history
all 3 modes supported,
COCO and MOT format for videos
  • Loading branch information
0ssamaak0 committed May 1, 2023
1 parent a970db0 commit 4c9b806
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
27 changes: 12 additions & 15 deletions labelme-master/labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3132,16 +3132,6 @@ def saveFile(self, _value=False):
self._saveFile(self.save_path)

def exportData(self):
# # Create a QFileDialog object
# FolderDialog = QFileDialog()
# # Set the mode to save a file
# FolderDialog.setAcceptMode(QFileDialog.AcceptSave)
# # Set the default file name
# FolderDialog.selectFile("coco.json")
# # set the default format
# FolderDialog.selectNameFilter("JSON (*.json)")
# # set dialog title
# FolderDialog.setWindowTitle("Save Annotations")
try:
if self.current_annotation_mode == "video":
dialog = QtWidgets.QDialog()
Expand Down Expand Up @@ -3187,14 +3177,21 @@ def exportData(self):
pth = self.CURRENT_VIDEO_PATH
# Check which radio button is checked and export accordingly
if coco_radio.isChecked():
pth = utils.exportCOCOvid(
json_file_name, target_path, self.CURRENT_VIDEO_WIDTH, self.CURRENT_VIDEO_HEIGHT, output_name="coco_vid")
folderDialog = utils.FolderDialog("coco.json", "json")
if folderDialog.exec_():
pth = utils.exportCOCOvid(
json_file_name, self.CURRENT_VIDEO_WIDTH, self.CURRENT_VIDEO_HEIGHT, folderDialog.selectedFiles()[0])
if mot_radio.isChecked():
pth = utils.exportMOT(json_file_name, target_path,
output_name="mot_vid")
folderDialog = utils.FolderDialog("mot.txt", "txt")
if folderDialog.exec_():
pth = utils.exportMOT(
json_file_name, folderDialog.selectedFiles()[0])

elif self.current_annotation_mode == "img" or self.current_annotation_mode == "dir":
pth = utils.exportCOCO(self.target_directory, self.save_path)
folderDialog = utils.FolderDialog("coco.json", "json")
if folderDialog.exec_():
pth = utils.exportCOCO(
self.target_directory, self.save_path, folderDialog.selectedFiles()[0])
except Exception as e:
# Error QMessageBox
msg = QtWidgets.QMessageBox()
Expand Down
1 change: 1 addition & 0 deletions labelme-master/labelme/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
from .export import exportCOCO
from .export import exportCOCOvid
from .export import exportMOT
from .export import FolderDialog

from .more_models import ModelExplorerDialog
50 changes: 29 additions & 21 deletions labelme-master/labelme/utils/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
import numpy as np
from PyQt5.QtWidgets import QFileDialog


def get_bbox(segmentation):
Expand Down Expand Up @@ -50,7 +51,7 @@ def get_area_from_polygon(polygon, mode="segmentation"):
raise ValueError("mode must be either 'segmentation' or 'bbox'")


def exportCOCO(target_directory, save_path):
def exportCOCO(target_directory, save_path, annotation_path):
'''
Exports the annotations in the Labelmm format to COCO format
input:
Expand Down Expand Up @@ -137,18 +138,14 @@ def exportCOCO(target_directory, save_path):
file["images"] = images
file["annotations"] = annotations

# make a directory under file_path called Annotations (if it doesn't exist)
if not os.path.exists(f"{file_path}/Annotations"):
os.makedirs(f"{file_path}/Annotations")

# write in the output file in json, format the output to be pretty
with open(f"{file_path}/Annotations/{output_name}.json", 'w') as outfile:
with open(annotation_path, 'w') as outfile:
json.dump(file, outfile, indent=4)

return (f"{file_path}/Annotations")
return (annotation_path)


def exportCOCOvid(results_file, save_path, vid_width, vid_height, output_name="coco_vid"):
def exportCOCOvid(results_file, vid_width, vid_height, annotation_path):
file = {}
file["info"] = {
"description": "Exported from Labelmm",
Expand Down Expand Up @@ -208,18 +205,14 @@ def exportCOCOvid(results_file, save_path, vid_width, vid_height, output_name="c
file["images"] = images
file["annotations"] = annotations

# make a directory under save_path called Annotations (if it doesn't exist)
if not os.path.exists(f"{save_path}/Annotations"):
os.makedirs(f"{save_path}/Annotations")

# write in the output file in json, format the output to be pretty
with open(f"{save_path}/Annotations/{output_name}.json", 'w') as outfile:
with open(annotation_path, 'w') as outfile:
json.dump(file, outfile, indent=4)

return (f"{save_path}/Annotations/")
return (annotation_path)


def exportMOT(results_file, save_path, output_name="mot_vid"):
def exportMOT(results_file, annotation_path):
rows = []
with open(results_file) as f:
data = json.load(f)
Expand All @@ -229,12 +222,27 @@ def exportMOT(results_file, save_path, output_name="mot_vid"):
rows.append(
f'{frame["frame_idx"]}, {object["tracker_id"]}, {object["bbox"][0]}, {object["bbox"][1]}, {object["bbox"][2]}, {object["bbox"][3]}, {object["confidence"]}, {object["class_id"] + 1}, 1')

# save rows in a file in save_path, file name is output_name, with .txt extension
if not os.path.exists(f"{save_path}/Annotations"):
os.makedirs(f"{save_path}/Annotations")

with open(f"{save_path}/Annotations/{output_name}.txt", 'w') as outfile:
with open(annotation_path, 'w') as outfile:
# write each row in the file as a new line
outfile.write("\n".join(rows))

return (f"{save_path}/Annotations/")
return (annotation_path)


# Define a class that inherits from QFileDialog
class FolderDialog(QFileDialog):
# Override the __init__ method to accept parameters
def __init__(self, default_file_name, default_format):
# Call the parent constructor
super().__init__()
# Set the mode to save a file
self.setAcceptMode(QFileDialog.AcceptSave)
# Set the default file name
self.selectFile(default_file_name)
# Set the default format
self.setNameFilters(
[f"{default_format.upper()} (*.{default_format.lower()})", "All Files (*)"])
self.selectNameFilter(
f"{default_format.upper()} (*.{default_format.lower()})")
# Set dialog title
self.setWindowTitle("Save Annotations")

0 comments on commit 4c9b806

Please sign in to comment.