在检测到python时运行特定代码 [英] run a certain code while it detects something python

查看:58
本文介绍了在检测到python时运行特定代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码用于在检测到驾驶员昏昏欲睡时发出警报

This code is used to play an alarm if it detects the driver is drowsy

if args["alarm"] != "":
    t = Thread(target=sound_alarm,
        args=(args["alarm"],))
    t.daemon = False
    t.start()

整个代码如下:

if ear < EYE_AR_THRESH:
        COUNTER += 1

        # if the eyes were closed for a sufficient number of
        # then sound the alarm
        if COUNTER >= EYE_AR_CONSEC_FRAMES:

            # if the alarm is not on, turn it on
            if not ALARM_ON:
                ALARM_ON = True

                # check to see if an alarm file was supplied,
                # and if so, start a thread to have the alarm
                # sound played in the background
                if args["alarm"] != "":
                    t = Thread(target=sound_alarm,
                        args=(args["alarm"],))
                    t.daemon = False
                    t.start()

            # draw an alarm on the frame
            cv2.putText(frame, "DROWSINESS ALERT!", (10, 30),
                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
            fan_on()




# otherwise, the eye aspect ratio is not below the blink
# threshold, so reset the counter and alarm
else:
    COUNTER = 0
    ALARM_ON = False
    fan_off()

为简单起见.如果检测到驾驶员困倦,则会发出警报.当警报检测到驾驶员困倦时如何运行警报,因为与此同时警报仅运行一次.

To keep it simple. An alarm will sound if it detects the driver is drowsy. How can I run the alarm while it detects the driver is drowsy, because at the meantime the alarm only run once.

这是我的声音警报方法:

This is my sound alarm method:

def sound_alarm(path):
    pygame.mixer.init()
    pygame.mixer.music.load(path)
    pygame.mixer.music.play()

预先感谢

推荐答案

我建议您使用 while 循环,以便重复您的代码直到特定条件变为 False

I recommend you to use a while loop, in order to repeat your code until a certain condition becomes False

while 循环的主要结构如下:

The main structure of the while loop looks like:

while condition_which_is_true:
    do_something()

因此,在这种特殊情况下,我将执行以下操作:

So, in this particular situation, I would do the following:

if ear < EYE_AR_THRESH:
    COUNTER += 1

    # if the eyes were closed for a sufficient number of
    # then sound the alarm
    if COUNTER >= EYE_AR_CONSEC_FRAMES:

        # if the alarm is not on, turn it on
        while not ALARM_ON:
            ALARM_ON = True

            # check to see if an alarm file was supplied,
            # and if so, start a thread to have the alarm
            # sound played in the background
            if args["alarm"] != "":
                t = Thread(target=sound_alarm,
                    args=(args["alarm"],))
                t.daemon = False
                t.start()

        # draw an alarm on the frame
        cv2.putText(frame, "DROWSINESS ALERT!", (10, 30),
            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
        fan_on()

# otherwise, the eye aspect ratio is not below the blink
# threshold, so reset the counter and alarm
else:
    COUNTER = 0
    ALARM_ON = False
    fan_off()

请注意,由于您尚未提供完整的代码示例,因此很难为您提供帮助

Please, note that, as you haven't provided a fully working code example it's very difficult to help you

这篇关于在检测到python时运行特定代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆