2023-12-13 02:11:29 +01:00
|
|
|
from typing import Any
|
|
|
|
from django.db.models.query import QuerySet
|
2024-01-04 21:33:13 +01:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.shortcuts import render
|
2023-12-06 18:12:31 +01:00
|
|
|
from django.views import View
|
2023-11-20 15:53:42 +01:00
|
|
|
from .forms import DetectForm
|
2023-12-13 02:11:29 +01:00
|
|
|
from django.views.generic import ListView, DetailView
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.utils.decorators import method_decorator
|
2024-01-04 21:33:13 +01:00
|
|
|
from .models import PredictionBatch, UploadImage, PredictedImage
|
2023-12-13 02:11:29 +01:00
|
|
|
from .utils import predict_image
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2023-11-20 15:53:42 +01:00
|
|
|
|
|
|
|
|
2023-12-06 18:12:31 +01:00
|
|
|
class DetectView(View):
|
|
|
|
form_class = DetectForm
|
|
|
|
template_name = "upload.html"
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class()
|
2023-11-28 18:32:10 +01:00
|
|
|
return render(request, "upload.html", {"form": form})
|
2023-12-06 18:12:31 +01:00
|
|
|
|
2023-12-13 02:11:29 +01:00
|
|
|
@method_decorator(login_required)
|
2023-12-06 18:12:31 +01:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.form_class(request.POST, request.FILES)
|
2023-12-19 17:33:46 +01:00
|
|
|
files = request.FILES.getlist("image")
|
|
|
|
predictions = []
|
2023-12-06 18:12:31 +01:00
|
|
|
if form.is_valid():
|
2024-01-04 21:33:13 +01:00
|
|
|
pred_batch = PredictionBatch.objects.create(owner=request.user)
|
2023-12-19 17:33:46 +01:00
|
|
|
for f in files:
|
|
|
|
image = UploadImage(
|
|
|
|
image=f,
|
|
|
|
)
|
|
|
|
image.save()
|
|
|
|
prediciton_results = predict_image(image)
|
|
|
|
image.predicted_image_url = (
|
|
|
|
f"{image.image.name}_predicted/{image.image.name.split('/')[-1]}"
|
|
|
|
)
|
|
|
|
image.save()
|
|
|
|
try:
|
2023-12-23 17:01:39 +01:00
|
|
|
results_metrics = prediciton_results
|
2023-12-19 17:33:46 +01:00
|
|
|
except IndexError as e:
|
2023-12-23 17:01:39 +01:00
|
|
|
predicted_image = PredictedImage.objects.create(
|
2023-12-19 17:33:46 +01:00
|
|
|
original_image=image,
|
|
|
|
image=image.predicted_image_url,
|
2024-01-04 21:33:13 +01:00
|
|
|
prediction_data={"data": "no predicitions"},
|
2023-12-19 17:33:46 +01:00
|
|
|
)
|
|
|
|
else:
|
2023-12-23 17:01:39 +01:00
|
|
|
predicted_image = PredictedImage.objects.create(
|
2023-12-19 17:33:46 +01:00
|
|
|
original_image=image,
|
|
|
|
image=image.predicted_image_url,
|
2023-12-23 17:01:39 +01:00
|
|
|
prediction_data=results_metrics,
|
2023-12-19 17:33:46 +01:00
|
|
|
)
|
2023-12-23 17:01:39 +01:00
|
|
|
predictions.append(predicted_image)
|
2024-01-04 21:33:13 +01:00
|
|
|
pred_batch.images.add(*predictions)
|
|
|
|
pred_batch.save()
|
2023-12-06 18:12:31 +01:00
|
|
|
return render(
|
|
|
|
request,
|
2023-12-19 17:33:46 +01:00
|
|
|
"results.html",
|
2023-12-06 18:12:31 +01:00
|
|
|
{
|
2023-12-13 02:11:29 +01:00
|
|
|
"img_saved": True,
|
2024-01-04 21:33:13 +01:00
|
|
|
"img": pred_batch,
|
2023-12-06 18:12:31 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return render(request, "upload.html", {"form": form})
|
|
|
|
|
|
|
|
|
2023-12-13 02:11:29 +01:00
|
|
|
class ListHistory(LoginRequiredMixin, ListView):
|
2024-01-04 21:33:13 +01:00
|
|
|
model = PredictionBatch
|
|
|
|
queryset = PredictionBatch.objects.all()
|
2023-12-13 02:11:29 +01:00
|
|
|
template_name = "history.html"
|
2023-12-19 17:33:46 +01:00
|
|
|
paginate_by = 3
|
2023-12-06 18:12:31 +01:00
|
|
|
|
2023-12-13 02:11:29 +01:00
|
|
|
def get_queryset(self) -> QuerySet[Any]:
|
2024-01-04 21:33:13 +01:00
|
|
|
queryset = PredictionBatch.objects.filter(owner=self.request.user).order_by(
|
|
|
|
"-date_predicted"
|
|
|
|
)
|
2023-12-13 02:11:29 +01:00
|
|
|
return queryset
|
2023-12-06 18:12:31 +01:00
|
|
|
|
|
|
|
|
2023-12-13 02:11:29 +01:00
|
|
|
class DetectionDetails(LoginRequiredMixin, DetailView):
|
2024-01-04 21:33:13 +01:00
|
|
|
model = PredictionBatch
|
|
|
|
template_name = "results.html"
|
|
|
|
context_object_name = "img"
|
|
|
|
|
|
|
|
|
|
|
|
def download_pred_res(request, pk):
|
|
|
|
pred_batch = PredictionBatch.objects.get(pk=pk)
|
|
|
|
response = HttpResponse(content_type="text/plain")
|
|
|
|
response["Content-Disposition"] = "attachment; filename=predictions.txt"
|
|
|
|
for img in pred_batch.images.all():
|
|
|
|
response.write(img.prediction_data)
|
|
|
|
return response
|