只能检测头盔,不能检测人头,不能判断是否带头盔

https://github.com/BlcaKHat/yolov3-Helmet-Detection/blob/master/Helmet_detection_YOLOV3.py

权重:

https://github.com/rezabonyadi/Helmet_Detection_YOLO

from time import sleep
import cv2 as cv
import argparse
import sys
import numpy as np
import os.path
from glob import glob
#from PIL import image
frame_count = 0             # used in mainloop  where we're extracting images., and then to drawPred( called by post process)
frame_count_out=0           # used in post process loop, to get the no of specified class value.
# Initialize the parameters
confThreshold = 0.5  #Confidence threshold
nmsThreshold = 0.4   #Non-maximum suppression threshold
inpWidth = 416       #Width of network's input image
inpHeight = 416      #Height of network's input image# Load names of classes
classesFile = "obj.names";
classes = None
with open(classesFile, 'rt') as f:classes = f.read().rstrip('\n').split('\n')# Give the configuration and weight files for the model and load the network using them.
modelConfiguration = "yolov3-obj.cfg";
modelWeights = "yolov3-obj_2400.weights";net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)# Get the names of the output layers
def getOutputsNames(net):# Get the names of all the layers in the networklayersNames = net.getLayerNames()# Get the names of the output layers, i.e. the layers with unconnected outputsreturn [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]# Draw the predicted bounding box
def drawPred(classId, conf, left, top, right, bottom):global frame_count
# Draw a bounding box.cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)label = '%.2f' % conf# Get the label for the class name and its confidenceif classes:assert(classId < len(classes))label = '%s:%s' % (classes[classId], label)#Display the label at the top of the bounding boxlabelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)top = max(top, labelSize[1])#print(label)            #testing#print(labelSize)        #testing#print(baseLine)         #testinglabel_name,label_conf = label.split(':')    #spliting into class & confidance. will compare it with person.if label_name == 'Helmet':#will try to print of label have people.. or can put a counter to find the no of people occurance.#will try if it satisfy the condition otherwise, we won't print the boxes or leave it.cv.rectangle(frame, (left, top - round(1.5*labelSize[1])), (left + round(1.5*labelSize[0]), top + baseLine), (255, 255, 255), cv.FILLED)cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,0), 1)frame_count+=1#print(frame_count)if(frame_count> 0):return frame_count# Remove the bounding boxes with low confidence using non-maxima suppression
def postprocess(frame, outs):frameHeight = frame.shape[0]frameWidth = frame.shape[1]global frame_count_outframe_count_out=0classIds = []confidences = []boxes = []# Scan through all the bounding boxes output from the network and keep only the# ones with high confidence scores. Assign the box's class label as the class with the highest score.classIds = []               #have to fins which class have hieghest confidence........=====>>><<<<=======confidences = []boxes = []for out in outs:for detection in out:scores = detection[5:]classId = np.argmax(scores)confidence = scores[classId]if confidence > confThreshold:center_x = int(detection[0] * frameWidth)center_y = int(detection[1] * frameHeight)width = int(detection[2] * frameWidth)height = int(detection[3] * frameHeight)left = int(center_x - width / 2)top = int(center_y - height / 2)classIds.append(classId)#print(classIds)confidences.append(float(confidence))boxes.append([left, top, width, height])# Perform non maximum suppression to eliminate redundant overlapping boxes with# lower confidences.indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)count_person=0 # for counting the classes in this loop.for i in indices:i = i[0]box = boxes[i]left = box[0]top = box[1]width = box[2]height = box[3]#this function in  loop is calling drawPred so, try pushing one test counter in parameter , so it can calculate it.frame_count_out = drawPred(classIds[i], confidences[i], left, top, left + width, top + height)#increase test counter till the loop end then print...#checking class, if it is a person or notmy_class='Helmet'                   #======================================== mycode .....unknown_class = classes[classId]if my_class == unknown_class:count_person += 1#if(frame_count_out > 0):print(frame_count_out)if count_person >= 1:path = 'test_out/'frame_name=os.path.basename(fn)             # trimm the path and give file name.cv.imwrite(str(path)+frame_name, frame)     # writing to folder.#print(type(frame))cv.imshow('img',frame)cv.waitKey(800)#cv.imwrite(frame_name, frame)#======================================mycode.........# Process inputs
winName = 'Deep learning object detection in OpenCV'
cv.namedWindow(winName, cv.WINDOW_NORMAL)for fn in glob('images/*.jpg'):frame = cv.imread(fn)frame_count =0# Create a 4D blob from a frame.blob = cv.dnn.blobFromImage(frame, 1/255, (inpWidth, inpHeight), [0,0,0], 1, crop=False)# Sets the input to the networknet.setInput(blob)# Runs the forward pass to get output of the output layersouts = net.forward(getOutputsNames(net))# Remove the bounding boxes with low confidencepostprocess(frame, outs)# Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)t, _ = net.getPerfProfile()#print(t)label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())#print(label)cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))#print(label)

opencv 头盔检测相关推荐

  1. Pyhton,OpenCV对象检测之——Haar级联人脸及眼睛检测

    Pyhton,OpenCV对象检测之--Haar级联人脸及眼睛检测 1. 效果图 2. 原理 2.1 Haar人脸检测原理 2.2 Haar级联预训练的模型 3. 源码 3.1 图像面部及眼睛检测 3 ...

  2. 使用Python,OpenCV+OCR检测护照图像中的机器可读区域(MRZ Machine-Readable Zones)

    使用Python,OpenCV+OCR检测护照图像中的机器可读区域(MRZ Machine-Readable Zones) 1. 效果图 2. 原理 3. 源码 参考 这篇博客将介绍如何只使用基本的图 ...

  3. 如何使用 OpenCV Python 检测颜色

    作者 | 小白 来源 | 小白学视觉 在这篇文章中,我们将看到如何使用 Python 中的 OpenCV 模块检测颜色,进入这个领域的第一步就是安装下面提到的模块. pip install openc ...

  4. OpenCV行人检测

    注:本文翻译自:Pedestrian Detection OpenCV. 你知道OpenCV里面已经内置的行人检测方法吗?在OpenCV里面,有一个预先训练好了的HOG+线性SVM模型,能够对图像和视 ...

  5. OpenCV SURF检测的实例(附完整代码)

    OpenCV SURF检测的实例 OpenCV SURF检测的实例 OpenCV SURF检测的实例 #include <iostream> #include "opencv2/ ...

  6. OpenCV方形检测Square Detection的实例(附完整代码)

    OpenCV方形检测Square Detection的实例 OpenCV方形检测Square Detection的实例 OpenCV方形检测Square Detection的实例 #include & ...

  7. OpenCV BLOB检测和过滤区域的实例(附完整代码)

    OpenCV BLOB检测和过滤区域的实例 OpenCV BLOB检测和过滤区域的实例 OpenCV BLOB检测和过滤区域的实例 #include <opencv2/core.hpp> ...

  8. OpenCV与图像处理学习十七——OpenCV人脸检测(含代码)

    OpenCV与图像处理学习十七--OpenCV人脸检测(含代码) 一.人脸识别概要 1.1 人脸检测 1.2 人脸对齐(Face Alignment) 1.3 人脸特征提取(Face Feature ...

  9. OpenCV-Python实战(21)——OpenCV人脸检测项目在Web端的部署

    OpenCV-Python实战(21)--OpenCV人脸检测项目在Web端的部署 0. 前言 1. OpenCV 人脸检测项目在 Web 端的部署 1.1 解析请求并构建响应 1.2 构建请求进行测 ...

最新文章

  1. 经典算法研究系列:二、Dijkstra 算法初探
  2. 美国智库报告:自动驾驶对社会、经济与劳动力的影响
  3. 断点续传---多线程下载进阶(一)
  4. Spring配置多数据源错误总结
  5. css设置元素的宽高为整数,为什么有的浏览器解析出来的宽高是小数?
  6. 合同相似可逆等价矩阵的关系及性质_线性代数预习自学笔记-11:等价性与相似性...
  7. 软件测试——0422作业
  8. 14_python基础—匿名函数lambda表达式
  9. java修饰符作用域
  10. Java和python哪个好,学哪个有用。
  11. 通讯录 C语言分类,C语言 通讯录
  12. 天勤数据结构完全二叉树选择题
  13. FTP测试手机软件画画教程图片,「推荐」手机、平板绘画党福利!10个非常好用的绘画App赶紧试试...
  14. 【安信可首款4G模组CA-01直连阿里物联网平台②】一机一密认证方式连接
  15. Anaconda 安装pkgs
  16. angularjs常见错误_AngularJS开发人员应避免的7大错误
  17. 华为ACL配置(基本ACL+高级ACL+综合应用)
  18. rebase interactive
  19. 广西单招计算机科学与技术专业,广西单招考什么科目
  20. 迪桑特宣布彭于晏成为品牌代言人

热门文章

  1. C语言--返回局部变量的地址
  2. imp 只导表前10条数据_做电商设计,你必须懂的10条数据指标
  3. 频率分布直方图组距如何确定_小猿圈Python开发之绘制频率分布直方图示例
  4. 解决npm ERR! Unexpected end of JSON input while parsing near...的方法
  5. html中设置文本框长度,Html的文本框怎样限制录入文本框的字节长度
  6. mega_[MEGA DEAL] Android课程的Java基础知识(61%折扣)
  7. 通过jsl工具将java程序注册为windows服务
  8. mysql怎么导出所有的表名称_MYSQL导出表名(navicat 导出表名称)
  9. 年终凡尔赛,都是别人家的公司...
  10. 又一个 Java 面试神器!