Djangoで
python manege.py runserver
でアプリ(myapp)を起動します。ホーム画面(index.html)は画像をフォームにアップロードし、それが特定のディレクトリ('/Users/downloads/django_app/myapp/image/myapp'
)に保存される形式になっています。(アプリの形式の参考サイト:https://qiita.com/narupo/items/e3dbdd5d030952d10661、
https://qiita.com/okoppe8/items/86776b8df566a4513e96)
特定のディレクトリから画像を読み込んで、予測結果を返すCNNのコードがmain.pyにあります。
自分がやりたいこと:
フォームに画像を投稿した時、POST時の処理としてmain.pyを起動させ、その変数を受け取り、index.htmlにレンダリングする。
・質問
views.pyのPOST時の処理のとき、main.pyを実行させる方法はどうやるのでしょうか?
個人的にはmain.pyの予測結果を変数(LABEL)に格納して、import文main.pyを呼び出して、レンダリングすることを考えています
python
1# views.py 2 3from django.shortcuts import render, redirect 4from .forms import PhotoForm 5from .models import Photo 6 7def index(req): 8 if req.method == 'GET': 9 return render(req, 'myapp/index.html', { 10 'form': PhotoForm(), 11 }) 12 13 if req.method == 'POST': 14 ==POST時の処理==
python
1# index.html 2<form action="{% url 'index' %}" method="POST" enctype="multipart/form-data"> 3 {% csrf_token %} 4 {{ form }} 5 <input type="submit" value="投稿" /> 6</form>
python
1#main.py 2 3from __future__ import print_function 4from __future__ import absolute_import 5 6import warnings 7import os 8import numpy as np 9import tensorflow as tf 10from keras.optimizers import SGD 11from keras import layers 12from keras.preprocessing import image 13from keras.applications.imagenet_utils import decode_predictions 14from keras.models import Model 15from keras.layers import Activation,AveragePooling2D, BatchNormalization, Concatenate, Conv2D,Dense 16from keras.layers import GlobalAveragePooling2D, GlobalMaxPooling2D, Input,Lambda, MaxPooling2D 17from keras import backend as K 18from keras import metrics 19from keras.preprocessing import image 20import matplotlib.pyplot as plt 21from keras.callbacks import EarlyStopping 22tf.logging.set_verbosity(tf.logging.ERROR) 23 24 25def InceptionResNetV2(img_input,include_top=True, 26 pooling=None, classes=13): 27 28 # Stem block: 35 x 35 x 192 29 x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid') 30 x = conv2d_bn(x, 32, 3, padding='valid') 31 x = conv2d_bn(x, 64, 3) 32 x = MaxPooling2D(3, strides=2)(x) 33 x = conv2d_bn(x, 80, 1, padding='valid') 34 x = conv2d_bn(x, 192, 3, padding='valid') 35 x = MaxPooling2D(3, strides=2)(x) 36 37 # Mixed 5b (Inception-A block): 35 x 35 x 320 38 branch_0 = conv2d_bn(x, 96, 1) 39 branch_1 = conv2d_bn(x, 48, 1) 40 branch_1 = conv2d_bn(branch_1, 64, 5) 41 branch_2 = conv2d_bn(x, 64, 1) 42 branch_2 = conv2d_bn(branch_2, 96, 3) 43 branch_2 = conv2d_bn(branch_2, 96, 3) 44 branch_pool = AveragePooling2D(3, strides=1, padding='same')(x) 45 branch_pool = conv2d_bn(branch_pool, 64, 1) 46 branches = [branch_0, branch_1, branch_2, branch_pool] 47 channel_axis = 1 if K.image_data_format() == 'channels_first' else 3 48 x = Concatenate(axis=channel_axis, name='mixed_5b')(branches) 49 50 # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320 51 for block_idx in range(1, 11): 52 x = inception_resnet_block(x, 53 scale=0.17, 54 block_type='block35', 55 block_idx=block_idx) 56 57 # Mixed 6a (Reduction-A block): 17 x 17 x 1088 58 branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid') 59 branch_1 = conv2d_bn(x, 256, 1) 60 branch_1 = conv2d_bn(branch_1, 256, 3) 61 branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid') 62 branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x) 63 branches = [branch_0, branch_1, branch_pool] 64 x = Concatenate(axis=channel_axis, name='mixed_6a')(branches) 65 66 # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088 67 for block_idx in range(1, 21): 68 x = inception_resnet_block(x, 69 scale=0.1, 70 block_type='block17', 71 block_idx=block_idx) 72 73 # Mixed 7a (Reduction-B block): 8 x 8 x 2080 74 branch_0 = conv2d_bn(x, 256, 1) 75 branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid') 76 branch_1 = conv2d_bn(x, 256, 1) 77 branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid') 78 branch_2 = conv2d_bn(x, 256, 1) 79 branch_2 = conv2d_bn(branch_2, 288, 3) 80 branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid') 81 branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x) 82 branches = [branch_0, branch_1, branch_2, branch_pool] 83 x = Concatenate(axis=channel_axis, name='mixed_7a')(branches) 84 85 # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080 86 for block_idx in range(1, 10): 87 x = inception_resnet_block(x, 88 scale=0.2, 89 block_type='block8', 90 block_idx=block_idx) 91 x = inception_resnet_block(x, 92 scale=1., 93 activation=None, 94 block_type='block8', 95 block_idx=10) 96 97 # Final convolution block: 8 x 8 x 1536 98 x = conv2d_bn(x, 1536, 1, name='conv_7b') 99 100 # Classification block 101 x = GlobalAveragePooling2D(name='avg_pool')(x) 102 return Dense(classes, activation='softmax', name='predictions')(x) 103 104input_ = Input(batch_shape=(None,100,100,3)) 105output_ = InceptionResNetV2(img_input=input_) 106model = Model(input_, output_, name='inception_resnet_v2') 107model.load_weights('/Users/downloads/Downloads/django_app/myapp/weght_dir/model.h5') 108model.compile(optimizer=SGD(decay=0.1, momentum=0.9, nesterov=True), 109 loss='categorical_crossentropy', 110 metrics=['accuracy']) 111img_path = '/Users/downloads/django_app/myapp/image/myapp/images.jpeg' 112img = image.load_img(img_path, target_size=(100, 100)) 113x = image.img_to_array(img) 114x = np.expand_dims(x, axis=0) 115x = preprocess_input(x) 116preds = model.predict(x) 117print('Predicted:', decode_predictions(preds))
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。