views.py
5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
from rest_framework import viewsets
import os
from django.http.request import QueryDict
from django.http import Http404
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, render
from api.models import Video, VideoFile
from api.serializers import VideoSerializer, VideoFileSerializer
from api import file_upload_path
import subprocess
import shlex
import json
# Create your views here.
import sys
sys.path.insert(0, "/home/jun/documents/univ/PKH_Project1/web/backend/yt8m/esot3ria")
import inference_pb
def with_ffprobe(filename):
result = subprocess.check_output(
f'ffprobe -v quiet -show_streams -select_streams v:0 -of json "{filename}"',
shell=True).decode()
fields = json.loads(result)['streams'][0]
duration = int(float(fields['duration']))
return duration
def index(request):
return render(request, template_name='index.html')
class VideoViewSet(viewsets.ModelViewSet):
queryset = Video.objects.all()
serializer_class = VideoSerializer
class VideoFileViewSet(viewsets.ModelViewSet):
queryset = VideoFile.objects.all()
serializer_class = VideoFileSerializer
class VideoFileUploadView(APIView):
parser_classes = (MultiPartParser, FormParser)
def get(self, request, format=None):
videoFiles = VideoFile.objects.all()
serializer = VideoFileSerializer(videoFiles, many=True)
return Response(serializer.data)
def post(self, req, *args, **kwargs):
# 동영상 길이
runTime = 0
# 요청된 데이터를 꺼냄( QueryDict)
new_data = req.data.dict()
# 요청된 파일 객체
file_name = req.data['file']
threshold = req.data['threshold']
# 저장될 파일의 풀path를 생성
new_file_full_name = file_upload_path(file_name.name)
# 새롭게 생성된 파일의 경로
file_path = '-'.join(new_file_full_name.split('-')[0:-1])
new_data['file_path'] = file_path
new_data['file_origin_name'] = req.data['file'].name
new_data['file_save_name'] = req.data['file']
new_query_dict = QueryDict('', mutable=True)
new_query_dict.update(new_data)
file_serializer = VideoFileSerializer(data = new_query_dict)
if file_serializer.is_valid():
file_serializer.save()
# 동영상 길이 출력
runTime = with_ffprobe('/'+file_serializer.data['file_save_name'])
print(runTime)
print(threshold)
process = subprocess.Popen(['./runMediaPipe.sh %s %s' %(file_serializer.data['file_save_name'],runTime,)], shell = True)
process.wait()
result = inference_pb.inference_pb('/tmp/mediapipe/features.pb', threshold)
return Response(result, status=status.HTTP_201_CREATED)
else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class VideoFileList(APIView):
def get_object(self, pk):
try:
return VideoFile.objects.get(pk=pk)
except VideoFile.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
video = self.get_object(pk)
serializer = VideoFileSerializer(video)
return Response(serializer.data)
def put(self, request, pk, format=None):
video = self.get_object(pk)
serializer = VideoFileSerializer(video, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None):
video = self.get_object(pk)
video.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
class FileListView(APIView):
def get(self, request):
data = {
"search": '',
"limit": 10,
"skip": 0,
"order": "time",
"fileList": [
{
"name": "1.png",
"created": "2020-04-30",
"size": 10234,
"isFolder": False,
"deletedDate": "",
},
{
"name": "2.png",
"created": "2020-04-30",
"size": 3145,
"isFolder": False,
"deletedDate": "",
},
{
"name": "3.png",
"created": "2020-05-01",
"size": 5653,
"isFolder": False,
"deletedDate": "",
},
]
}
return Response(data)
def post(self, request, format=None):
data = {
"isSuccess": True,
"File": {
"name": "test.jpg",
"created": "2020-05-02",
"deletedDate": "",
"size": 2312,
"isFolder": False
}
}
return Response(data)