2023-11-20 15:53:42 +01:00
|
|
|
from django.shortcuts import render
|
|
|
|
from .forms import DetectForm
|
|
|
|
from .models import DetectImage
|
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
|
|
|
from django.conf import settings
|
2023-11-27 16:52:09 +01:00
|
|
|
from ultralytics import YOLO
|
2023-11-20 15:53:42 +01:00
|
|
|
|
|
|
|
# Create your views here.
|
2023-11-27 16:52:09 +01:00
|
|
|
MODEL = YOLO("yolov8n.pt")
|
2023-11-20 15:53:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def view(request):
|
|
|
|
if request.method == "GET":
|
2023-11-28 18:32:10 +01:00
|
|
|
form = DetectForm()
|
2023-11-20 15:53:42 +01:00
|
|
|
return render(request, "upload.html", {"form": form})
|
|
|
|
form = DetectForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
image = form.save()
|
2023-11-27 16:52:09 +01:00
|
|
|
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}")
|
2023-11-20 15:53:42 +01:00
|
|
|
saved = form.is_valid()
|
|
|
|
return render(
|
2023-11-27 16:52:09 +01:00
|
|
|
request,
|
|
|
|
"upload.html",
|
|
|
|
{
|
|
|
|
"img_saved": saved,
|
|
|
|
"img_path": f"{image.image.name}_predicted/{image.image.name.split('/')[-1]}",
|
|
|
|
},
|
2023-11-20 15:53:42 +01:00
|
|
|
)
|
|
|
|
else:
|
2023-11-28 18:32:10 +01:00
|
|
|
return render(request, "upload.html", {"form": form})
|