【征稿计划第三期】AI识别森林火灾
wangwei8638 发布于2019-08 浏览:3163 回复:2
0
收藏
最后编辑于2022-04

【使用攻略】+【EasyDL图像分类】

EasyDL定制图像分类模型,可识别一张图中是否是某类物体/状态/场景,适用于图片中主体或者状态单一的场景。本文利用EasyDL创建一个模型,用于对森林火灾进行预警。为提高分辨能力,采用100张森林火灾和100张火烧云照片进行训练,标签分为fire和not_fire两类,下面简要介绍使用流程。

一.创建模型

1.进入官方主页,点击“开始训练”。

https://ai.baidu.com/easydl/image_classification

2.点击“创建模型”,输入名称等相关信息,如图示:

3.在侧边栏数据中心点击创建数据集,输入名称

图像分类的数据上传方式非常简单,只需要将所有准备好的图片按分类放在不同的文件夹里,同时将所有文件夹压缩为**.zip格式,在【创建数据集】页面直接上传即可。

4. 数据处理完后的图片如下,可以编辑名称、查看图片或继续上传。

二.训练模型

1.在侧边栏模型中心点击“训练模型”,选择此次训练的森林火灾模型,勾选应用类型中的云服务,训练方式为快速训练。

2. 添加训练数据,先选择数据集,再按分类选择数据集里的标签。

3.点击“开始训练”

三.校验模型

在侧边栏模型中心点击校验模型,上传用于测试的图片,右侧可以看到识别结果

四.发布模型

在侧边栏模型中心点击发布模型,输入服务名称及接口地址,点击提交申请,即可申请发布模型。

发布结果稍后可在我的模型中查看,差不多一个工作日就有结果。当然要得到预测准确的模型,需要不断的优化迭代,重新发布。

五.模型调用

Python3调用代码:

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib3
import certifi
import os
import requests
import base64
import json
from pprint import pprint
import time
#client_id 为官网获取的AK, client_secret 为官网获取的SK
api_key = '************'
secret_key = '*******************'
 
class FireRecognizer(object):
    def __init__(self, api_key, secret_key):
        self.access_token = self._get_access_token(api_key=api_key, secret_key=secret_key)
        self.API_URL = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/forest-fire ' + '?access_token=' \
                      + self.access_token
    #获取token
    @staticmethod
    def _get_access_token(api_key, secret_key):
        api = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \
            '&client_id={}&client_secret={}'.format(api_key, secret_key)
        rp = requests.post(api)
        if rp.ok:
            rp_json = rp.json()
            return rp_json['access_token']
        else:
            print('=> Error in get access token!')
    def get_result(self, params):
        rp = requests.post(self.API_URL, data=params)
        if rp.ok:
#            print('=> Success! got result: ')
            rp_json = rp.json()
#            pprint(rp_json)
            return rp_json
        else:
            print('=> Error! token invalid or network error!')
            print(rp.content)
            return None
    #识别森林火灾
    def detect(self, img_path):
        http=urllib3.PoolManager(cert_reqs = 'CERT_REQUIRED',ca_certs = certifi.where()) 
        f = open(img_path, 'rb')
        access_token='24.1349658a1f8aecd61c3c74ef8755bc74.2592000.1558857660.282335-16115229'
        url='https://aip.baidubce.com/rpc/2.0/ai_custom/v1/detection/ismoking?access_token='+access_token
        strover = '识别结果:'
        img_str = base64.b64encode(f.read())
        params={'image':''+str(img_str,'utf-8')+'','threshold':0.3}
        encoded_data = json.dumps(params).encode('utf-8')
#        print(encoded_data)
        request=http.request('POST', 
                      url,
                      body=encoded_data,
                      headers={'Content-Type':'application/json'})
        result = str(request.data,'utf-8')
        print(result)
 
if __name__ == '__main__':
    recognizer =FireRecognizer(api_key, secret_key)
    img = 'F:\paddle\fire\s111.jpg'
    recognizer.detect(img)

收藏
点赞
0
个赞
共2条回复 最后由用户已被禁言回复于2022-04
#3wangwei8638回复于2019-08
#2 果断叫小黑回复
这个没什么意义吧?实际发生火灾的时候你还去识别?

尽早预警,烧成亚马逊那样还有救吗

0
#2果断叫小黑回复于2019-08

这个没什么意义吧?实际发生火灾的时候你还去识别?

0
TOP
切换版块