[Python Study Notes]行人检测

# --------------------------------------------------------------
# @文件: 行人识别.py
# @工程: blog
# @时间: 2018/3/16 21:12
# @作者: liu yang
# @博客: liuyang1.club
# @邮箱: liuyang0001@outlook.com
# -------------------------------------------------------------
# 编码格式
# -*- coding: utf-8 -*-
# Python版本
# #!/usr/bin/python3

import cv2


def draw_detection(img, rects):
    for x, y, w, h in rects:
        pad_w, pad_h = int(0.05 * w), int(0.05 * h)
        cv2.rectangle(img, (x + pad_w, y + pad_h), (x + w - pad_w, y + h - pad_h), (0, 255, 0), 2)


def draw_alart(img, rects):
    area = [0, 2]
    size = img.shape
    area_img = size[0] * size[1]
    for x, y, w, h in rects:
        area.append(w * h)
    thresh = 0.25
    if max(area) / area_img > thresh:
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, "Incoming Peaple", (round(size[0] / 2) - 80, round(size[1] / 2) - 50),
                    font, 6, (0, 0, 255), 25)


# 传入opencv里默认的参数
hog = cv2.HOGDescriptor()
# 得到行人的特征值
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# 调用摄像头,也可以传入一个路径视频文件
cap = cv2.VideoCapture(0)
while True:
    # ret是一个表
    # 循环读取每一帧的数据
    ret, frame = cap.read()
    # 扫描图像,如果检测不到进行缩小检测
    found, w = hog.detectMultiScale(frame, 0, winStride=(8, 8), padding=(8, 8), scale=1.05)
    draw_detection(frame, found)
    draw_alart(frame, found)
    # 显示每一帧的画面
    cv2.imshow("pd", frame)
    # 按q退出
    key = cv2.waitKey(1) & 0xff
    if key == ord('q'):
        break

# 运行结束后释放摄像头
cap.release()
cv2.destroyAllWindows()

posted @ 2018-03-16 22:32  刘六六  阅读(606)  评论(0编辑  收藏  举报