-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.py
More file actions
102 lines (89 loc) · 3.62 KB
/
detect.py
File metadata and controls
102 lines (89 loc) · 3.62 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
#
# Copyright (c) 2022 Vladislav Tsendrovskii
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import json
import os
import numpy as np
import vstarstack.library.data
import vstarstack.library.image_process
import vstarstack.library.image_process.togray
import vstarstack.tool.cfg
import vstarstack.library.common
import vstarstack.tool.usage
import vstarstack.tool.common
import vstarstack.library.objects.brightness_detector as bd
import vstarstack.library.objects.disc_detector as dd
def _process_file(project, filename, descfilename, detector):
image = vstarstack.library.data.DataFrame.load(filename)
if os.path.isfile(descfilename):
with open(descfilename, encoding='utf8') as f:
desc = json.load(f)
else:
desc = {}
desc["object"] = {}
print("detector = ", detector)
gray,_ = vstarstack.library.image_process.togray.df_to_gray(image)
gray = gray / np.amax(gray)
if vstarstack.tool.cfg.DEBUG:
import matplotlib.pyplot as plt
plt.imshow(gray)
plt.show()
thresh = project.config.objects.threshold
if detector == "disc":
mindelta = project.config.objects.disc.mindelta
maxdelta = project.config.objects.disc.maxdelta
num_bin_curv = project.config.objects.disc.num_bins_curvature
num_bin_dist = project.config.objects.disc.num_bins_distance
planets = dd.detect(gray, thresh, mindelta, maxdelta, num_bin_curv, num_bin_dist)
elif detector == "brightness":
min_size = project.config.objects.brightness.min_diameter
max_size = project.config.objects.brightness.max_diameter
planets = bd.detect(gray, min_size, max_size, thresh)
else:
raise Exception(f"Invalid detector {detector}")
if len(planets) > 0:
desc["object"] = planets[0]
vstarstack.tool.common.check_dir_exists(descfilename)
with open(descfilename, "w", encoding='utf8') as f:
json.dump(desc, f, indent=4)
def _process_path(project, npys, descs, detector):
files = vstarstack.tool.common.listfiles(npys, ".zip")
for name, filename in files:
print(name)
out = os.path.join(descs, name + ".json")
_process_file(project, filename, out, detector)
def _process(project, detector, argv):
if len(argv) > 0:
input_path = argv[0]
output_path = argv[1]
if os.path.isdir(input_path):
_process_path(project, input_path, output_path, detector)
else:
_process_file(project, input_path, output_path, detector)
else:
_process_path(project,
project.config.paths.light.npy,
project.config.paths.descs,
detector)
def _process_brightness(project, argv):
_process(project, "brightness", argv)
def _process_disc(project, argv):
_process(project, "disc", argv)
commands = {
"brightness": (_process_brightness,
"detect compact objects with brightness detector",
"npy/ descs/"),
"disc": (_process_disc,
"detect compact objects with disc detector",
"npy/ descs/"),
}