当前位置:   article > 正文

python烟火检测pytorch抽烟检测EfficientNet_烟雾和明火检测pytorch

烟雾和明火检测pytorch

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

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

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. '''
  4. '''
  5. import json
  6. from PIL import Image, ImageDraw, ImageFont
  7. import torch
  8. from torch import nn
  9. from torchvision import transforms
  10. from efficientnet_pytorch import FireSmokeEfficientNet
  11. import collections
  12. image_dir = './tests/5.jpg'
  13. model_para = collections.OrderedDict()
  14. model = FireSmokeEfficientNet.from_arch('efficientnet-b0')
  15. # out_channels = model._fc.in_features
  16. model._fc = nn.Linear(1280, 3)
  17. print(model)
  18. modelpara = torch.load('./checkpoint.pth.tar',map_location='cpu')
  19. # print(modelpara['state_dict'].keys())
  20. for key in modelpara['state_dict'].keys():
  21. # print(key[7:])
  22. # newkey = model_para[key.split('.',2)[-1]]
  23. # print(newkey)
  24. model_para[key[7:]] =modelpara['state_dict'][key]
  25. # print(model_para.keys())
  26. # 训练模型转换
  27. model.load_state_dict(model_para)
  28. # Preprocess image
  29. tfms = transforms.Compose([transforms.Resize(224), transforms.ToTensor(),
  30. transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),])
  31. image = Image.open(image_dir)
  32. img = tfms(image).unsqueeze(0)
  33. print(img.shape) # torch.Size([1, 3, 224, 224])
  34. # Load ImageNet class names
  35. labels_map = json.load(open('examples/simple/fire_smoke_map.txt'))
  36. labels_map = [labels_map[str(i)] for i in range(3)]
  37. # Classify
  38. model.eval()
  39. with torch.no_grad():
  40. outputs = model(img)
  41. draw = ImageDraw.Draw(image)
  42. font = ImageFont.truetype('simkai.ttf', 30)
  43. # Print predictions
  44. print('-----')
  45. cout = 0
  46. for idx in torch.topk(outputs, k=2).indices.squeeze(0).tolist():
  47. cout += 1
  48. prob = torch.softmax(outputs, dim=1)[0, idx].item()
  49. print('{label:<75} ({p:.2f}%)'.format(label=labels_map[idx], p=prob*100))
  50. position = (10, 30*cout - 20)
  51. text = '{label:<5} :{p:.2f}%'.format(label=labels_map[idx], p=prob*100)
  52. draw.text(position, text, font=font, fill="#ff0000", spacing=0, align='left')
  53. image.save('results/result_{}'.format(image_dir.split('/')[-1]))

 

 

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/626622
推荐阅读
相关标签
  

闽ICP备14008679号