实时口罩检测求助

第一次写博客求助

#AI项目名称
实时口罩识别
#主要应用领域(主要介绍该领域存在的问题以及你对这些问题的思考、想去解决这些问题的思路等。)
领域 :领域主要在一些人群密集的场所
问题:人群密集场所人口流动十分的大,所以需要进出时佩戴口罩,就目前学校的检测都是通过人力检测这样耗费了人力。
思路:可不可以通过在深度学习后实现自动检测呢?
解决办法:通过改进学习过的口罩佩戴识别实验达到目标
#项目开发语言、工具与平台
Python
Sublime
win10的操作系统
附上代码请大佬观摩
部分程序代码

# -*- coding:utf-8 -*-
import cv2
import time
import argparse

import numpy as np
from PIL import Image
from keras.models import model_from_json
from utils.anchor_generator import generate_anchors
from utils.anchor_decode import decode_bbox
from utils.nms import single_class_non_max_suppression
from load_model.tensorflow_loader import load_tf_model, tf_inference

'''yangsongling 改写部分'''
# 获取视频流
cap = cv2.VideoCapture(0)
# 指定编码格式
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# cv2.VideoWriter的参数分别为:保存路径,编码格式,帧率,帧大小
out = cv2.VideoWriter("avis/001.avi", fourcc, 20.0, (640,480))
#保存avi后不必删除下次打开会自动覆盖
while(cap.isOpened()):
    ret, frame = cap.read()
    
    if ret==True:
        # 如果帧的大小与上述的(640, 480)不一致,需要resize
        out.write(frame)
 
        cv2.imshow('frame',frame)
        #按q关闭窗口
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
 
cap.release()
out.release()
cv2.destroyAllWindows()

#导入口罩智能识别模型
sess, graph = load_tf_model('models\face_mask_detection.pb')
# anchor configuration锚点设置
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

# generate anchors生成锚点
anchors = generate_anchors(feature_map_sizes, anchor_sizes, anchor_ratios)

#以下用于推断(inference),批大小为1,模型输出形状为[1,N,4],因此将锚点的dim扩展为[1,anchor_num,4]
anchors_exp = np.expand_dims(anchors, axis=0)
#将推断结果分为两类,一类Mask,表示佩戴了口罩,另外一类是NoMask,表示没有佩戴口罩
id2class = {
    
    0: 'mask', 1: 'NoMask'}


def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True):
    '''  检测推理的主要功能
   # :param image:3D numpy图片数组
    #  :param conf_thresh:分类概率的最小阈值。
   #  :param iou_thresh:网管的IOU门限
   #  :param target_shape:模型输入大小。
   #  :param draw_result:是否将边框拖入图像。
   #  :param show_result:是否显示图像。
    '''
    # image = np.copy(image)
    output_info = []
    height, width, _ = image.shape
    image_resized = cv2.resize(image, target_shape)
    image_np = image_resized / 255.0  # 归一化到0~1
    image_exp = np.expand_dims(image_np, axis=0)
    y_bboxes_output, y_cls_output = tf_inference(sess, graph, image_exp)

    # remove the batch dimension, for batch is always 1 for inference.
    y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0]
    y_cls = y_cls_output[0]
    # 为了加快速度,请执行单类NMS,而不是多类NMS。
    bbox_max_scores = np.max(y_cls, axis=1)
    bbox_max_score_classes = np.argmax(y_cls, axis=1)

    # keep_idx是nms之后的活动边界框。
    keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh,iou_thresh=iou_thresh)
    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[1] * height))
        xmax = min(int(bbox[2] * width), width)
        ymax = min(int(bbox[3] * height), height)

        if draw_result:
            if class_id == 0:
                color = (0, 255, 0)
            else:
                color = (255, 0, 0)
            cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2)
            cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, color)
        output_info.append([class_id, conf, xmin, ymin, xmax, ymax])

    if show_result:
        Image.fromarray(image).show()
    return output_info

def run_on_video(video_path, output_video_name, conf_thresh):
    cap = cv2.VideoCapture(video_path)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    fps = cap.get(cv2.CAP_PROP_FPS)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    #writer = cv2.VideoWriter(output_video_name, fourcc, int(fps), (int(width), int(height)))
    total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    if not cap.isOpened():
        raise ValueError("Video open failed.")
        return
    status = True
    idx = 0
    while status:
        start_stamp = time.time()
        status, img_raw = cap.read()
        img_raw = cv2.cvtColor(img_raw, cv2.COLOR_BGR2RGB)
        read_frame_stamp = time.time()
        if (status):
            inference(img_raw,
                      conf_thresh,
                      iou_thresh=0.5,
                      target_shape=(260, 260),
                      draw_result=True,
                      show_result=False)
            cv2.imshow('image', img_raw[:, :, ::-1])
            cv2.waitKey(1)
            inference_stamp = time.time()
            # writer.write(img_raw)
            write_frame_stamp = time.time()
            idx += 1
            print("%d of %d" % (idx, total_frames))
            print("read_frame:%f, infer time:%f, write time:%f" % (read_frame_stamp - start_stamp, inference_stamp - read_frame_stamp, write_frame_stamp - inference_stamp))
run_on_video("avis/001.avi", '', conf_thresh=0.5)

我把需要的文件也上传到百度云了

https://pan.baidu.com/s/1xZNUrz8TV-QFdfLfulOZFw
提取码 s8nj
在这里插入图片描述

写完之后

问题


然后有没有大佬教教我怎么搞?
注:本人是一名大一学生还在学习中,过程中可能用到过前辈的东西没注明来处
看到可找我我撤销文章,毕竟第一次写这个.

猜你喜欢

转载自blog.csdn.net/xiaodududao/article/details/107251662