|
| 1 | +# |
| 2 | +# Copyright (c) 2023-2024 Vladislav Tsendrovskii |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, version 3 of the License. |
| 7 | +# This program is distributed in the hope that it will be useful, |
| 8 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | +# See the GNU General Public License for more details. |
| 11 | +# You should have received a copy of the GNU General Public License |
| 12 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 13 | +# |
| 14 | + |
| 15 | +import os |
| 16 | +import json |
| 17 | + |
| 18 | +import vstarstack.tool.common |
| 19 | +import vstarstack.tool.cfg |
| 20 | +import vstarstack.library.data |
| 21 | +import vstarstack.library.common |
| 22 | +from vstarstack.library.objects.features import find_keypoints_orb |
| 23 | +from vstarstack.library.objects.features import find_keypoints_brightness |
| 24 | +from vstarstack.library.objects.features import describe_keypoints |
| 25 | + |
| 26 | +def build_keypoints_structure(keypoints, ds, fname, name): |
| 27 | + record = { |
| 28 | + "fname" : fname, |
| 29 | + "name" : name, |
| 30 | + "points" : [], |
| 31 | + } |
| 32 | + |
| 33 | + for keypoint, desc in zip(keypoints, ds): |
| 34 | + for di in desc: |
| 35 | + if di != 0: |
| 36 | + break |
| 37 | + else: |
| 38 | + continue |
| 39 | + record["points"].append({ |
| 40 | + "keypoint" : keypoint, |
| 41 | + "descriptor" : [int(item) for item in list(desc)], |
| 42 | + }) |
| 43 | + |
| 44 | + return record |
| 45 | + |
| 46 | +def proj_find_keypoints_orb(files, num_splits, param): |
| 47 | + points = {} |
| 48 | + |
| 49 | + for fname in files: |
| 50 | + name = os.path.splitext(os.path.basename(fname))[0] |
| 51 | + dataframe = vstarstack.library.data.DataFrame.load(fname) |
| 52 | + gray, _ = vstarstack.library.common.df_to_light(dataframe) |
| 53 | + |
| 54 | + keypoints = find_keypoints_orb(gray, num_splits, param) |
| 55 | + ds = describe_keypoints(gray, keypoints, param) |
| 56 | + points[name] = build_keypoints_structure(keypoints, ds, fname, name) |
| 57 | + |
| 58 | + return points |
| 59 | + |
| 60 | +def proj_find_keypoints_brightness(files, num_splits, param, orb_param): |
| 61 | + points = {} |
| 62 | + |
| 63 | + for fname in files: |
| 64 | + name = os.path.splitext(os.path.basename(fname))[0] |
| 65 | + dataframe = vstarstack.library.data.DataFrame.load(fname) |
| 66 | + gray, _ = vstarstack.library.common.df_to_light(dataframe) |
| 67 | + |
| 68 | + keypoints = find_keypoints_brightness(gray, num_splits, param) |
| 69 | + ds = describe_keypoints(gray, keypoints, orb_param) |
| 70 | + points[name] = build_keypoints_structure(keypoints, ds, fname, name) |
| 71 | + |
| 72 | + return points |
| 73 | + |
| 74 | +def save_features(points, features_path): |
| 75 | + for name in points: |
| 76 | + record = points[name] |
| 77 | + with open(os.path.join(features_path, f"{name}_keypoints.json"), "w") as f: |
| 78 | + json.dump(record, f, indent=4, ensure_ascii=False) |
| 79 | + |
| 80 | +def find_points_orb(project: vstarstack.tool.cfg.Project, argv: list[str]): |
| 81 | + if len(argv) >= 2: |
| 82 | + inputs = argv[0] |
| 83 | + features = argv[1] |
| 84 | + else: |
| 85 | + inputs = project.config.paths.npy_fixed |
| 86 | + features = project.config.objects.features.path |
| 87 | + |
| 88 | + num_splits = project.config.objects.features.num_splits |
| 89 | + param = { |
| 90 | + "patchSize" : project.config.objects.features.orb.patchSize |
| 91 | + } |
| 92 | + |
| 93 | + files = vstarstack.tool.common.listfiles(inputs, ".zip") |
| 94 | + files = [filename for _, filename in files] |
| 95 | + points = proj_find_keypoints_orb(files, num_splits, param) |
| 96 | + save_features(points, features) |
| 97 | + |
| 98 | +def find_points_brightness(project: vstarstack.tool.cfg.Project, argv: list[str]): |
| 99 | + if len(argv) >= 2: |
| 100 | + inputs = argv[0] |
| 101 | + features = argv[1] |
| 102 | + else: |
| 103 | + inputs = project.config.paths.npy_fixed |
| 104 | + features = project.config.objects.features.path |
| 105 | + |
| 106 | + num_splits = project.config.objects.features.num_splits |
| 107 | + |
| 108 | + orb_param = { |
| 109 | + "patchSize" : project.config.objects.features.orb.patchSize |
| 110 | + } |
| 111 | + |
| 112 | + param = { |
| 113 | + "blur_size" : project.config.objects.features.bright_spots.blurSize, |
| 114 | + "k_thr" : project.config.objects.features.bright_spots.k_thr, |
| 115 | + "min_value" : project.config.objects.features.bright_spots.minValue, |
| 116 | + "min_pixel" : project.config.objects.features.bright_spots.minPixel, |
| 117 | + "max_pixel" : project.config.objects.features.bright_spots.maxPixel, |
| 118 | + } |
| 119 | + |
| 120 | + files = vstarstack.tool.common.listfiles(inputs, ".zip") |
| 121 | + files = [filename for _, filename in files] |
| 122 | + points = proj_find_keypoints_brightness(files, num_splits, param, orb_param) |
| 123 | + save_features(points, features) |
| 124 | + |
| 125 | +commands = { |
| 126 | + "brightness": (find_points_brightness, "find keypoints with brightness detector", "npys/ features/"), |
| 127 | + "orb": (find_points_orb, "find keypoints with ORB detector", "npys/ features/") |
| 128 | +} |
0 commit comments