frameforge-py 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- frameforge_py-0.1.0/PKG-INFO +12 -0
- frameforge_py-0.1.0/README.md +0 -0
- frameforge_py-0.1.0/pyproject.toml +56 -0
- frameforge_py-0.1.0/src/frameforge/__init__.py +2 -0
- frameforge_py-0.1.0/src/frameforge/app.py +15 -0
- frameforge_py-0.1.0/src/frameforge/cli/cli.py +66 -0
- frameforge_py-0.1.0/src/frameforge/commands/__init__.py +9 -0
- frameforge_py-0.1.0/src/frameforge/commands/image_cmd_set/__init__.py +10 -0
- frameforge_py-0.1.0/src/frameforge/commands/image_cmd_set/image_genvideo.py +76 -0
- frameforge_py-0.1.0/src/frameforge/commands/image_cmd_set/image_process.py +78 -0
- frameforge_py-0.1.0/src/frameforge/commands/video_cmd_set/__init__.py +10 -0
- frameforge_py-0.1.0/src/frameforge/commands/video_cmd_set/video2frames.py +73 -0
- frameforge_py-0.1.0/src/frameforge/context/__init__.py +0 -0
- frameforge_py-0.1.0/src/frameforge/context/context.py +7 -0
- frameforge_py-0.1.0/src/frameforge/handlers/image_handlers/__init__.py +0 -0
- frameforge_py-0.1.0/src/frameforge/handlers/image_handlers/handle_genvideo.py +7 -0
- frameforge_py-0.1.0/src/frameforge/handlers/image_handlers/handle_process.py +66 -0
- frameforge_py-0.1.0/src/frameforge/handlers/image_handlers/utils.py +37 -0
- frameforge_py-0.1.0/src/frameforge/handlers/video_handlers/__init__.py +0 -0
- frameforge_py-0.1.0/src/frameforge/handlers/video_handlers/utils.py +53 -0
- frameforge_py-0.1.0/src/frameforge/py.typed +0 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: frameforge-py
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add your description here
|
|
5
|
+
Author: daiyuxiang
|
|
6
|
+
Author-email: daiyuxiang <1477127478@qq.com>
|
|
7
|
+
Requires-Dist: cmd2>=3.5.1
|
|
8
|
+
Requires-Dist: numpy>=2.2.6
|
|
9
|
+
Requires-Dist: opencv-python>=5.0.0.93
|
|
10
|
+
Requires-Python: ==3.10.12
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
File without changes
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "frameforge-py"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Add your description here"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "daiyuxiang", email = "1477127478@qq.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = "==3.10.12"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"cmd2>=3.5.1",
|
|
12
|
+
"numpy>=2.2.6",
|
|
13
|
+
"opencv-python>=5.0.0.93",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
# ─────────────── 镜像源 ───────────────
|
|
17
|
+
[tool.uv]
|
|
18
|
+
python-install-mirror = "https://registry.npmmirror.com/-/binary/python-build-standalone/"
|
|
19
|
+
|
|
20
|
+
[[tool.uv.index]]
|
|
21
|
+
name = "tsinghua"
|
|
22
|
+
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
|
|
23
|
+
default = true
|
|
24
|
+
|
|
25
|
+
# ─────────────── 构建系统 ───────────────
|
|
26
|
+
[build-system]
|
|
27
|
+
requires = ["uv_build>=0.11.24,<0.12.0"]
|
|
28
|
+
build-backend = "uv_build"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
[tool.uv.build-backend]
|
|
32
|
+
module-root = "src"
|
|
33
|
+
module-name = "frameforge"
|
|
34
|
+
|
|
35
|
+
[project.scripts]
|
|
36
|
+
frameforge-cli = "frameforge.app:main"
|
|
37
|
+
|
|
38
|
+
# ─────────────── 发布 ───────────────
|
|
39
|
+
# Linux : uv publish --index testpypi --token $TEST_PYPI_TOKEN
|
|
40
|
+
# Win : uv publish --index testpypi --token $env:TEST_PYPI_TOKEN
|
|
41
|
+
# 验证安装(任选一种):
|
|
42
|
+
# uv add --index testpypi <your-package-name>
|
|
43
|
+
# uv tool install --index testpypi=https://test.pypi.org/simple/ <your-package-name>
|
|
44
|
+
[[tool.uv.index]]
|
|
45
|
+
name = "testpypi"
|
|
46
|
+
url = "https://test.pypi.org/simple/"
|
|
47
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
48
|
+
explicit = true
|
|
49
|
+
|
|
50
|
+
# Linux : uv publish --index pypi --token $PYPI_TOKEN
|
|
51
|
+
# Win : uv publish --index pypi --token $env:PYPI_TOKEN
|
|
52
|
+
[[tool.uv.index]]
|
|
53
|
+
name = "pypi"
|
|
54
|
+
url = "https://pypi.org/simple/"
|
|
55
|
+
publish-url = "https://upload.pypi.org/legacy/"
|
|
56
|
+
explicit = true
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# PYTHON_ARGCOMPLETE_OK
|
|
3
|
+
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from .cli.cli import FrameForgeShell
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main():
|
|
10
|
+
# if enconuter error, sys don't exit TOOL
|
|
11
|
+
sys.exit(FrameForgeShell().cmdloop())
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
if __name__ == "__main__":
|
|
15
|
+
main()
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# PYTHON_ARGCOMPLETE_OK
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import cmd2
|
|
8
|
+
from cmd2 import with_argparser, with_category
|
|
9
|
+
import argparse
|
|
10
|
+
|
|
11
|
+
from ..context.context import FrameForgeContext
|
|
12
|
+
|
|
13
|
+
from ..commands.image_cmd_set.image_process import ImageProcessCommand
|
|
14
|
+
from ..commands.image_cmd_set.image_genvideo import ImageGenvideoCommand
|
|
15
|
+
|
|
16
|
+
from ..commands.video_cmd_set.video2frames import ImageVideo2FramesCommand
|
|
17
|
+
|
|
18
|
+
class FrameForgeShell(cmd2.Cmd):
|
|
19
|
+
|
|
20
|
+
def __init__(self):
|
|
21
|
+
# Define global context
|
|
22
|
+
self.context = FrameForgeContext()
|
|
23
|
+
|
|
24
|
+
# add a persistent cmd2 command history
|
|
25
|
+
tool_workdir = os.path.join(str(Path.home()), ".frameforge")
|
|
26
|
+
os.makedirs(tool_workdir, exist_ok=True, mode=0o755)
|
|
27
|
+
super().__init__(
|
|
28
|
+
persistent_history_file=os.path.join(tool_workdir, "history"),
|
|
29
|
+
persistent_history_length=100,
|
|
30
|
+
suggest_similar_command=True,
|
|
31
|
+
auto_load_commands=False,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# register command
|
|
35
|
+
self.register_command_set(ImageProcessCommand(self.context))
|
|
36
|
+
self.register_command_set(ImageGenvideoCommand(self.context))
|
|
37
|
+
self.register_command_set(ImageVideo2FramesCommand(self.context))
|
|
38
|
+
|
|
39
|
+
self.intro = "Welcome to FrameForge. Type help or ? to list commands.\n"
|
|
40
|
+
self.prompt = "(FrameForge) "
|
|
41
|
+
|
|
42
|
+
# ------------------------ image 主命令 ------------------------
|
|
43
|
+
image_parser = cmd2.Cmd2ArgumentParser(description="Image Command Set")
|
|
44
|
+
image_parser.add_subparsers(dest="image_subcommand")
|
|
45
|
+
|
|
46
|
+
@with_category("FrameForge")
|
|
47
|
+
@with_argparser(image_parser)
|
|
48
|
+
def do_image(self, ns: argparse.Namespace):
|
|
49
|
+
"""Image Command Set"""
|
|
50
|
+
|
|
51
|
+
handler = ns.cmd2_handler.get()
|
|
52
|
+
if handler is not None:
|
|
53
|
+
handler(ns)
|
|
54
|
+
|
|
55
|
+
# ------------------------ video 主命令 ------------------------
|
|
56
|
+
video_parser = cmd2.Cmd2ArgumentParser(description="Video Command Set")
|
|
57
|
+
video_parser.add_subparsers(dest="video_subcommand")
|
|
58
|
+
|
|
59
|
+
@with_category("FrameForge")
|
|
60
|
+
@with_argparser(video_parser)
|
|
61
|
+
def do_video(self, ns: argparse.Namespace):
|
|
62
|
+
"""Video Command Set"""
|
|
63
|
+
|
|
64
|
+
handler = ns.cmd2_handler.get()
|
|
65
|
+
if handler is not None:
|
|
66
|
+
handler(ns)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import cmd2
|
|
2
|
+
from argparse import Namespace
|
|
3
|
+
import cv2
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from ...context.context import FrameForgeContext
|
|
7
|
+
|
|
8
|
+
from ...handlers.image_handlers.utils import *
|
|
9
|
+
from ...handlers.image_handlers.handle_genvideo import *
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ImageGenvideoCommand(cmd2.CommandSet):
|
|
13
|
+
|
|
14
|
+
def __init__(self, context: FrameForgeContext):
|
|
15
|
+
super().__init__()
|
|
16
|
+
self.context = context
|
|
17
|
+
|
|
18
|
+
# ------------------------ gen-video 子命令 ------------------------
|
|
19
|
+
# 子命令解析器(image2video)
|
|
20
|
+
i2v_parser = cmd2.Cmd2ArgumentParser(description="图像合成视频")
|
|
21
|
+
i2v_parser.add_argument("img", nargs='+',
|
|
22
|
+
completer=cmd2.Cmd.path_complete,
|
|
23
|
+
help="Image Path: FileName / glob format")
|
|
24
|
+
i2v_parser.add_argument("-fps", nargs=1, type=int, metavar='FPS',
|
|
25
|
+
help="FPS of video, default fps = 10")
|
|
26
|
+
i2v_parser.add_argument('-f', '--video_format', choices=['mp4', 'avi'],
|
|
27
|
+
help="Format of video, defalut format = .mp4")
|
|
28
|
+
i2v_parser.add_argument('--gray', action='store_true',
|
|
29
|
+
help="Saved video is gray, default is Color RGB")
|
|
30
|
+
i2v_parser.add_argument("--name", default="video",
|
|
31
|
+
help="Video name, default is video.mp4/video.avi")
|
|
32
|
+
|
|
33
|
+
# gen-video 子命令注入
|
|
34
|
+
@cmd2.as_subcommand_to("image", "gen-video", i2v_parser, help="gen video from images")
|
|
35
|
+
def handle_gen_video(self, args: Namespace):
|
|
36
|
+
"""处理图像子命令"""
|
|
37
|
+
self.gen_video(args)
|
|
38
|
+
|
|
39
|
+
# ------------------------ 子命令处理函数 ------------------------
|
|
40
|
+
def gen_video(self, args: Namespace):
|
|
41
|
+
|
|
42
|
+
self.context.imgList = load_image(args.img)
|
|
43
|
+
if self.context.imgList == []:
|
|
44
|
+
self._cmd.poutput("\nNo image is loaded, please load Image files first!\n")
|
|
45
|
+
return
|
|
46
|
+
|
|
47
|
+
imgList = self.context.imgList
|
|
48
|
+
|
|
49
|
+
# default value definition
|
|
50
|
+
save_video = args.name + ".mp4"
|
|
51
|
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # .avi -> 'XVID'
|
|
52
|
+
fps = 10
|
|
53
|
+
image_size = cv2.imread(imgList[0]).shape[1::-1] # (W, H)
|
|
54
|
+
isColor = True
|
|
55
|
+
|
|
56
|
+
# sort self.imgList by BaseName of imageFiles
|
|
57
|
+
imgList.sort(key=lambda filePath: int(os.path.basename(filePath).split(".")[0]))
|
|
58
|
+
self._cmd.poutput("Sorted Image List = ", imgList)
|
|
59
|
+
|
|
60
|
+
# gen video
|
|
61
|
+
if args.video_format == "avi":
|
|
62
|
+
fourcc = cv2.VideoWriter_fourcc(*"XVID") # .avi -> 'XVID'
|
|
63
|
+
save_video = args.name + ".avi"
|
|
64
|
+
|
|
65
|
+
if args.fps:
|
|
66
|
+
fps = args.fps[0]
|
|
67
|
+
|
|
68
|
+
if args.gray:
|
|
69
|
+
isColor = False
|
|
70
|
+
|
|
71
|
+
videowriter = cv2.VideoWriter(save_video, fourcc, fps, image_size, isColor)
|
|
72
|
+
for image in imgList:
|
|
73
|
+
img = cv2.imread(image)
|
|
74
|
+
videowriter.write(img)
|
|
75
|
+
|
|
76
|
+
videowriter.release()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import cmd2
|
|
2
|
+
from argparse import Namespace
|
|
3
|
+
import cv2
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from ...context.context import FrameForgeContext
|
|
7
|
+
|
|
8
|
+
from ...handlers.image_handlers.utils import *
|
|
9
|
+
from ...handlers.image_handlers.handle_process import *
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ImageProcessCommand(cmd2.CommandSet):
|
|
13
|
+
|
|
14
|
+
def __init__(self, context: FrameForgeContext):
|
|
15
|
+
super().__init__()
|
|
16
|
+
self.context = context
|
|
17
|
+
|
|
18
|
+
# ------------------------ process 子命令 ------------------------
|
|
19
|
+
# 子命令解析器
|
|
20
|
+
process_parser = cmd2.Cmd2ArgumentParser(description="图像处理")
|
|
21
|
+
process_parser.add_argument("img", nargs='+',
|
|
22
|
+
help="Image Path: FileName / glob format",
|
|
23
|
+
completer=cmd2.Cmd.path_complete)
|
|
24
|
+
process_parser.add_argument('--gray', action="store_true",
|
|
25
|
+
help="Convert image to single channel")
|
|
26
|
+
process_parser.add_argument('--resize', nargs=2, type=int, metavar=("Width", "Height"),
|
|
27
|
+
help="Image size for resizing image")
|
|
28
|
+
process_parser.add_argument('--crop', nargs=2, type=int, metavar=("Width", "Height"),
|
|
29
|
+
help="Image size for croping image")
|
|
30
|
+
process_parser.add_argument('-s', '--save_format', choices=['png', 'jpg', 'ppm', 'pgm', 'bmp', 'npy'],
|
|
31
|
+
help="Saved format of image file")
|
|
32
|
+
process_parser.add_argument('--chw', action='store_true',
|
|
33
|
+
help="chw layout for image, only valid for '.npy' format")
|
|
34
|
+
process_parser.add_argument('-o', '--output-dir', type=str, default='./FF_Output', metavar='DIR',
|
|
35
|
+
help='输出目录路径,不存在时自动创建(默认: ./FF_Output)')
|
|
36
|
+
|
|
37
|
+
# process 子命令注入
|
|
38
|
+
@cmd2.as_subcommand_to("image", "process", process_parser, help="image process: resize, crop, gray, save-formt")
|
|
39
|
+
def handle_process(self, args: Namespace):
|
|
40
|
+
"""处理图像子命令"""
|
|
41
|
+
self.process(args)
|
|
42
|
+
|
|
43
|
+
# ------------------------ 子命令处理函数 ------------------------
|
|
44
|
+
def process(self, args: Namespace):
|
|
45
|
+
|
|
46
|
+
self.context.imgList = load_image(args)
|
|
47
|
+
if self.context.imgList == []:
|
|
48
|
+
self._cmd.poutput("\nNo image is loaded, please load Image files first!\n")
|
|
49
|
+
return
|
|
50
|
+
else:
|
|
51
|
+
self._cmd.poutput("Image List = ", self.context.imgList)
|
|
52
|
+
|
|
53
|
+
dest_folder_name = args.output_dir
|
|
54
|
+
|
|
55
|
+
if not os.path.exists(dest_folder_name):
|
|
56
|
+
os.mkdir(dest_folder_name)
|
|
57
|
+
|
|
58
|
+
for imageFilename in self.context.imgList:
|
|
59
|
+
# open Image
|
|
60
|
+
image = cv2.imread(imageFilename)
|
|
61
|
+
|
|
62
|
+
basename = os.path.basename(imageFilename)
|
|
63
|
+
img_prefix, img_ext = os.path.splitext(basename)
|
|
64
|
+
|
|
65
|
+
if args.gray:
|
|
66
|
+
image = color2gray(image)
|
|
67
|
+
|
|
68
|
+
if args.resize:
|
|
69
|
+
image = resize(image, args)
|
|
70
|
+
|
|
71
|
+
if args.crop:
|
|
72
|
+
image = crop(image, args)
|
|
73
|
+
|
|
74
|
+
if args.save_format:
|
|
75
|
+
img_ext = args.save_format
|
|
76
|
+
|
|
77
|
+
save_path = os.path.join(dest_folder_name, f"{img_prefix}.{img_ext}")
|
|
78
|
+
save_image(image, save_path, args)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import cmd2
|
|
2
|
+
from argparse import Namespace
|
|
3
|
+
import cv2
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from ...context.context import FrameForgeContext
|
|
7
|
+
|
|
8
|
+
from ...handlers.video_handlers.utils import *
|
|
9
|
+
|
|
10
|
+
class ImageVideo2FramesCommand(cmd2.CommandSet):
|
|
11
|
+
|
|
12
|
+
def __init__(self, context: FrameForgeContext):
|
|
13
|
+
super().__init__()
|
|
14
|
+
self.context = context
|
|
15
|
+
|
|
16
|
+
# ------------------------ video2frames 子命令 ------------------------
|
|
17
|
+
# 子命令解析器
|
|
18
|
+
v2f_parser = cmd2.Cmd2ArgumentParser(description="处理video")
|
|
19
|
+
v2f_parser.add_argument("video", nargs='+',
|
|
20
|
+
completer=cmd2.Cmd.path_complete,
|
|
21
|
+
help="Video Path: FileName / glob format")
|
|
22
|
+
v2f_parser.add_argument('--step', nargs='?', type=int, default=1, metavar="STEP",
|
|
23
|
+
help="step of extracting frames, default=1")
|
|
24
|
+
v2f_parser.add_argument('-s', '--save_format_image',
|
|
25
|
+
choices=['jpg', 'ppm', 'png', 'bmp'],
|
|
26
|
+
default='png',
|
|
27
|
+
help="Saved format of image file, default=png")
|
|
28
|
+
|
|
29
|
+
# video2frames 子命令注入
|
|
30
|
+
@cmd2.as_subcommand_to("video", "video2frames", v2f_parser, help="xxx")
|
|
31
|
+
def handle_video2frames(self, args: Namespace):
|
|
32
|
+
"""处理图像子命令"""
|
|
33
|
+
self.video2frames(args)
|
|
34
|
+
|
|
35
|
+
# ------------------------ 子命令处理函数 ------------------------
|
|
36
|
+
def video2frames(self, args: Namespace):
|
|
37
|
+
|
|
38
|
+
self.context.videoList = load_video(args)
|
|
39
|
+
if self.context.videoList == []:
|
|
40
|
+
self._cmd.poutput("\nNo video is loaded, please load video files first!\n")
|
|
41
|
+
return
|
|
42
|
+
else:
|
|
43
|
+
self._cmd.poutput("Video List = ", self.context.videoList)
|
|
44
|
+
|
|
45
|
+
# default value definition
|
|
46
|
+
imageSuffix = args.save_format_image
|
|
47
|
+
step = args.step
|
|
48
|
+
|
|
49
|
+
# process video
|
|
50
|
+
videoList = self.context.videoList
|
|
51
|
+
for video in videoList:
|
|
52
|
+
videoName = os.path.split(video)[1].split(".")[0] # get videoName without suffix
|
|
53
|
+
# print(videoName)
|
|
54
|
+
|
|
55
|
+
if not os.path.exists(videoName):
|
|
56
|
+
os.mkdir(videoName)
|
|
57
|
+
|
|
58
|
+
videoCapture = cv2.VideoCapture()
|
|
59
|
+
videoCapture.open(video)
|
|
60
|
+
fps = videoCapture.get(cv2.CAP_PROP_FPS) # get fps
|
|
61
|
+
# get total number of frames
|
|
62
|
+
frame_count = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
|
|
63
|
+
|
|
64
|
+
idx = 1
|
|
65
|
+
for i in range(int(frame_count)): # read & save frame
|
|
66
|
+
ret, frame = videoCapture.read()
|
|
67
|
+
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
68
|
+
if i % step == 0:
|
|
69
|
+
imageName = videoName + '/' + str(idx) + '.' + imageSuffix
|
|
70
|
+
cv2.imwrite(imageName, frame)
|
|
71
|
+
idx += 1
|
|
72
|
+
|
|
73
|
+
videoCapture.release()
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# 明确指定允许被 import * 导出的函数
|
|
2
|
+
__all__ = ["color2gray", "resize", "crop", "save_image"]
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
import cv2
|
|
6
|
+
import numpy as np
|
|
7
|
+
import glob
|
|
8
|
+
from argparse import Namespace
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def color2gray(image: np.ndarray) -> np.ndarray:
|
|
12
|
+
channel = image.shape[2] if image.ndim == 3 else 1
|
|
13
|
+
|
|
14
|
+
if channel == 3:
|
|
15
|
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
16
|
+
elif channel == 4:
|
|
17
|
+
return cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
|
|
18
|
+
else:
|
|
19
|
+
return image
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def resize(image: np.ndarray, args: Namespace) -> np.ndarray:
|
|
23
|
+
RW = args.resize[0]
|
|
24
|
+
RH = args.resize[1]
|
|
25
|
+
return cv2.resize(image, (RW, RH))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def crop(image: np.ndarray, args: Namespace) -> np.ndarray:
|
|
29
|
+
|
|
30
|
+
# 原图像尺寸
|
|
31
|
+
h, w = image.shape[:2]
|
|
32
|
+
|
|
33
|
+
crop_w = args.crop[0]
|
|
34
|
+
crop_h = args.crop[1]
|
|
35
|
+
|
|
36
|
+
# 边界保护:如果裁剪尺寸大于原图,则自动缩小到原图尺寸
|
|
37
|
+
crop_w = min(crop_w, w)
|
|
38
|
+
crop_h = min(crop_h, h)
|
|
39
|
+
|
|
40
|
+
# 计算居中裁剪的坐标
|
|
41
|
+
x0 = (w - crop_w) // 2
|
|
42
|
+
y0 = (h - crop_h) // 2
|
|
43
|
+
x1 = x0 + crop_w
|
|
44
|
+
y1 = y0 + crop_h
|
|
45
|
+
|
|
46
|
+
# (left, upper, right, lower)
|
|
47
|
+
cropped_image = image[y0:y1, x0:x1]
|
|
48
|
+
|
|
49
|
+
return cropped_image
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def save_image(image: np.ndarray, save_path: str, args: Namespace) -> None:
|
|
53
|
+
"""
|
|
54
|
+
统一处理图像保存逻辑,支持常规格式与 npy 格式。
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
# 根据格式执行不同的保存逻辑
|
|
58
|
+
if args.save_format == "npy":
|
|
59
|
+
arr = image.astype(np.uint8)
|
|
60
|
+
if args.chw and arr.ndim == 3:
|
|
61
|
+
arr = arr.transpose(2, 0, 1) # HWC to CHW
|
|
62
|
+
np.save(save_path, arr)
|
|
63
|
+
else:
|
|
64
|
+
success = cv2.imwrite(save_path, image)
|
|
65
|
+
if not success:
|
|
66
|
+
raise IOError(f"OpenCV 无法保存图像,请检查路径或格式是否支持: {save_path}")
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# 明确指定允许被 import * 导出的函数
|
|
2
|
+
__all__ = ["load_image"]
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
import cv2
|
|
6
|
+
import glob
|
|
7
|
+
from argparse import Namespace
|
|
8
|
+
|
|
9
|
+
def judgeImgFormat_cv2(imageFilePath):
|
|
10
|
+
flag = True
|
|
11
|
+
try:
|
|
12
|
+
img = cv2.imread(imageFilePath)
|
|
13
|
+
if img is not None:
|
|
14
|
+
pass
|
|
15
|
+
else:
|
|
16
|
+
flag = False
|
|
17
|
+
except:
|
|
18
|
+
print(
|
|
19
|
+
imageFilePath,
|
|
20
|
+
": is not supported by CV2 Image format, and so will be cleared",
|
|
21
|
+
)
|
|
22
|
+
flag = False
|
|
23
|
+
|
|
24
|
+
return flag
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def load_image(args: Namespace) -> list:
|
|
28
|
+
|
|
29
|
+
# collect image
|
|
30
|
+
image_list = []
|
|
31
|
+
for file_path in args.img:
|
|
32
|
+
image_list.extend(glob.glob(file_path))
|
|
33
|
+
|
|
34
|
+
# judge the ImageFile format
|
|
35
|
+
image_list = list(filter(judgeImgFormat_cv2, image_list))
|
|
36
|
+
|
|
37
|
+
return image_list
|
|
File without changes
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# 明确指定允许被 import * 导出的函数
|
|
2
|
+
__all__ = ["load_video"]
|
|
3
|
+
|
|
4
|
+
import glob
|
|
5
|
+
import cv2
|
|
6
|
+
from argparse import Namespace
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def judgeVideoFormat_cv2(videoFilePath):
|
|
10
|
+
"""
|
|
11
|
+
使用 OpenCV 校验视频文件是否有效且可读
|
|
12
|
+
"""
|
|
13
|
+
flag = True
|
|
14
|
+
cap = None
|
|
15
|
+
try:
|
|
16
|
+
cap = cv2.VideoCapture(videoFilePath)
|
|
17
|
+
|
|
18
|
+
# 1. 检查视频是否能被 OpenCV 成功打开
|
|
19
|
+
if not cap.isOpened():
|
|
20
|
+
flag = False
|
|
21
|
+
|
|
22
|
+
# 2. 进一步检查是否能读取到有效帧(防止元数据损坏的空视频)
|
|
23
|
+
if flag:
|
|
24
|
+
ret, frame = cap.read()
|
|
25
|
+
if not ret or frame is None:
|
|
26
|
+
flag = False
|
|
27
|
+
|
|
28
|
+
except Exception:
|
|
29
|
+
# 捕获底层 OpenCV 可能抛出的异常(如段错误、不支持的编码等)
|
|
30
|
+
flag = False
|
|
31
|
+
finally:
|
|
32
|
+
# 确保释放资源,防止文件句柄泄露
|
|
33
|
+
if cap is not None:
|
|
34
|
+
cap.release()
|
|
35
|
+
|
|
36
|
+
if not flag:
|
|
37
|
+
print(
|
|
38
|
+
videoFilePath,
|
|
39
|
+
": is not supported by CV2 Video format, and so will be cleared",
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
return flag
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def load_video(args: Namespace) -> list:
|
|
46
|
+
video_list = []
|
|
47
|
+
|
|
48
|
+
for file_path in args.video:
|
|
49
|
+
video_list.extend(glob.glob(file_path))
|
|
50
|
+
|
|
51
|
+
video_list = list(filter(judgeVideoFormat_cv2, video_list))
|
|
52
|
+
|
|
53
|
+
return video_list
|
|
File without changes
|