Browse Source

Phase1コミット

master
first_user 3 years ago
parent
commit
10f94a4fe2
  1. 4
      .gitignore
  2. 4
      detect_app/apps.py
  3. 4
      detect_app/detect.py
  4. 4
      detect_app/forms.py
  5. 41
      detect_app/templates/detect_app/upload.html
  6. 6
      detect_app/upload.py
  7. 7
      detect_app/urls.py
  8. 22
      detect_app/views.py
  9. 1
      sample_site/settings.py
  10. 1
      sample_site/urls.py

4
.gitignore

@ -1,3 +1,5 @@
env/
db.sqlite3
*.pyc
*.pyc
detect_app/static/detect_app/images/input.jpg
detect_app/static/detect_app/images/output.jpg

4
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'

4
detect_app/detect.py

@ -0,0 +1,4 @@
import shutil
def detect(input, output):
shutil.copy2(input, output)

4
detect_app/forms.py

@ -0,0 +1,4 @@
from django import forms
class UploadFileForm(forms.Form):
file = forms.FileField()

41
detect_app/templates/detect_app/upload.html

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>物体検出</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center">
<h1>物体検出</h1>
</div>
<div class="row justify-content-center">
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div>
<label>画像ファイル</label>
</div>
<div>
{{form.file}}
</div>
{% if result %}
<div>
<img src="{{ result }}" class="img-responsive">
</div>
{% endif %}
<hr>
{% for error in form.file.errors %}
{{error}}
{% endfor %}
<button class="btn btn-primary btn-lg btn-block" type="submit">送信</button>
</form>
</div>
</div>
</body>
</html>

6
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)

7
detect_app/urls.py

@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path('', views.upload_file, name='index'),
]

22
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})

1
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',

1
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),
]
Loading…
Cancel
Save