diff --git a/PlanktonDetector/DetectionApp/urls.py b/PlanktonDetector/DetectionApp/urls.py index 6b31a57..160f192 100644 --- a/PlanktonDetector/DetectionApp/urls.py +++ b/PlanktonDetector/DetectionApp/urls.py @@ -1,8 +1,12 @@ -from django.urls import path +from django.urls import path, include from . import views from django.conf.urls.static import static from django.conf import settings +from django.contrib.auth import urls -urlpatterns = [path("detect/", views.view, name="detect")] + static( - settings.MEDIA_URL, document_root=settings.MEDIA_ROOT -) +urlpatterns = [ + path("detect/", views.DetectView.as_view(), name="detect"), + path("login/", views.LoginView.as_view(), name="login"), + path("logout/", views.logout_view, name="logout"), + path("register/", views.SignupView.as_view(), name="signup"), +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/PlanktonDetector/DetectionApp/views.py b/PlanktonDetector/DetectionApp/views.py index 8ed1608..eb2bcf5 100644 --- a/PlanktonDetector/DetectionApp/views.py +++ b/PlanktonDetector/DetectionApp/views.py @@ -1,37 +1,85 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect +from django.views import View from .forms import DetectForm -from .models import DetectImage -from django.core.files.uploadedfile import SimpleUploadedFile from django.conf import settings from ultralytics import YOLO +from django.contrib.auth.forms import AuthenticationForm, UserCreationForm +from django.contrib.auth import authenticate, login, logout # Create your views here. MODEL = YOLO("best.pt") -def view(request): - if request.method == "GET": - form = DetectForm() - return render(request, "upload.html", {"form": form}) - form = DetectForm(request.POST, request.FILES) - if form.is_valid(): - image = form.save() - MODEL.predict( - image.image.path, - save=True, - project=settings.MEDIA_ROOT, - name=f"{image.image.name}_predicted", - imgsz=[640, 640], - ) - print(f"{settings.BASE_DIR}/predicted2/{image.image.name}") - saved = form.is_valid() - return render( - request, - "upload.html", - { - "img_saved": saved, - "img_path": f"{image.image.name}_predicted/{image.image.name.split('/')[-1]}", - }, - ) - else: +class DetectView(View): + form_class = DetectForm + template_name = "upload.html" + + def get(self, request, *args, **kwargs): + form = self.form_class() return render(request, "upload.html", {"form": form}) + + def post(self, request, *args, **kwargs): + form = self.form_class(request.POST, request.FILES) + if form.is_valid(): + image = form.save() + MODEL.predict( + image.image.path, + save=True, + project=settings.MEDIA_ROOT, + name=f"{image.image.name}_predicted", + imgsz=[640, 640], + ) + saved = form.is_valid() + return render( + request, + "upload.html", + { + "img_saved": saved, + "img_path": f"{image.image.name}_predicted/{image.image.name.split('/')[-1]}", + }, + ) + else: + return render(request, "upload.html", {"form": form}) + + +class LoginView(View): + form_class = AuthenticationForm + template_name = "login_signup.html" + + def get(self, request, *args, **kwargs): + form = self.form_class() + return render(request, self.template_name, {"form": form}) + + def post(self, request, *args, **kwargs): + form = self.form_class(data=request.POST) + if form.is_valid(): + user = authenticate( + username=form.cleaned_data["username"], + password=form.cleaned_data["password"], + ) + if user is not None: + login(request, user) + return redirect("detect") + return render(request, self.template_name, {"form": form}) + + +class SignupView(View): + form_class = UserCreationForm + template_name = "login_signup.html" + + def get(self, request, *args, **kwargs): + form = self.form_class() + return render(request, self.template_name, {"form": form, "signup": True}) + + def post(self, request, *args, **kwargs): + form = self.form_class(data=request.POST) + if form.is_valid(): + user = form.save() + login(request, user) + return redirect("detect") + return render(request, self.template_name, {"form": form, "signup": True}) + + +def logout_view(request): + logout(request) + return redirect("detect") diff --git a/PlanktonDetector/PlanktonDetector/settings.py b/PlanktonDetector/PlanktonDetector/settings.py index 5f19555..66a1b06 100644 --- a/PlanktonDetector/PlanktonDetector/settings.py +++ b/PlanktonDetector/PlanktonDetector/settings.py @@ -56,7 +56,7 @@ ROOT_URLCONF = "PlanktonDetector.urls" TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [os.path.join(BASE_DIR, 'templates')], + "DIRS": [os.path.join(BASE_DIR, "templates")], "APP_DIRS": True, "OPTIONS": { "context_processors": [ @@ -119,14 +119,16 @@ USE_TZ = True STATIC_URL = "static/" -STATIC_ROOT = '' +STATIC_ROOT = "" -STATICFILES_DIRS = ('static',) +STATICFILES_DIRS = ("static",) MEDIA_ROOT = BASE_DIR / "uploaded_media" MEDIA_URL = "media/" +LOGOUT_REDIRECT_URL = "detect/" + # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/PlanktonDetector/db.sqlite3 b/PlanktonDetector/db.sqlite3 index 8663940..6eb5dc8 100644 Binary files a/PlanktonDetector/db.sqlite3 and b/PlanktonDetector/db.sqlite3 differ diff --git a/PlanktonDetector/static/DetectionApp/css/base.css b/PlanktonDetector/static/DetectionApp/css/base.css new file mode 100644 index 0000000..e665d26 --- /dev/null +++ b/PlanktonDetector/static/DetectionApp/css/base.css @@ -0,0 +1,40 @@ +body, html{ + height:100%; + margin: 0px 0px 0px 0px; + display:flex; + justify-content: center; + flex-direction: column; + font-family:'Inter', sans-serif; +} + +.top-menu a{ + text-decoration: none; + margin-right: 2rem; + color:black; +} + +.top-menu{ + display: flex; + flex-direction: row; + height: 5%; + justify-content:flex-end; + align-items: center; + background-color:lightgray; + margin-bottom:5px; + border: grey solid; + border-width: 0px 2px 2px 2px; +} + + +.footer{ + display:flex; + flex-direction: row; + justify-content: center; + align-items: center; + margin-top: 5px; + background-color: lightgray; + border: grey solid; + border-width: 2px 2px 0px 2px; + justify-content: space-evenly; + align-items: center; +} diff --git a/PlanktonDetector/static/DetectionApp/css/login_signup.css b/PlanktonDetector/static/DetectionApp/css/login_signup.css new file mode 100644 index 0000000..934c4c5 --- /dev/null +++ b/PlanktonDetector/static/DetectionApp/css/login_signup.css @@ -0,0 +1,33 @@ +.form_div{ + height: 100%; + width: 100%; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; +} + +.form_div input,label, p{ + font-size: 1.2rem; + margin-bottom: 5px; +} + +.form{ + display: flex; + flex-direction: column; + align-items: center; + justify-content: space-evenly; + border: grey solid; + border-width: 4px; + border-radius: 10%; + padding: 5%; + background-color: lightgray; +} + +.form span{ + display: none; +} + +#submit{ + background-color: chartreuse; +} diff --git a/PlanktonDetector/static/DetectionApp/css/styles.css b/PlanktonDetector/static/DetectionApp/css/upload.css similarity index 74% rename from PlanktonDetector/static/DetectionApp/css/styles.css rename to PlanktonDetector/static/DetectionApp/css/upload.css index d1913e2..efd0898 100644 --- a/PlanktonDetector/static/DetectionApp/css/styles.css +++ b/PlanktonDetector/static/DetectionApp/css/upload.css @@ -1,32 +1,34 @@ -html, body, form{ + +form{ height:100%; - width: 100%; + width:100%; display:flex; - flex-direction: row; - font-family:'Inter', sans-serif; + flex-direction:row; + margin:auto; } + .side_menu{ display: flex; - justify-content: flex-start; + justify-content: center; align-items:center; flex-direction: column; width: 25%; background-color: lightgray; + border: grey solid; + border-width: 2px; } #description{ font-size: 40px; - margin-left: 10px; - margin-right: 10px; + text-align: center; } #submit{ margin-bottom: 40px; background-color: chartreuse; margin-top: auto; - justify-self: flex-end; height: 50px; width: 80%; border-radius: 25px; @@ -44,7 +46,6 @@ html, body, form{ .upload_button{ - justify-self: right; display:flex; justify-content: flex-start; align-items: flex-start; diff --git a/PlanktonDetector/templates/base.html b/PlanktonDetector/templates/base.html new file mode 100644 index 0000000..8ddf9b5 --- /dev/null +++ b/PlanktonDetector/templates/base.html @@ -0,0 +1,30 @@ +{% load static %} + + + +
+ + + + + + + {%block extracss%}{%endblock extracss%} +Register
+{% else%} +Login
+{%endif%} + +Don't have an account? Sign Up
+{%else%} + + +{%endif%} +