图像分类三之在线web 服务

本文介绍了一个使用Flask框架部署的CNN模型图像分类服务,该服务能够接收上传的图片,利用预训练的CNN模型进行分类,并返回预测的类别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在线web后台flask 服务
cnn_flask_pre.py

-- coding: utf-8 --

“”"
用训练好的CNN模型做预测
模型文件为 cnn-t10-fine-best.model
“”"

from cnn_model_t7 import CNN_MODEL_T7
import os,shutil
from database import update_logotype_30m
from flask import Flask, render_template, jsonify, request, make_response, send_from_directory, abort,Response

import random
import json,os,base64,time,sys
import datetime


#from PictureSearchFaissWeb.picture_classify.cnn_model_t7  import CNN_MODEL_T7
#from PictureSearchFaissWeb.picture_classify.database import update_logotype_30m

model = CNN_MODEL_T7(include_top=True)
app = Flask(__name__)

basedir = os.path.abspath(os.path.dirname(__file__))
print("basedir" ,basedir)

def create_uuid(): 
        nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M%S");  
        randomNum = random.randint(0, 100); 
        if randomNum <= 10:
            randomNum = str(0) + str(randomNum);
        uniqueNum = str(nowTime) + str(randomNum);
        return uniqueNum;





@app.route("/health")
def health():
    result = {'status': 'UP'}
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/getUser")
def getUser():
    result = {'username': 'python', 'password': 'python'}
    return Response(json.dumps(result), mimetype='application/json')

@app.route("/hello")
def hello():
    result = {'username': 'admin', 'password': 'admin'}
    return Response(json.dumps(result), mimetype='application/json')




@app.route('/')  
@app.route('/upload')
def upload_test():
  return render_template('image-classify.html')

@app.route('/classify_upload',methods=['POST'] )
def cnn_pred():
  
    #path = "/opt/service/PictureSearchFaissWeb/src/PictureSearchFaissWeb/picture_classify/classify_upload/2019070907061019.jpg"
   
    category = ['en', 'image', 'mixed_en', 'mixed_zh', 'mixed_zh_en', 'zh', 'zh_en']
 
   
    image_file =  request.files['image']
        
     
    file_name = create_uuid() +'.' + image_file.name.split('.')[-1]    

    file_dir = os.path.join(basedir, "classify_upload")
    if not os.path.exists(file_dir):
        os.makedirs(file_dir)

    
    print('filename' ,file_name)

    # 将它用写入一个图片文件即可保存

    image_file.save( file_dir+'/' +file_name )  
    
  
    file_path = file_dir +'/' + file_name
    print('file_path' ,file_path)

    logotype = model.predict_classes(file_path)
     
    print('logotype' ,logotype)    
    body_data = {}   
    image_type = category[logotype]    
 
    body_data['type']=image_type;
    body_json = json.dumps(body_data)
    
    #return  jsonify(body_json)
    return image_type 
if __name__ =='__main__':
     
     app.run('172.16.1.141',8084,debug=True)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值