diff --git a/.gitignore b/.gitignore index c477410..5279a05 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ env/ db.sqlite3 -*.pyc \ No newline at end of file +*.pyc +detect_app/static/detect_app/images/input.jpg +detect_app/static/detect_app/images/output.jpg diff --git a/detect_app/apps.py b/detect_app/apps.py index 89cb6b2..fa83d05 100644 --- a/detect_app/apps.py +++ b/detect_app/apps.py @@ -3,3 +3,7 @@ from django.apps import AppConfig class DetectAppConfig(AppConfig): name = 'detect_app' + image_filebase = 'detect_app/static/detect_app/images/' + image_urlbase = '/static/detect_app/images/' + image_file_input = 'input.jpg' + image_file_output = 'output.jpg' diff --git a/detect_app/detect.py b/detect_app/detect.py new file mode 100644 index 0000000..498cda0 --- /dev/null +++ b/detect_app/detect.py @@ -0,0 +1,4 @@ +import shutil + +def detect(input, output): + shutil.copy2(input, output) diff --git a/detect_app/forms.py b/detect_app/forms.py new file mode 100644 index 0000000..21e602d --- /dev/null +++ b/detect_app/forms.py @@ -0,0 +1,4 @@ +from django import forms + +class UploadFileForm(forms.Form): + file = forms.FileField() \ No newline at end of file diff --git a/detect_app/templates/detect_app/upload.html b/detect_app/templates/detect_app/upload.html new file mode 100644 index 0000000..2b7c823 --- /dev/null +++ b/detect_app/templates/detect_app/upload.html @@ -0,0 +1,41 @@ + + + + + + + 物体検出 + + + + +
+
+

物体検出

+
+
+
+ {% csrf_token %} +
+ +
+
+ {{form.file}} +
+ {% if result %} +
+ +
+ {% endif %} +
+ {% for error in form.file.errors %} + {{error}} + {% endfor %} + +
+
+
+ + + \ No newline at end of file diff --git a/detect_app/upload.py b/detect_app/upload.py new file mode 100644 index 0000000..0701163 --- /dev/null +++ b/detect_app/upload.py @@ -0,0 +1,6 @@ +from .apps import DetectAppConfig + +def handle_uploaded_file(f, output): + with open(output, 'wb') as destination: + for chunk in f.chunks(): + destination.write(chunk) \ No newline at end of file diff --git a/detect_app/urls.py b/detect_app/urls.py new file mode 100644 index 0000000..0a16e13 --- /dev/null +++ b/detect_app/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path('', views.upload_file, name='index'), +] diff --git a/detect_app/views.py b/detect_app/views.py index 91ea44a..10b25b3 100644 --- a/detect_app/views.py +++ b/detect_app/views.py @@ -1,3 +1,23 @@ +from django.http import HttpResponseRedirect from django.shortcuts import render +from .apps import DetectAppConfig +from .forms import UploadFileForm +from .upload import handle_uploaded_file +from .detect import detect -# Create your views here. + +def upload_file(request): + result = None + if request.method == 'POST': + form = UploadFileForm(request.POST, request.FILES) + if form.is_valid(): + input_path = DetectAppConfig.image_filebase + DetectAppConfig.image_file_input + output_path = DetectAppConfig.image_filebase + DetectAppConfig.image_file_output + output_url = DetectAppConfig.image_urlbase + DetectAppConfig.image_file_output + + handle_uploaded_file(request.FILES['file'], input_path) + detect(input_path, output_path) + result = output_url + else: + form = UploadFileForm() + return render(request, 'detect_app/upload.html', {'form': form, 'result': result}) diff --git a/sample_site/settings.py b/sample_site/settings.py index f913827..9f50423 100644 --- a/sample_site/settings.py +++ b/sample_site/settings.py @@ -31,6 +31,7 @@ ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ + 'detect_app.apps.DetectAppConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/sample_site/urls.py b/sample_site/urls.py index 2e338aa..49f29ba 100644 --- a/sample_site/urls.py +++ b/sample_site/urls.py @@ -18,5 +18,6 @@ from django.urls import include, path urlpatterns = [ path('sample_app/', include('sample_app.urls')), + path('detect_app/', include('detect_app.urls')), path('admin/', admin.site.urls), ]