python - 在检测到 python 时运行特定代码

标签 python python-3.x pygame driver

此代码用于在检测到驾驶员困倦时播放警报

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()

为了保持简单。如果检测到司机昏昏欲睡,就会发出警报。 当它检测到司机昏昏欲睡时,我该如何运行警报,因为在此期间警报只运行一次。

这是我的声音报警方法:

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

提前致谢

最佳答案

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

while的主要结构循环看起来像:

while condition_which_is_true:
    do_something()

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

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()

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

关于python - 在检测到 python 时运行特定代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579289/

相关文章:

python - 制作稍微更高效的函数结构

python - 我的函数使用 'None'返回了新行

用于调用函数的 Python 管道符

python - 我无法在 pygame python 中传递名为 window 的窗口

python - 如何在一个函数中休眠而不休眠整个程序

python - 使 PyInstaller exe 同时执行命令行和窗口

python - 修改不同的对象而不检查标志

python - MySQLdb 安装错误 - _mysql.c :44:23: error: my_config. h: No such file or directory

Python with 语句,继续

python - 如何将一个元组作为输入,并将其作为新元组返回,但仅限奇数?