-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_utils.py
More file actions
64 lines (46 loc) · 1.64 KB
/
Copy pathplatform_utils.py
File metadata and controls
64 lines (46 loc) · 1.64 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
"""Cross-platform helpers for paths and camera capture."""
from __future__ import annotations
import sys
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import cv2
def default_captures_save_path() -> str:
return str(Path.home() / "Pictures" / "Camera_Captures")
def default_records_save_path() -> str:
return str(Path.home() / "Pictures" / "Camera_Records")
def resolve_settings_path(configured: str | None, fallback: str) -> str:
if configured and str(configured).strip():
return str(configured)
return fallback
def pick_directory(initial_path: str | None = None) -> str | None:
"""Open a native folder picker dialog. Returns None if cancelled."""
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
root.attributes("-topmost", True)
try:
kwargs: dict[str, str] = {}
if initial_path and str(initial_path).strip():
candidate = Path(str(initial_path).strip().rstrip("/\\"))
if candidate.is_dir():
kwargs["initialdir"] = str(candidate)
selected = filedialog.askdirectory(**kwargs)
return selected or None
finally:
root.destroy()
def opencv_capture_backend() -> int:
import cv2
if sys.platform == "win32":
return cv2.CAP_DSHOW
if sys.platform == "darwin":
return cv2.CAP_AVFOUNDATION
return cv2.CAP_V4L2
def open_video_capture(index: int) -> cv2.VideoCapture:
import cv2
cap = cv2.VideoCapture(index, opencv_capture_backend())
if not cap.isOpened():
cap.release()
cap = cv2.VideoCapture(index)
return cap