Update myapp/views.py
Browse files- myapp/views.py +86 -105
myapp/views.py
CHANGED
|
@@ -1,105 +1,86 @@
|
|
| 1 |
-
from django.http import StreamingHttpResponse, JsonResponse
|
| 2 |
-
from django.shortcuts import get_object_or_404
|
| 3 |
-
from django.conf import settings
|
| 4 |
-
from .detector import TrafficDetector
|
| 5 |
-
from .forms import UploadFile
|
| 6 |
-
from .models import Document
|
| 7 |
-
from django.shortcuts import render, redirect
|
| 8 |
-
import os
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
MODEL_PATH = os.path.join(settings.BASE_DIR, 'myapp', 'media', 'model_4', 'model_4.pt')
|
| 12 |
-
detector = TrafficDetector(MODEL_PATH)
|
| 13 |
-
|
| 14 |
-
def detect_stream(request, file_id):
|
| 15 |
-
doc = get_object_or_404(Document, id=file_id)
|
| 16 |
-
file_path = doc.uploaded_file.path
|
| 17 |
-
|
| 18 |
-
# Track current file being processed
|
| 19 |
-
detector.current_file_id = file_id
|
| 20 |
-
detector.stop_processing = False
|
| 21 |
-
|
| 22 |
-
return StreamingHttpResponse(
|
| 23 |
-
detector.process_file(file_path),
|
| 24 |
-
content_type='multipart/x-mixed-replace; boundary=frame'
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
def get_vehicle_count(request):
|
| 28 |
-
return JsonResponse({
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
def
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
files = Document.objects.all()
|
| 88 |
-
|
| 89 |
-
active_file_id = request.GET.get('play')
|
| 90 |
-
return render(request, 'traffic_system.html',{
|
| 91 |
-
'form': form,
|
| 92 |
-
'files':files,
|
| 93 |
-
'active_file_id': active_file_id,
|
| 94 |
-
'objects_count': detector.objects_count
|
| 95 |
-
})
|
| 96 |
-
|
| 97 |
-
def about(request):
|
| 98 |
-
return render(request, 'about.html')
|
| 99 |
-
|
| 100 |
-
def menu(request):
|
| 101 |
-
return render(request, 'index.html')
|
| 102 |
-
|
| 103 |
-
def Doc(request):
|
| 104 |
-
return render(request, 'doc.html')
|
| 105 |
-
|
|
|
|
| 1 |
+
from django.http import StreamingHttpResponse, JsonResponse
|
| 2 |
+
from django.shortcuts import get_object_or_404
|
| 3 |
+
from django.conf import settings
|
| 4 |
+
from .detector import TrafficDetector
|
| 5 |
+
from .forms import UploadFile
|
| 6 |
+
from .models import Document
|
| 7 |
+
from django.shortcuts import render, redirect
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
MODEL_PATH = os.path.join(settings.BASE_DIR, 'myapp', 'media', 'model_4', 'model_4.pt')
|
| 12 |
+
detector = TrafficDetector(MODEL_PATH)
|
| 13 |
+
|
| 14 |
+
def detect_stream(request, file_id):
|
| 15 |
+
doc = get_object_or_404(Document, id=file_id)
|
| 16 |
+
file_path = doc.uploaded_file.path
|
| 17 |
+
|
| 18 |
+
# Track current file being processed
|
| 19 |
+
detector.current_file_id = file_id
|
| 20 |
+
detector.stop_processing = False
|
| 21 |
+
|
| 22 |
+
return StreamingHttpResponse(
|
| 23 |
+
detector.process_file(file_path),
|
| 24 |
+
content_type='multipart/x-mixed-replace; boundary=frame'
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def get_vehicle_count(request):
|
| 28 |
+
return JsonResponse({
|
| 29 |
+
'objects_count': detector.objects_count,
|
| 30 |
+
})
|
| 31 |
+
|
| 32 |
+
def get_traffic_weight(request):
|
| 33 |
+
return JsonResponse({
|
| 34 |
+
'traffic_weight': detector.traffic_weight
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
def delete_file(request, file_id):
|
| 38 |
+
doc = get_object_or_404(Document, id=file_id)
|
| 39 |
+
# Get the file path before deleting from database
|
| 40 |
+
file_path = doc.uploaded_file.path
|
| 41 |
+
|
| 42 |
+
# If this file is currently being processed, stop it
|
| 43 |
+
if detector.current_file_id == file_id:
|
| 44 |
+
detector.stop_processing = True
|
| 45 |
+
import time
|
| 46 |
+
time.sleep(1) # Wait for stream to stop
|
| 47 |
+
|
| 48 |
+
# Delete from database
|
| 49 |
+
doc.delete()
|
| 50 |
+
|
| 51 |
+
# Delete the physical file from the uploads folder
|
| 52 |
+
if os.path.exists(file_path):
|
| 53 |
+
try:
|
| 54 |
+
os.remove(file_path)
|
| 55 |
+
except OSError as e:
|
| 56 |
+
print(f"Error deleting file {file_path}: {e}")
|
| 57 |
+
|
| 58 |
+
return redirect('traffic')
|
| 59 |
+
|
| 60 |
+
def traffic_view(request):
|
| 61 |
+
if request.method == 'POST':
|
| 62 |
+
form = UploadFile(request.POST, request.FILES)
|
| 63 |
+
if form.is_valid():
|
| 64 |
+
form.save()
|
| 65 |
+
else:
|
| 66 |
+
form = UploadFile()
|
| 67 |
+
|
| 68 |
+
files = Document.objects.all()
|
| 69 |
+
|
| 70 |
+
active_file_id = request.GET.get('play')
|
| 71 |
+
return render(request, 'traffic_system.html',{
|
| 72 |
+
'form': form,
|
| 73 |
+
'files':files,
|
| 74 |
+
'active_file_id': active_file_id,
|
| 75 |
+
'objects_count': detector.objects_count,
|
| 76 |
+
'traffic_weight': detector.traffic_weight
|
| 77 |
+
})
|
| 78 |
+
|
| 79 |
+
def about(request):
|
| 80 |
+
return render(request, 'about.html')
|
| 81 |
+
|
| 82 |
+
def menu(request):
|
| 83 |
+
return render(request, 'index.html')
|
| 84 |
+
|
| 85 |
+
def Doc(request):
|
| 86 |
+
return render(request, 'doc.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|