This repository was archived by the owner on Apr 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (63 loc) · 2.44 KB
/
main.py
File metadata and controls
90 lines (63 loc) · 2.44 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
"""Entry point for Astro-Pi"""
import logging
import time
from datetime import datetime
from pathlib import Path
from typing import Union
import config
from utils import camera, speeds, statistics
from utils.camera import Camera
_LOGGER = logging.getLogger(__name__)
def sleep_until(date: datetime) -> None:
time.sleep(max((date - datetime.now()).total_seconds(), 0))
def update_images(camera_: Camera) -> Union[list[Path], None]:
image_count = camera.count_images(config.IMAGE_PATH)
image = camera_.capture(config.IMAGE_PATH)
if not image_count:
_LOGGER.info(f'Taken initial image "images/{image.name}"')
return None
_LOGGER.info(f'Taken image "images/{image.name}"')
return camera.prune_images(config.IMAGE_PATH, config.IMAGE_LOOKBEHIND + 1)
def update_speeds(
base_image: Path,
compare_images: list[Path],
match_speeds: list[float],
geotag_speeds: list[float],
) -> None:
_LOGGER.info(
f"Comparing ({base_image.name}) -> ({', '.join([image.name for image in compare_images])})"
)
last_match_speeds, last_geotag_speeds = speeds.compute_speeds(
base_image, compare_images
)
match_speeds.extend(last_match_speeds)
geotag_speeds.extend(last_geotag_speeds)
geotags_result = speeds.process_geotags(geotag_speeds)
matches_result = speeds.process_matches(match_speeds)
if not any((geotags_result, matches_result)):
_LOGGER.info("Insufficient data for final speed")
return
average_speed = statistics.weigh(
list(filter(None, (geotags_result, matches_result)))
)
with open(config.BASE_DIR / "result.txt", "w") as output:
output.write(f"{average_speed:#.5g} km/s\n")
_LOGGER.info(f'Final Speed: {average_speed:#.5g} km/s - Saved to "results.txt"')
def main() -> None:
config.setup_logging()
start_time = datetime.now()
end_time = start_time + config.RUN_DURATION
config.IMAGE_PATH.mkdir(exist_ok=True)
camera.purge_images(config.IMAGE_PATH)
camera_ = Camera()
match_speeds: list[float] = []
geotag_speeds: list[float] = []
_LOGGER.info("Astro-Pi Started")
while datetime.now() < end_time:
next_image_time = datetime.now() + config.IMAGE_INTERVAL
images = update_images(camera_)
if images is not None:
update_speeds(images.pop(), images[::-1], match_speeds, geotag_speeds)
sleep_until(next_image_time)
if __name__ == "__main__":
main()