AI展示框架(4):基于flask的图像场景识别web程序构建

2019-05-26 14:13:24 浏览数 (1)

图像场景识别是DL+计算机视觉处理的入门程序之一,因此在构建AI展示框架的第一步,则是实现基于flask的图像场景识别。

整个过程非常简单,步骤如下: 

(1)设置图像选择和上传的HTML脚本

代码语言:javascript复制
<div class="box box-primary">
				<form role="form" action="{{ url_for('cv_image_recognize') }}"  method="post"  enctype="multipart/form-data" >
			      <div class="box-body">
			      	<div class="form-group">
			          <label>图像场景识别模型:</label>
			          <select class="form-control" name='selectmodel'>
			            {% for modelname in modelnames %}
              			<option>{{ modelname }}</option>
              			{% endfor %}
			          </select>
			        </div>
			      	<div class="form-group">
			          <label>返回结果TopK值:</label>
			          <select class="form-control" name='topk'>
              			<option>1</option>
              			<option>3</option>
              			<option>5</option>
              			<option selected="selected" >10</option>
			          </select>
			        </div>
			        <div class="form-group">
			          <label for="exampleInputFile">选择一个图像(.jpg, .png, .bmp等)</label>
			          <input type="file" name=file id="exampleInputFile" value="选择图像" >
			   		</div>
			      </div>
			      <div class="box-footer">
			       	<input type="submit" value="上传并预测" name="predict"/>
			      </div>
			    </form>
     		</div>

(2)设置响应的程序

代码语言:javascript复制
    @app.route('/cv_image_recognize', methods=['GET', 'POST'])
    def cv_image_recognize():
        print('upload image and predict...')
        #print(request.method)
        if request.method == 'POST':            
            if 'file' not in request.files:
                print('No file part')
                return redirect(request.url)
            file = request.files['file']
            arch = request.form['selectmodel']
            topk = 5
            try:
                topk = int(request.form['topk'])
            except:
                topk = 5
            # 上传文件到static文件夹中
            uploadfile = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)            
            if file and func.allowed_file(file.filename):
                file.save(uploadfile)
            #with tempfile.NamedTemporaryFile() as temp:
            #    form.input_photo.data.save(temp)
            #    temp.flush()
            #image_binary = image.read()
            classlist,totaltime = imagenet_logits.RecognizeScene(ai_dir,arch, uploadfile, topk)#detect_objects(temp.name)
        
            photo_form = PhotoForm(request.form)
            return render_template('cv/image_recognize.html', header='图片场景识别',description='利用深度学习 imagenet来实现自然图像场景的分类识别,即what is in the picture。',
                               update_date='2019-04-15',current_user=current_user,
                               modelnames=model_list,
                               filename=file.filename, classlist=classlist,totaltime=totaltime)
        else:            
            return render_template('cv/image_recognize.html', header='图片场景识别',description='利用深度学习 imagenet来实现自然图像场景的分类识别,即what is in the picture。',
                               update_date='2019-04-15',current_user=current_user,
                               filename='',modelnames=model_list, result={})

(3)主要算法处理模块

采用pretrained-models.pytorch来实现,该程序集成了多种模型,是一个非常不错的图像识别框架。

具体实现效果如下所示:

在具体实现过程中,可以动态允许用户选择任意一种模型:

['alexnet', 'bninception', 'cafferesnet101', 'densenet121', 'densenet161', 'densenet169', 'densenet201', 'dpn107', 'dpn131', 'dpn68', 'dpn68b', 'dpn92', 'dpn98', 'fbresnet152', 'inceptionresnetv2', 'inceptionv3', 'inceptionv4', 'nasnetalarge', 'nasnetamobile', 'pnasnet5large', 'polynet', 'resnet101', 'resnet152', 'resnet18', 'resnet34', 'resnet50', 'resnext101_32x4d', 'resnext101_64x4d', 'se_resnet101', 'se_resnet152', 'se_resnet50', 'se_resnext101_32x4d', 'se_resnext50_32x4d', 'senet154', 'squeezenet1_0', 'squeezenet1_1', 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19', 'vgg19_bn', 'xception']

并支持用户设置返回的topk的个数。

0 人点赞