北京 上海巡回站 | NVIDIA DLI深度学习培训
2018年1月26/1月12日

NVIDIA 深度学习学院 带你快速进入火热的DL领域
阅读全文                                                
>

正文共6736个字,5张图,预计阅读时间17分钟。

前一段时间做企业的智能安全项目,我们在面对一些问题时,大胆采用深度学习的方法,解决传统算法和统计学算法不好实现的问题,今天就和大家分享一下,如何解决通过视频监控检测工人是否佩戴安全帽的深度学习算法。

安全帽检测

企业安全智能监控画面

01

概述

对于图像识别,采用传统的算法(opencv的一些算法),判断形状、颜色等等,我们在实验室和办公场所做测试,效果还不错,和容易识别出来。 一旦到了工业现场,图像完全不行,连人和车都识别不出来。在不同光线下不论采用什么颜色空间(RGB、HSV什么)都无法分离出合理的色彩,更不要提判断和检测了。有感于实际的现场环境,决定放弃传统的算法,拿起深度学习的工具,来搞定这个项目。

02

数据准备

高大上的人工智能背后,一定是苦逼的数据准备,深度学习的模型,需要成千上万的训练和测试数据,这些数据的采集和整理,还有数据的清洗都是体力活啊。

当然,我还是没傻到一张张去拍照片。我通过现场的摄像头做了视频采集,然后拿到录像,做了一个代码从录像中找到人,再把人的上半部分处理一下,变成标准格式的图片。这样子,2-3天的录像就可以产生几十万张图片了,看来训练集的问题解决了。

# -*- coding:utf-8 -*-
 # 录像转换为图片
from time import gmtime, strftime
import cv2
videoFile = '/media/kingstar/kingstardata/safety_eyes/nohatdata/7.mp4'
cap = cv2.VideoCapture(videoFile)
cap.set(3,640)
cap.set(4,480)
 while(True):    
ret, frame = cap.read()    
img = frame    
cv2.imshow('my', img)    
f = strftime("%Y%m%d%H%M%S.jpg", gmtime())    
cv2.imwrite('output/'+ f, img)  
if cv2.waitKey(1) & 0xFF == ord('q'):        
break  
 if img.size == 0:        
break
cap.release
cv2.destroyAllWindows()

采用SSD的算法(用于物体检测的深度学习主流算法之一)检测出图片中的人。

# -*-coding: utf-8-*-
from keras.preprocessing import image
from keras.applications.imagenet_utils
import preprocess_input from scipy.misc
import imread
import numpy as np
from ssd import SSD300
from ssd_utils import BBoxUtility
import matplotlib.pyplot as plt
import cv2
from os import listdir
 voc_classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle',              
 'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable',              
 'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant',              
 'Sheep', 'Sofa', 'Train', 'Tvmonitor']
NUM_CLASSES = len(voc_classes) + 1
input_shape=(300, 300, 3)
model = SSD300(input_shape, num_classes=NUM_CLASSES) model.load_weights('weights_SSD300.hdf5', by_name=True)
bbox_util = BBoxUtility(NUM_CLASSES)
 def ssd(img_path):    
global oPath    
inputs = []    
images = []    
#img_path = 'test02.jpg'    
img0 = cv2.imread(img_path)    
img = image.load_img(img_path, target_size=(300, 300))    
img = image.img_to_array(img)  
 images.append(imread(img_path))    
inputs.append(img.copy())    
inputs = preprocess_input(np.array(inputs))    
preds = model.predict(inputs, batch_size=1, verbose=1)    
results = bbox_util.detection_out(preds)    
#print results    
# Parse the outputs.    
for i, img in enumerate(images):        
det_label = results[i][:, 0]        
det_conf = results[i][:, 1]        
det_xmin = results[i][:, 2]        
det_ymin = results[i][:, 3]        
det_xmax = results[i][:, 4]        
det_ymax = results[i][:, 5]        
#print i,det_label,det_conf        
# Get detections with confidence higher than 0.6.        
top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.5]        
top_conf = det_conf[top_indices]        
top_label_indices = det_label[top_indices].tolist()        
top_xmin = det_xmin[top_indices]        
top_ymin = det_ymin[top_indices]        
top_xmax = det_xmax[top_indices]        
top_ymax = det_ymax[top_indices]      
 #colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()        
#plt.imshow(img / 255.)        
#currentAxis = plt.gca()        
#print top_label_indices        
#print top_conf        
#print top_conf.shape[0]        
for i0 in range(top_conf.shape[0]):            
xmin = int(round(top_xmin[i0] * img.shape[1]))

ymin = int(round(top_ymin[i0] * img.shape[0]))            
xmax = int(round(top_xmax[i0] * img.shape[1]))            
ymax = int(round(top_ymax[i0] * img.shape[0]))

score = top_conf[i0]          
 label = int(top_label_indices[i0])            
label_name = voc_classes[label - 1]            
#display_txt = '{:0.2f}, {}'.format(score, label_name)            
#coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1            
#color = colors[label]            
#currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))            
#currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})            print label_name,score,xmin,ymin,xmax,ymax            
fileStr0 = img_path.split('.')[-2]            
fileStr0 = fileStr0.split('/')[-1]            
if label_name == 'Person':                
fileStr = '%s/Person5/%s.%d.jpg' %(oPath,fileStr0,i0+1)                
im = img0[ymin:ymax,xmin:xmax]                
r = cv2.imwrite(fileStr,im)                
print 'Person0',fileStr            
if label_name == 'Car1' or label_name == 'Motorbike1':                
fileStr = '%s/Car/%s.%d.jpg' %(oPath,fileStr0,i0+1)                
im = img0[ymin:ymax,xmin:xmax]                
r = cv2.imwrite(fileStr,im)                
print 'Car0',fileStr        
#plt.show()        
#cv2.imshow('im', im)        
#cv2.waitKey(0) if __name__ == "__main__":    img_path = 'test02.jpg'    
mPath = '/media/kingstar/kingstardata/safety_eyes/baidu5'    
oPath = '/media/kingstar/kingstardata/safety_eyes/out'    
trainFileList = listdir(mPath)    
m =len(trainFileList)    
print m    
for i in range(m):        
fileNameStr = trainFileList[i]        
fileStr = fileNameStr.split('.')[-2]        
print i,fileNameStr,fileStr

fileNameStr = '%s/%s' % (mPath,fileNameStr)        
print 'step:%d/%d' % (i,m)        
ssd(fileNameStr)

这样就可以建立自己的训练集:

train和test (带不带帽子的标注需要人工去做... 这个还是很苦逼)

训练集

03

搭建模型

考虑到标准的图片只有128*128,特征不是很多,就动手搭建一个不算深的深度学习模型,采用卷积神经网络处理图形特征,搞过cnn的同学会觉得so easy。

model = Sequential() model.add(Convolution2D(32, 3, 3, input_shape=( img_width, img_height, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy',              optimizer='rmsprop',              metrics=['accuracy'])

这是个只有三层的卷积神经网络,我们就拿这个模型进行训练和学习吧。

04

训练神经网络

深度学习的训练是极其需要强大的算力的,多亏我们的模型较小,另外我们还DIY了一台深度学习服务器,有了强大的GPU做运算。

经过了一晚上的训练,终于出了结果,数据上还不错,识别率竟然到了95%以上。

具体代码如下:

# -*-coding: utf-8-*-
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers
import Activation, Dropout, Flatten, Dense
 # dimensions of our images.
img_width, img_height = 128, 128
 train_data_dir = '/media/kingstar/kingstardata/data/train'
validation_data_dir = '/media/kingstar/kingstardata/data/test'
nb_train_samples = 4000
nb_validation_samples = 800
nb_epoch = 60 model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=( img_width, img_height, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy',              
optimizer='rmsprop',              
metrics=['accuracy'])
train_datagen = ImageDataGenerator(        rescale=1./255,        
shear_range=0.2,        
zoom_range=0.2,        
horizontal_flip=True)
 test_datagen = ImageDataGenerator(rescale=1./255)
 train_generator = train_datagen.flow_from_directory(        
train_data_dir,        
target_size=(img_width, img_height),        
batch_size=32,      
 class_mode='binary')
 validation_generator = test_datagen.flow_from_directory(        
validation_data_dir,        
target_size=(img_width, img_height),        
batch_size=32,        
class_mode='binary')
 model.fit_generator(        
train_generator,        
samples_per_epoch=nb_train_samples,        
nb_epoch=nb_epoch,        
validation_data=validation_generator,        
nb_val_samples=nb_validation_samples)

model.save('trianHat12801.h5')

现场

05

结论

通过简单的cnn模型和一个小规模的数据集的训练,基本上达到了目标。

不过在实际测试用的识别率还是比较低,感觉还是无法直接用于生产环境。

没关系,下一步我们会采用成熟的模型vgg或resnet,在这个模型后端做修改,进行调优和训练,另外提高训练集的数量和进一步做数据清洗,已达到可以直接在生产环境上使用的目标。

原文链接:https://www.jianshu.com/p/2101f5e5e577

查阅更为简洁方便的分类文章以及最新的课程、产品信息,请移步至全新呈现的“LeadAI学院官网”:

www.leadai.org

请关注人工智能LeadAI公众号,查看更多专业文章

大家都在看


LSTM模型在问答系统中的应用

基于TensorFlow的神经网络解决用户流失概览问题

最全常见算法工程师面试题目整理(一)

最全常见算法工程师面试题目整理(二)

TensorFlow从1到2 | 第三章 深度学习革命的开端:卷积神经网络

装饰器 | Python高级编程

今天不如来复习下Python基础

通过深度学习实现安全帽佩戴的检测相关推荐

  1. 深度学习实现安全帽佩戴的检测

    向AI转型的程序员都关注了这个号

  2. 基于深度学习的安全帽检测系统(YOLOv5清新界面版,Python代码)

    摘要:安全帽检测系统用于自动化监测安全帽佩戴情况,在需要佩戴安全帽的场合自动安全提醒,实现图片.视频和摄像头等多种形式监测.在介绍算法原理的同时,给出Python的实现代码.训练数据集,以及PyQt的 ...

  3. 基于AI深度学习的安全帽检测算法,如何应用在实际场景中?

    安全帽是建筑业.制造业等企业生产中非常重要的劳保工具,因未佩戴安全帽而导致的安全事故也引发大量关注.所以,实时检测工作人员的安全帽佩戴状况,成为企业安全生产监管中不容忽视的环节. 基于AI深度学习的目 ...

  4. 安全帽佩戴识别检测 YOLOv5

    安全帽佩戴识别检测通过Python基于YOLOv5深度学习框架模型,对现场画面中进行分析检测.Yolo模型采用预定义预测区域的方法来完成目标检测,具体而言是将原始图像划分为 7x7=49 个网格(gr ...

  5. 毕业设计-基于深度学习的安全帽智能识别系统

    目录 前言 课题背景和意义 实现技术思路 一.系统架构 二.基于深度学习的目标检测算法实现 三.交互设计与实现 四.基于深度学习的安全帽智能识别系统测试 五.总结 实现效果图样例 最后 前言

  6. 基于深度学习的口罩识别与检测PyTorch实现

    基于深度学习的口罩识别与检测PyTorch实现 1. 设计思路 1.1 两阶段检测器:先检测人脸,然后将人脸进行分类,戴口罩与不戴口罩. 1.2 一阶段检测器:直接训练口罩检测器,训练样本为人脸的标注 ...

  7. 基于深度学习识别模型的缺陷检测

    根据读者反映,咱们的这个PCB素材设置的不对,应该是没有漆,铜线等等,应该是黑白的.额,具体我也知道,但没去过工厂,实在很难获得这些素材... 所以就当是一次瑕疵识别的实践,具体的数据集你可以自己定义 ...

  8. 【深度学习】深入浅出YOLOv3目标检测算法和实现(图片和视频)

    [深度学习]深入浅出YYOLOv3目标检测算法(图片和视频) 文章目录 1 概述 2 一个全卷积神经网络--Darknet-53 3 解释输出 4 代码实现4.1 导入项目4.2 执行脚本4.3 预测 ...

  9. 基于深度学习的安卓恶意应用检测----------android manfest.xml + run time opcode, use 深度置信网络(DBN)...

    基于深度学习的安卓恶意应用检测 from:http://www.xml-data.org/JSJYY/2017-6-1650.htm 苏志达, 祝跃飞, 刘龙     摘要: 针对传统安卓恶意程序检测 ...

最新文章

  1. docker mysql详解_Docker轻松入门(详解)
  2. 每周算法讲堂 floyd
  3. 重新精读《Java 编程思想》系列之向上转型与向下转型
  4. HTML--猫眼电影---浮动练习
  5. MySQL 数据格式化
  6. Python输出帮助文档的方法及命令详解
  7. Mybatis 有坑,千万别踩!
  8. 从零打造Android计算器(安卓开发初体验)
  9. 1那智机器人主电连接
  10. 爬取中国地震台网以及地震科学数据
  11. 上海万应云——大数据精准招商系统
  12. 【C++】二分法查找某个数字在数组中的下标
  13. 〖Docker指南②〗Docker常用命令汇总
  14. android微信怎么建群,微信群空间是什么?如何创建微信群空间?
  15. #1045 无法登录 MySQL 服务器(实际上是我第一次使用,不知道密码)
  16. 《Cocos Creator游戏实战》游戏转场时如何保留节点信息
  17. 微信视频号亮剑,微推贝贝推广引流再次升级
  18. shell脚本监控系统资源并通过短信报警
  19. 利用arduino UNO配合Orangepi 4B实现制作机器人
  20. Push Mail技术(转)

热门文章

  1. Vue学习—深入剖析vue-cli脚手架(一)
  2. 卖身风波中的考拉员工
  3. Prometheus pod 流量监控
  4. 《中国新闻周刊》报道:施一公深陷泥潭
  5. BLAM跑自己的数据包无法显示全局点云地图解决(速腾聚创RS-LiDAR-16 雷达 )-SLAM不学无术小问题
  6. TelephonyTesgistry
  7. Oracle数据库Blob字段存储文本文件
  8. Android音频子系统(十)------MTK Audio录音流程代码解析
  9. html/template 和 text/template区别
  10. 【BUUCTF】[MRCTF2020]套娃