python烟火检测pytorch抽烟检测EfficientNet

python烟火检测pytorch火焰抽烟正常检测EfficientNet

烟火检测功能主要应用于建筑工地、工业园区、仓库、以及其他易燃易爆场景的监控。该功能适用于白天或者光照条件较好的夜晚环境,不适用于光照条件较差、遮挡严重的场景。具体应用示例如下:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''

'''
import json
from PIL import Image, ImageDraw, ImageFont
import torch
from torch import nn
from torchvision import transforms
from efficientnet_pytorch import FireSmokeEfficientNet
import collections
image_dir = './tests/5.jpg'
model_para = collections.OrderedDict()
model = FireSmokeEfficientNet.from_arch('efficientnet-b0')
# out_channels = model._fc.in_features
model._fc = nn.Linear(1280, 3)
print(model)
modelpara = torch.load('./checkpoint.pth.tar',map_location='cpu')
# print(modelpara['state_dict'].keys())
for key in modelpara['state_dict'].keys():
    # print(key[7:])
    # newkey = model_para[key.split('.',2)[-1]]
    # print(newkey)
    model_para[key[7:]] =modelpara['state_dict'][key]

# print(model_para.keys())
# 训练模型转换



model.load_state_dict(model_para)

# Preprocess image
tfms = transforms.Compose([transforms.Resize(224), transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),])
image = Image.open(image_dir)
img = tfms(image).unsqueeze(0)
print(img.shape) # torch.Size([1, 3, 224, 224])

# Load ImageNet class names
labels_map = json.load(open('examples/simple/fire_smoke_map.txt'))
labels_map = [labels_map[str(i)] for i in range(3)]

# Classify
model.eval()
with torch.no_grad():
    outputs = model(img)

draw = ImageDraw.Draw(image)
font = ImageFont.truetype('simkai.ttf', 30)
# Print predictions
print('-----')
cout = 0
for idx in torch.topk(outputs, k=2).indices.squeeze(0).tolist():
    cout += 1
    prob = torch.softmax(outputs, dim=1)[0, idx].item()
    print('{label:<75} ({p:.2f}%)'.format(label=labels_map[idx], p=prob*100))
    position = (10, 30*cout - 20)
    text = '{label:<5} :{p:.2f}%'.format(label=labels_map[idx], p=prob*100)
    draw.text(position, text, font=font, fill="#ff0000", spacing=0, align='left')

image.save('results/result_{}'.format(image_dir.split('/')[-1]))

 

 

 python烟火检测pytorch抽烟检测EfficientNet_哔哩哔哩_bilibili

https://download.csdn.net/download/babyai996/85094931

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

babyai997

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

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

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

打赏作者

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

抵扣说明:

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

余额充值