2023-12-23 17:01:39 +01:00
|
|
|
import json
|
2023-11-20 15:53:42 +01:00
|
|
|
from django.db import models
|
2023-11-28 18:32:10 +01:00
|
|
|
from django.core.validators import FileExtensionValidator
|
2023-12-13 02:11:29 +01:00
|
|
|
from django.contrib.auth.models import User
|
2023-11-20 15:53:42 +01:00
|
|
|
|
|
|
|
# Create your models here.
|
|
|
|
|
2023-11-28 18:32:10 +01:00
|
|
|
|
2023-12-13 02:11:29 +01:00
|
|
|
class UploadImage(models.Model):
|
2023-11-28 18:32:10 +01:00
|
|
|
image = models.ImageField(
|
|
|
|
upload_to="plankton/%Y/%m/%d/",
|
|
|
|
validators=[FileExtensionValidator(allowed_extensions=["jpg", "png"])],
|
|
|
|
)
|
2023-12-13 02:11:29 +01:00
|
|
|
predicted_image_url = models.CharField(max_length=500)
|
|
|
|
|
|
|
|
|
|
|
|
class PredictedImage(models.Model):
|
|
|
|
original_image = models.OneToOneField(UploadImage, on_delete=models.CASCADE)
|
|
|
|
image = models.ImageField(upload_to=f"plankton/%Y/%m/%d/{original_image.name}")
|
2023-12-23 17:01:39 +01:00
|
|
|
prediction_data = models.JSONField()
|
2023-12-13 02:11:29 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def get_original_image(self):
|
|
|
|
return self.original_image.image
|
2023-12-23 17:01:39 +01:00
|
|
|
|
|
|
|
def get_prediction_data(self):
|
|
|
|
results = json.loads(self.prediction_data)
|
|
|
|
for pred in results:
|
|
|
|
pred["confidence"] = round(pred["confidence"] * 100, 2)
|
|
|
|
return results
|
2024-01-04 21:33:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PredictionBatch(models.Model):
|
|
|
|
images = models.ManyToManyField(PredictedImage)
|
|
|
|
date_predicted = models.DateTimeField(auto_now_add=True)
|
|
|
|
owner = models.ForeignKey(User, on_delete=models.CASCADE)
|