Spaces:
Sleeping
Sleeping
Update app/apps/emotion_recognition/views.py
Browse files- app/apps/emotion_recognition/views.py +117 -118
app/apps/emotion_recognition/views.py
CHANGED
@@ -1,119 +1,118 @@
|
|
1 |
-
from django.shortcuts import render, redirect
|
2 |
-
from django.views.decorators.http import require_http_methods
|
3 |
-
from django.http import HttpResponse, StreamingHttpResponse
|
4 |
-
from django.contrib.auth.mixins import LoginRequiredMixin
|
5 |
-
from django.views.generic import UpdateView, DeleteView
|
6 |
-
from django.contrib import messages
|
7 |
-
from django.urls import reverse_lazy
|
8 |
-
|
9 |
-
from rest_framework.views import APIView
|
10 |
-
from rest_framework.response import Response
|
11 |
-
from rest_framework import status
|
12 |
-
from rest_framework.parsers import MultiPartParser, FormParser
|
13 |
-
from .serializers import ImageSerializer
|
14 |
-
from PIL import Image
|
15 |
-
|
16 |
-
from .models import UserImageRecognition
|
17 |
-
from .
|
18 |
-
from .
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
samples
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
image_data.
|
44 |
-
image_data.
|
45 |
-
image_data.
|
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 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
serializer
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
"
|
110 |
-
|
111 |
-
|
112 |
-
response =
|
113 |
-
response
|
114 |
-
response
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
1 |
+
from django.shortcuts import render, redirect
|
2 |
+
from django.views.decorators.http import require_http_methods
|
3 |
+
from django.http import HttpResponse, StreamingHttpResponse
|
4 |
+
from django.contrib.auth.mixins import LoginRequiredMixin
|
5 |
+
from django.views.generic import UpdateView, DeleteView
|
6 |
+
from django.contrib import messages
|
7 |
+
from django.urls import reverse_lazy
|
8 |
+
|
9 |
+
from rest_framework.views import APIView
|
10 |
+
from rest_framework.response import Response
|
11 |
+
from rest_framework import status
|
12 |
+
from rest_framework.parsers import MultiPartParser, FormParser
|
13 |
+
from .serializers import ImageSerializer
|
14 |
+
from PIL import Image
|
15 |
+
|
16 |
+
from .models import UserImageRecognition
|
17 |
+
from .tasks import proccess_uploaded_image, process_image_from_api, convert_image_to_bytes
|
18 |
+
from .forms import RecognitionEditForm
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
@require_http_methods(['GET', 'POST'])
|
23 |
+
def index(request):
|
24 |
+
try:
|
25 |
+
|
26 |
+
# GET method, return HTML page
|
27 |
+
if request.method == 'GET':
|
28 |
+
samples = UserImageRecognition.objects.all()
|
29 |
+
return render(request, 'recognition/index.html', {'samples': samples, })
|
30 |
+
|
31 |
+
if request.FILES and request.method == 'POST':
|
32 |
+
for f in request.FILES.getlist('uploaded_file'):
|
33 |
+
uploaded_image = f
|
34 |
+
image_data = UserImageRecognition.objects.create(uploaded_image=uploaded_image)
|
35 |
+
|
36 |
+
proccess_uploaded_image(image_data.id)
|
37 |
+
|
38 |
+
return redirect('recognition:index')
|
39 |
+
|
40 |
+
except Exception as e:
|
41 |
+
|
42 |
+
image_data.status = 'ERR'
|
43 |
+
image_data.error_occurred = True
|
44 |
+
image_data.error_message = str(e)
|
45 |
+
image_data.save()
|
46 |
+
|
47 |
+
return HttpResponse(f'Error: {str(e)}')
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
# @require_http_methods(['GET', 'POST'])
|
52 |
+
# def real_time_recognition(request):
|
53 |
+
# return render(request, 'recognition/real_time.html')
|
54 |
+
|
55 |
+
# def real_time_stream(request):
|
56 |
+
# return StreamingHttpResponse(emotionVideo(),content_type="multipart/x-mixed-replace;boundary=frame")
|
57 |
+
|
58 |
+
|
59 |
+
class RecognitionUpdateView(LoginRequiredMixin, UpdateView):
|
60 |
+
model = UserImageRecognition
|
61 |
+
form_class = RecognitionEditForm
|
62 |
+
template_name = "recognition/recognition_edit.html"
|
63 |
+
|
64 |
+
def get(self, request, pk):
|
65 |
+
self.object = self.get_object()
|
66 |
+
|
67 |
+
context = self.get_context_data(object=self.object)
|
68 |
+
return self.render_to_response(context)
|
69 |
+
|
70 |
+
def get_success_url(self, **kwargs):
|
71 |
+
pk = self.object.pk
|
72 |
+
messages.success(self.request, 'Запись была успешно изменена!')
|
73 |
+
return reverse_lazy('recognition:recognition_edit', args=(pk,))
|
74 |
+
|
75 |
+
|
76 |
+
class RecognitionDeleteView(LoginRequiredMixin, DeleteView):
|
77 |
+
model = UserImageRecognition
|
78 |
+
template_name = "recognition/recognition_delete.html"
|
79 |
+
|
80 |
+
def delete(self, request, pk):
|
81 |
+
return super().delete(request, pk)
|
82 |
+
|
83 |
+
def get_success_url(self, **kwargs):
|
84 |
+
obj = self.get_object()
|
85 |
+
messages.success(self.request, 'Запись была успешно удалёна!')
|
86 |
+
return reverse_lazy('recognition:index')
|
87 |
+
|
88 |
+
|
89 |
+
class ImageProcessingView(APIView):
|
90 |
+
parser_classes = (MultiPartParser, FormParser) # Для обработки загруженных файлов
|
91 |
+
serializer_class = ImageSerializer
|
92 |
+
|
93 |
+
def post(self, request, format=None):
|
94 |
+
"""
|
95 |
+
Обрабатывает изображение, переданное через API, и возвращает финальное изображение с эмоциями.
|
96 |
+
"""
|
97 |
+
serializer = self.serializer_class(data=request.data)
|
98 |
+
if serializer.is_valid():
|
99 |
+
image_file = serializer.validated_data['image']
|
100 |
+
|
101 |
+
try:
|
102 |
+
# Обработка изображения
|
103 |
+
final_image, predicted_emotions, recognized_emotion = process_image_from_api(image_file)
|
104 |
+
output_image = convert_image_to_bytes(final_image)
|
105 |
+
|
106 |
+
# Формируем ответ
|
107 |
+
response_data = {
|
108 |
+
"predicted_emotions": predicted_emotions,
|
109 |
+
"recognized_emotion": recognized_emotion,
|
110 |
+
}
|
111 |
+
response = Response(response_data, status=status.HTTP_200_OK)
|
112 |
+
response['Content-Type'] = 'image/jpeg'
|
113 |
+
response.content = output_image.getvalue()
|
114 |
+
return response
|
115 |
+
except Exception as e:
|
116 |
+
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
117 |
+
|
|
|
118 |
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|