如何检测特定对象何时在OPENCV程序中移出框架?

发布于 2025-02-08 23:14:01 字数 1266 浏览 1 评论 0原文

我目前正在练习OPENCV并尝试了面部识别代码,并且工作正常。如果我离开屏幕超过2分钟,我想通知。当我不在时,我正在尝试播放音频文件,并在回来时停止它。

import cv2 as cv
import sys

cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv.CASCADE_SCALE_IMAGE  
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Display the resulting frame
    cv.imshow('Video', frame)
    
    # check if the tuple faces is empty
    if len(faces) == 0:
      start_time = time.time()
      while len(faces) == 0:
        print('person is away for ',time.time()-start_time)
        d_time = time.time()
        if d_time-start_time > 120:
            pygame.mixer.init()
            sound = pygame.mixer.Sound("Recording.mp3")
            sound.play(5)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv.destroyAllWindows()

I'm currently practicing opencv and tried the face recognition code and it's working fine. I'd like to get notified if I'm away from the screen for more than 2 mins. I'm trying to play an audio file when I'm away and stop it when I'm back.

import cv2 as cv
import sys

cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv.CASCADE_SCALE_IMAGE  
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Display the resulting frame
    cv.imshow('Video', frame)
    
    # check if the tuple faces is empty
    if len(faces) == 0:
      start_time = time.time()
      while len(faces) == 0:
        print('person is away for ',time.time()-start_time)
        d_time = time.time()
        if d_time-start_time > 120:
            pygame.mixer.init()
            sound = pygame.mixer.Sound("Recording.mp3")
            sound.play(5)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv.destroyAllWindows()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文