我是OpenCV和Python的新手,但设法通过在线教程进行了安装。我正在尝试运行以下脚本:
from scipy.spatial import distance as dist
from imutils.video import VideoStream
from imutils import face_utils
from threading import Thread
import numpy as np
import argparse
import imutils
import time
import dlib
import cv2
import subprocess
def sound_alarm(path):
# play an alarm sound
subprocess.call(["afplay",path])
def eye_aspect_ratio(eye):
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
C = dist.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True,
help="path to facial landmark predictor")
ap.add_argument("-a", "--alarm", type=str, default="",
help="path alarm .WAV file")
ap.add_argument("-w", "--webcam", type=int, default=0,
help="index of webcam on system")
args = vars(ap.parse_args())
EYE_AR_THRESH = 0.25
EYE_AR_CONSEC_FRAMES = 10
COUNTER = 0
ALARM_ON = False
print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
print("[INFO] starting video stream thread...")
vs = VideoStream(src=args["webcam"]).start()
time.sleep(1.0)
while True:
frame = vs.read()
frame = imutils.resize(frame, width=450)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
if ear < EYE_AR_THRESH:
COUNTER += 1
if COUNTER >= EYE_AR_CONSEC_FRAMES:
if not ALARM_ON:
ALARM_ON = True
if args["alarm"] != "":
t = Thread(target=sound_alarm,
args=(args["alarm"],))
t.deamon = True
t.start()
cv2.putText(frame, "DROWSINESS ALERT!", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
else:
COUNTER = 0
ALARM_ON = False
cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
vs.stop()
脚本运行正常,但出现以下问题:
1)根据触发条件(alarm.wav),没有警报声音。
2)执行此脚本时:
python detect_drowsiness.py --shape-predictor
shape_predictor_68_face_landmarks.dat --alarm alarm.wav
我收到以下错误
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Mandeesh\AppData\Local\Programs\Python\Python37-
32\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\Mandeesh\AppData\Local\Programs\Python\Python37-
32\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "detect_drowsiness.py", line 18, in sound_alarm
subprocess.call(["afplay",path])
File "C:\Users\Mandeesh\AppData\Local\Programs\Python\Python37-
32\lib\subprocess.py", line 317, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\Mandeesh\AppData\Local\Programs\Python\Python37-
32\lib\subprocess.py", line 769, in __init__
restore_signals, start_new_session)
File "C:\Users\Mandeesh\AppData\Local\Programs\Python\Python37-
32\lib\subprocess.py", line 1172, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
我对opencv和python完全陌生,并在朋友的项目中尝试了此脚本。任何人都可以帮助建议潜在的问题吗?
答案 0 :(得分:0)
欢迎来到Stackoverflow!
发生此错误是因为您的脚本未将正确的文件路径传递给Bracket *l1 = new Bracket(next, position + 1);
opening_brackets_stack.push(*l1);
函数。
检查包含以下内容的行:
Bracket l1{next, position + 1};
opening_brackets_stack.push(l1);
如果您在使sound_alarm(path)
正常工作时遇到麻烦,建议您先在单独的脚本中使用一个简单的函数来隔离问题,以测试警报是否响起。
您可以尝试:
if args["alarm"] != "":
t = Thread(target=sound_alarm,
args=(args["alarm"],))
t.deamon = True
t.start()
如果您在控制台行中打印了sound_alarm(path)
和import subprocess
path = r'C:\your_file_path_here'
def sound_alarm(path):
# play an alarm sound
print ('entered function')
subprocess.call(["afplay",path])
print ('ended function')
sound_alarm(path)
,但是仍然没有声音播放,这意味着entered function
的声音在播放方式上存在问题。文件,这意味着调用该文件的代码是错误的,必须重新处理,否则该文件会出现问题。
答案 1 :(得分:0)
感谢解决了这个问题。我试图在Windows上运行MACOS函数“ afplay”。使用“ playsound”修改了脚本,并且声音触发了!
谢谢。