只检测到一次面部后,Opencv会打印一条消息

时间:2018-05-19 13:06:21

标签: python opencv

目前,我在我的覆盆子pi上运行了一个opencv面部检测脚本。它用python编码。人脸检测部分正在工作。基本上,我想在检测到面部后将mqtt消息发送到另一个覆盆子pi。我可以通过向该主机发布来轻松地做到这一点,问题是如果长时间检测到该人的面部,则会发送多条消息。有没有办法只发送一条消息,直到不再检测到面部?所以基本上,将检测到人的面部并且将发送一个mqtt消息,然后在未检测到面部之前将不再发送消息。这似乎是一个非常简单的问题,所以也许我只是错过了明显的问题。下面我有运行opencv脚本的代码:

from picamera.array import PiRGBArray
from picamera import PiCamera
import paho.mqtt.client as mqtt

host = '10.0.0.192'
client = mqtt.Client("Security")
client.connect(host)

import time
import cv2

threshold = 60  #  BINARY threshold
blurValue = 41  # GaussianBlur parameter
bgSubThreshold = 50
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 64
rawCapture = PiRGBArray(camera, size=(640, 480))

# allow the camera to warmup
time.sleep(0.1)

# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text
    image = frame.array
    face_cascade = cv2.CascadeClassifier('/home/pi/opencv-face-sentdex/faces.xml')
    # show the frame
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (blurValue, blurValue), 0)
    faces = face_cascade.detectMultiScale(blur, 1.1, 5)
    if faces is not None:
        for (x,y,w,h) in faces:
            cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)
            client.publish("/home/pi/snips-app","faceDetected")#publish

    cv2.imshow("Frame", image)

    key = cv2.waitKey(1) & 0xFF

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

0 个答案:

没有答案