ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

opencv检测口罩佩戴

2020-11-29 20:01:54  阅读:528  来源: 互联网

标签:口罩 mask max cv2 佩戴 detection opencv bbox net


# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import cv2
import numpy as np
from anchor_generator import generate_anchors
from anchor_decode import decode_bbox
from nms import single_class_non_max_suppression

feature_map_sizes = [[33, 33], [17, 17], [9, 9], [5, 5], [3, 3]]
anchor_sizes = [[0.04, 0.056], [0.08, 0.11], [0.16, 0.22], [0.32, 0.45], [0.64, 0.72]]
anchor_ratios = [[1, 0.62, 0.42]] * 5

anchors = generate_anchors(feature_map_sizes,anchor_sizes,anchor_ratios)
anchors_exp = np.expand_dims(anchors,axis=0)
id2class = {0:'Mask',1:'NoMask'}


def face_mask_detection_caffe(conf_thresh=0.5,iou_thresh=0.4):
    output_info = []
    model_txt = "D:/vs2019Proj/ConsoleApplication1/face_mask_detection/face_mask_detection.prototxt"
    model_bin = "D:/vs2019Proj/ConsoleApplication1/face_mask_detection/face_mask_detection.caffemodel"
    net = cv2.dnn.readNetFromCaffe(model_txt,model_bin)
    print(type(net))
    if net.empty():
        return;
    # cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture("mask.jpg")
    # img = cv2.imread("mask.jpg")
    if not cap.isOpened():
        return ;
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
    net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
    while True:
        status,img = cap.read()
        if not status:
            break ;
        blob = cv2.dnn.blobFromImage(img, 1 / 255.0, (260, 260), (104.0, 177.0, 123.0))
        net.setInput(blob,"data")
        outname = net.getUnconnectedOutLayersNames()
        detection = net.forward(outname)
        # print(type(detection))
        y_bboxes = decode_bbox(anchors_exp,detection[0])[0]
        # print(y_bboxes)
        y_cls = detection[1][0]
        bbox_max_scores = np.max(y_cls,axis=1)
        bbox_max_score_classes = np.argmax(y_cls,axis=1)
        keep_idxs = single_class_non_max_suppression(y_bboxes,bbox_max_scores)
        for idx in keep_idxs:
            conf = float(bbox_max_scores[idx])
            class_id = bbox_max_score_classes[idx]
            bbox = y_bboxes[idx]
            xmin = max(0,int(bbox[0]*width))
            ymin = max(0,int(bbox[0]*height))
            xmax = min(int(bbox[2]*width),int(width))
            ymax = min(int(bbox[3]*height),int(height))
            if class_id == 0:
                color = (0,255,0)
            else:
                color = (255,0,0)
            cv2.rectangle(img,(xmin,ymin),(xmax,ymax),color,2)
            cv2.putText(img,"%s:%.2f"%(id2class[class_id],conf),(xmin+2,ymin-2),cv2.FONT_HERSHEY_SIMPLEX,0.8,color)
            # output_info.append([class_id, conf, xmin, ymin, xmax, ymax])
        cv2.imshow("img",img)
        c = cv2.waitKey(10)
        if c == 27:
            break
    return output_info


if __name__ == '__main__':
    info = face_mask_detection_caffe()
    print(info)



 

标签:口罩,mask,max,cv2,佩戴,detection,opencv,bbox,net
来源: https://blog.csdn.net/qq_37589971/article/details/110351518

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有