AI实战:上海垃圾分类系列(二)之快速搭建垃圾分类模型后台服务

前言

AI实战:上海垃圾分类系列(一)之快速搭建垃圾分类模型
AI实战:上海垃圾分类系列(二)之快速搭建垃圾分类模型后台服务
AI实战:上海垃圾分类系列(三)之快速搭建垃圾分类智能问答机器人

前一篇文章 AI实战:上海垃圾分类系列之快速搭建垃圾分类模型 ,本文在其之上搭建了一个基于Django、REST的web服务,实现了从浏览器中上传图片做垃圾分类的功能。

2019上海市生活垃圾按照以下标准分类!:http://sh.bendibao.com/zffw/2019225/202535.shtm

上海生活垃圾分类标准及投放要求:https://www.sohu.com/a/163450869_688983

模型介绍

垃圾分类模型详情请查看这里:AI实战:上海垃圾分类系列之快速搭建垃圾分类模型

环境

  • Python 3.5
  • Django 1.10.8
  • Django Rest Framework 3.5
  • Pillow

安装环境

pip install -r requirements.txt

垃圾分类识别模型

具体见:./refuse_recognize_service/refuse_recognize/readme.md

配置修改

  • 1、修改checkpoints

    ./refuse_recognize_service/refuse_recognize/runs/checkpoints
    把路径改为自己的全路径

  • 2、修改html样式:

    ./refuse_recognize_service/imageupload_frontend/static/index.html

核心代码

models.py :

import os
import uuid

from PIL import Image
from django.db import models
from django.conf import settings

import sys, time
sys.path.append('./refuse_recognize/')
sys.path.append('./refuse_recognize/textcnn/')
from refuse import *

refuse_classification = RefuseRecognize()#加载垃圾识别模型


def scramble_uploaded_filename(instance, filename):
    """
    Scramble / uglify the filename of the uploaded file, but keep the files extension (e.g., .jpg or .png)
    :param instance:
    :param filename:
    :return:
    """
    extension = filename.split(".")[-1]
    return "{}.{}".format(uuid.uuid4(), extension)


def create_thumbnail(input_image, thumbnail_size=(500, 500)):
    """
    Create a thumbnail of an existing image
    :param input_image:
    :param thumbnail_size:
    :return:
    """
    # make sure an image has been set
    if not input_image or input_image == "":
        return

    # open image
    image = Image.open(input_image)

    # use PILs thumbnail method; use anti aliasing to make the scaled picture look good
    image.thumbnail(thumbnail_size, Image.ANTIALIAS)

    # parse the filename and scramble it
    filename = scramble_uploaded_filename(None, os.path.basename(input_image.name))
    arrdata = filename.split(".")
    # extension is in the last element, pop it
    extension = arrdata.pop()
    basename = "".join(arrdata)
    # add _thumb to the filename
    new_filename = basename + "_thumb." + extension

    # save the image in MEDIA_ROOT and return the filename
    image.save(os.path.join(settings.MEDIA_ROOT, new_filename))

    return new_filename


class UploadedImage(models.Model):
    """
    Provides a Model which contains an uploaded image aswell as a thumbnail
    """
    image = models.ImageField("Uploaded image", upload_to=scramble_uploaded_filename)

    # thumbnail
    thumbnail = models.ImageField("Thumbnail of uploaded image", blank=True)

    # title and description
    title = models.CharField("Title of the uploaded image", max_length=800, default="Unknown Picture")
    description = models.TextField("Description of the uploaded image", default="")


    def __str__(self):
        return self.title

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        """
        On save, generate a new thumbnail
        :param force_insert:
        :param force_update:
        :param using:
        :param update_fields:
        :return:
        """
        # generate and set thumbnail or none
        self.thumbnail = create_thumbnail(self.image)
        

        # 垃圾类别识别
        image_data = Image.open(self.image)
        img_path = str(time.time) + '.png'
        while os.path.exists(img_path):
            img_path = str(time.time) + '.png'
        image_data.save(img_path, 'png')
        
        image_data = tf.gfile.FastGFile(img_path, 'rb').read()
        classify_res = refuse_classification.recognize_image(image_data)
        self.title = classify_res
        os.remove(img_path)

        # force update as we just changed something
        super(UploadedImage, self).save(force_update=force_update)

HTML显示样式

index.html :

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
    <!-- Include Angular and several angular libraries -->
    <script src="node_modules/angular/angular.js"></script>
    <script src="node_modules/angular-resource/angular-resource.min.js"></script>
    <script src="node_modules/ng-file-upload/dist/ng-file-upload.min.js"></script>
    <script src="node_modules/angular-animate/angular-animate.min.js"></script>
    <script src="node_modules/angularjs-toaster/toaster.js"></script>

    <!-- Include our app -->
    <script src="js/app.js"></script>
    <!-- Include our own controllers, factories, directives, etc... -->
    <script src="js/images.rest.js"></script>

    <!-- Include Bootstrap CSS -->
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">

    <!-- Include AngularJS Toaster CSS -->
    <link rel="stylesheet" href="node_modules/angularjs-toaster/toaster.min.css">
    <title>垃圾分类智能识别</title>
</head>
<body>

<div class="container-fluid">
    <div ng-app="imageuploadFrontendApp" ng-controller="MainCtrl">
        <toaster-container></toaster-container>
        <div class="panel panel-default">
            <div class="panel-body">
                <form class="form" name="form" ng-submit="uploadImage()">
                    <div class="form-group">
                        <h3 for="file">选择一张图片:</h3>
                        <input type="file" ngf-select ng-model="newImage.image" name="file"
                               class="form-control"
                               accept="image/*" ngf-max-size="10MB" required
                               ngf-model-invalid="errorFile">
                        <i ng-show="form.file.$error.maxSize">File too large
                            {{ errorFile.size / 1000000 | number:1 }} MB: max 10M</i>
                        <img ng-show="form.file.$valid" ngf-thumbnail="newImage.image" class="img-responsive" style="max-width: 50%">
                        <button class="btn btn-warning" ng-click="newImage.image = null" ng-show="newImage.image">Remove</button>
                    </div>
                    <button class="btn btn-primary" type="submit">
                        上传
                    </button>
                </form>
            </div>
        </div>

        <div ng-if="images.length == 0">
            请选择一张图片上传!
        </div>

        <!-- display the images -->
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-4" ng-repeat="image in images track by image.pk">
                <a href="{{ image.image }}">
                    <img class="img-responsive" ng-src="{{ image.thumbnail }}">
                    <p>{{ image.description }}</p>
                </a>
                <h3>垃圾类别:
                </h3>
                
                <textarea cols="50" rows="5" id="textareaid">{{ image.title }}</textarea>
                
                <h3>
                    <button class="btn btn-warning" ng-click="deleteImage(image)">删除</button>
                </h3>
            </div>
        </div>
    </div>
</div>
</body>
</html> 

开启服务

  • 服务

      cd refuse_recognize_service
      python manage.py migrate
      python manage.py runserver &  # 后台运行
    

效果展示

  • 在浏览器中打开 http://localhost:8000/static/index.html (我是在本机测试的,服务器测试的话修改localhost为相应的ip即可)。

  • 起始页面

1- 上传效果图

2

  • 点击上传后,垃圾分类结果效果图

垃圾分类结果
45
效果展示大概就是这样了,整体比较粗糙,识别准确率就不再优化了。
还有很多细节工作需要做,主要是垃圾类别标注数据、imagenet的结果的1000类中文对照优化等工作。

完整工程代码

完整的工程包括:
1、完整代码
2、完整的数据
3、完整的垃圾分类识别模型
4、文档

工程下载地址: 快速搭建垃圾分类模型后台服务

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值