-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
152 lines (131 loc) · 5.6 KB
/
manager.py
File metadata and controls
152 lines (131 loc) · 5.6 KB
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
import json
import os
from pathlib import Path
class ConfigManager:
def __init__(self, config_path):
self.config_path = config_path
self.cameraID = 0
self.cameraURL = ""
self.cameraResolutionWidth = 1920
self.cameraResolutionHeight = 1080
self.cameraFPS = 30
self.cameraBrightness = -1.0
self.cameraContrast = -1.0
self.cameraSaturation = -1.0
self.cameraHue = -1.0
self.cameraGain = -1.0
self.cameraExposure = -1.0
self.exportFolder = Path.home() / "Videos"
self.exportFilename = "%Y-%m-%d_%H-%M-%S"
self.exportFormat = "mp4"
self.exportCodec = "libx264"
self.exportBitrate = "2500k"
self.exportFPS = 30
def save(self):
config = {
"camera": {
"camera_id": self.cameraID,
"camera_url": self.cameraURL,
"resolution": {
"width": self.cameraResolutionWidth,
"height": self.cameraResolutionHeight
},
"fps": self.cameraFPS,
"brightness": self.cameraBrightness,
"contrast": self.cameraContrast,
"saturation": self.cameraSaturation,
"hue": self.cameraHue,
"gain": self.cameraGain,
"exposure": self.cameraExposure
},
"export": {
"folder": str(self.exportFolder),
"filename": self.exportFilename,
"format": self.exportFormat,
"codec": self.exportCodec,
"bitrate": self.exportBitrate,
"fps": self.exportFPS
}
}
with open(self.config_path, 'w') as json_file:
json.dump(config, json_file, indent=4)
def load(self):
if not os.path.exists(self.config_path):
print(f"Config file {self.config_path} not found.")
return
with open(self.config_path, 'r') as json_file:
config = json.load(json_file)
# Camera settings
self.cameraID = config["camera"]["camera_id"]
self.cameraURL = config["camera"]["camera_url"]
self.cameraResolution = (config["camera"]["resolution"]["width"], config["camera"]["resolution"]["height"])
self.cameraFPS = config["camera"]["fps"]
self.cameraBrightness = config["camera"]["brightness"]
self.cameraContrast = config["camera"]["contrast"]
self.cameraSaturation = config["camera"]["saturation"]
self.cameraHue = config["camera"]["hue"]
self.cameraGain = config["camera"]["gain"]
self.cameraExposure = config["camera"]["exposure"]
# Export settings
self.exportFolder = config["export"]["folder"]
self.exportFilename = config["export"]["filename"]
self.exportFormat = config["export"]["format"]
self.exportCodec = config["export"]["codec"]
self.exportBitrate = config["export"]["bitrate"]
self.exportFPS = config["export"]["fps"]
class ProjectManager:
def __init__(self, project_name, project_fps, project_width, project_height, project_location):
self.project_name = project_name
self.project_fps = project_fps
self.project_width = project_width
self.project_height = project_height
self.project_location = project_location
self.project_folder = Path(project_location)
self.frames_folder = self.project_folder / "frames" # Define the frames folder
self.current_frame = None
def create_project(self):
# Check if project name is blank
if not self.project_name.strip():
raise NameError("Project name cannot be blank!")
# Check if the folder already exists
if self.project_folder.exists():
raise FileExistsError(f"Folder already exists at {self.project_folder}")
# Create the project folder
self.project_folder.mkdir(parents=True)
self.frames_folder.mkdir(parents=True)
# Save project settings to a JSON file
project_settings = {
"name": self.project_name,
"fps": self.project_fps,
"width": self.project_width,
"height": self.project_height,
}
settings_file = self.project_folder / "project.json"
with open(settings_file, "w") as file:
json.dump(project_settings, file)
@staticmethod
def load_project(project_location):
# Define the project folder and settings file
project_folder = Path(project_location)
settings_file = project_folder / "project.json"
# Check if the settings file exists
if not settings_file.exists():
raise FileNotFoundError(f"Project settings file not found at {settings_file}")
# Load project settings from the JSON file
with open(settings_file, "r") as file:
project_settings = json.load(file)
# Ensure all required fields are present
required_fields = ["name", "fps", "width", "height"]
for field in required_fields:
if field not in project_settings:
raise KeyError(f"Missing required project setting: {field}")
# Create and return a ProjectManager instance
return ProjectManager(
project_name=project_settings["name"],
project_fps=project_settings["fps"],
project_width=project_settings["width"],
project_height=project_settings["height"],
project_location=project_location
)
def list_frames(self):
return sorted(self.frames_folder.glob("*.png"))