added compass direction finding
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import math
|
||||
import secrets
|
||||
|
||||
from django.contrib.auth.hashers import check_password, make_password
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.files.storage import default_storage
|
||||
from django.db import models, transaction
|
||||
from django.utils import timezone
|
||||
@@ -44,13 +46,48 @@ class Sensor(models.Model):
|
||||
self.save(update_fields=fields)
|
||||
|
||||
|
||||
class ClassificationRule(models.Model):
|
||||
"""A configurable inclusion or exclusion rule for a classification label."""
|
||||
|
||||
class RuleType(models.TextChoices):
|
||||
WHITELIST = "whitelist", "Whitelist"
|
||||
BLACKLIST = "blacklist", "Blacklist"
|
||||
|
||||
value = models.CharField(max_length=255)
|
||||
rule_type = models.CharField(max_length=10, choices=RuleType.choices)
|
||||
enabled = models.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["value"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=["value", "rule_type"], name="unique_classification_rule"),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.value} ({self.get_rule_type_display()})"
|
||||
|
||||
|
||||
class ClassifiedAudio(models.Model):
|
||||
"""Shared fields for a classifier response and its original audio upload."""
|
||||
|
||||
audio_file = models.FileField(upload_to="fireworks_audio/%Y/%m/%d")
|
||||
class SourceType(models.TextChoices):
|
||||
SENSOR = "sensor", "Sensor"
|
||||
MANUAL = "manual", "Manual report"
|
||||
|
||||
audio_file = models.FileField(upload_to="fireworks_audio/%Y/%m/%d", blank=True)
|
||||
sensor = models.ForeignKey(Sensor, null=True, blank=True, on_delete=models.SET_NULL, related_name="%(class)s_records")
|
||||
original_filename = models.CharField(max_length=255)
|
||||
classifier_response = models.JSONField()
|
||||
source_type = models.CharField(max_length=10, choices=SourceType.choices, default=SourceType.SENSOR)
|
||||
occurrences = models.PositiveIntegerField(default=1)
|
||||
location = models.CharField(max_length=255, blank=True)
|
||||
latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
|
||||
longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
|
||||
h3_cell = models.CharField(max_length=20, blank=True, db_index=True)
|
||||
visually_confirmed = models.BooleanField(default=False)
|
||||
direction_degrees = models.FloatField(null=True, blank=True)
|
||||
direction_accuracy_degrees = models.FloatField(null=True, blank=True)
|
||||
direction_source = models.CharField(max_length=32, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
@@ -60,6 +97,36 @@ class ClassifiedAudio(models.Model):
|
||||
def __str__(self):
|
||||
return self.original_filename
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
errors = {}
|
||||
if self.direction_degrees is not None and (
|
||||
not math.isfinite(self.direction_degrees) or not 0 <= self.direction_degrees < 360
|
||||
):
|
||||
errors["direction_degrees"] = "Direction must be a finite value from 0 up to, but not including, 360 degrees."
|
||||
if self.direction_accuracy_degrees is not None and (
|
||||
not math.isfinite(self.direction_accuracy_degrees) or self.direction_accuracy_degrees < 0
|
||||
):
|
||||
errors["direction_accuracy_degrees"] = "Compass accuracy must be a finite, non-negative value."
|
||||
if errors:
|
||||
raise ValidationError(errors)
|
||||
|
||||
|
||||
@property
|
||||
def direction_cardinal(self):
|
||||
if self.direction_degrees is None:
|
||||
return None
|
||||
directions = ("N", "NE", "E", "SE", "S", "SW", "W", "NW")
|
||||
return directions[int((self.direction_degrees + 22.5) % 360 // 45)]
|
||||
|
||||
|
||||
@property
|
||||
def direction_display(self):
|
||||
if self.direction_degrees is None:
|
||||
return "—"
|
||||
return f"{self.direction_degrees:g}° ({self.direction_cardinal})"
|
||||
|
||||
|
||||
@property
|
||||
def predictions(self):
|
||||
predictions = self.classifier_response.get("predictions", [])
|
||||
@@ -120,6 +187,16 @@ class PendingEvent(ClassifiedAudio):
|
||||
sensor=self.sensor,
|
||||
original_filename=self.original_filename,
|
||||
classifier_response=self.classifier_response,
|
||||
source_type=self.source_type,
|
||||
occurrences=self.occurrences,
|
||||
location=self.location,
|
||||
latitude=self.latitude,
|
||||
longitude=self.longitude,
|
||||
h3_cell=self.h3_cell,
|
||||
visually_confirmed=self.visually_confirmed,
|
||||
direction_degrees=self.direction_degrees,
|
||||
direction_accuracy_degrees=self.direction_accuracy_degrees,
|
||||
direction_source=self.direction_source,
|
||||
)
|
||||
self.delete()
|
||||
return event
|
||||
|
||||
Reference in New Issue
Block a user