2023-12-13 02:11:29 +01:00
|
|
|
from typing import Any
|
|
|
|
from django.db.models.query import QuerySet
|
2023-12-06 18:12:31 +01:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
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
|
|
|
|
from .models import UploadImage, PredictedImage
|
|
|
|
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)
|
|
|
|
if form.is_valid():
|
2023-12-13 02:11:29 +01:00
|
|
|
image = form.save(commit=False)
|
|
|
|
image.owner = request.user
|
|
|
|
image.save()
|
|
|
|
prediciton_results = predict_image(image)
|
|
|
|
image.predicted_image_url = (
|
|
|
|
f"{image.image.name}_predicted/{image.image.name.split('/')[-1]}"
|
|
|
|
)
|
|
|
|
image.save()
|
|
|
|
PredictedImage.objects.create(
|
|
|
|
original_image=image,
|
|
|
|
image=image.predicted_image_url,
|
|
|
|
confidence=prediciton_results[0]["confidence"],
|
|
|
|
class_name=prediciton_results[0]["name"],
|
2023-12-06 18:12:31 +01:00
|
|
|
)
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"upload.html",
|
|
|
|
{
|
2023-12-13 02:11:29 +01:00
|
|
|
"img_saved": True,
|
|
|
|
"img_url": image.predicted_image_url,
|
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):
|
|
|
|
model = PredictedImage
|
|
|
|
queryset = PredictedImage.objects.all()
|
|
|
|
template_name = "history.html"
|
|
|
|
paginate_by = 4
|
2023-12-06 18:12:31 +01:00
|
|
|
|
2023-12-13 02:11:29 +01:00
|
|
|
def get_queryset(self) -> QuerySet[Any]:
|
|
|
|
queryset = PredictedImage.objects.filter(
|
|
|
|
original_image__owner=self.request.user
|
|
|
|
).order_by("-date_predicted")
|
|
|
|
return queryset
|
2023-12-06 18:12:31 +01:00
|
|
|
|
|
|
|
|
2023-12-13 02:11:29 +01:00
|
|
|
class DetectionDetails(LoginRequiredMixin, DetailView):
|
|
|
|
model = PredictedImage
|
|
|
|
template_name = "detection_detail.html"
|