我正在做一个项目,如果分数超过 15,就会发出警报,如果分数超过 100,就会向注册号码发送消息。分数是根据用户闭眼的时间计算的。我正在使用以下库和工具
下面是我正在使用的代码
检测.py:
import cv2
import dlib
from scipy.spatial import distance
import pygame
import subprocess
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
height,width = frame.shape[:2]
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
Score = 0
cv2.rectangle(frame, (0,height-50) ,(200,height) , (0,0,0) , thickness=cv2.FILLED )
# INITIALIZING THE MIXER SO THAT
# ALERT AUDIO MESSAGE CAN BE DELIVERED
pygame.mixer.init()
pygame.mixer.music.load("alarm.wav")
# SETTING UP OF CAMERA TO 1 YOU CAN
# EVEN CHOOSE 0 IN PLACE OF 1
# FACE DETECTION OR MAPPING THE FACE TO
# GET THE Eye AND EYES DETECTED
face_detector = dlib.get_frontal_face_detector()
# PUT THE LOCATION OF .DAT FILE (FILE FOR
# PREDECTING THE LANDMARKS ON FACE )
dlib_facelandmark = dlib.shape_predictor(
"C:\\Users\\DELL\\Documents\\Project_TYCS\\shape_predictor_68_face_landmarks.dat")
# FUNCTION CALCULATING THE ASPECT RATIO FOR
# THE Eye BY USING EUCLIDEAN DISTANCE FUNCTION
def Detect_Eye(eye):
poi_A = distance.euclidean(eye[1], eye[5])
poi_B = distance.euclidean(eye[2], eye[4])
poi_C = distance.euclidean(eye[0], eye[3])
aspect_ratio_Eye = (poi_A+poi_B)/(2*poi_C)
return aspect_ratio_Eye
# MAIN LOOP IT WILL RUN ALL THE UNLESS AND
# UNTIL THE PROGRAM IS BEING KILLED BY THE USER
while True:
null, frame = cap.read()
gray_scale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_detector(gray_scale)
for face in faces:
face_landmarks = dlib_facelandmark(gray_scale, face)
leftEye = []
rightEye = []
# THESE ARE THE POINTS ALLOCATION FOR THE
# LEFT EYES IN .DAT FILE THAT ARE FROM 42 TO 47
for n in range(42, 48):
x = face_landmarks.part(n).x
y = face_landmarks.part(n).y
rightEye.append((x, y))
next_point = n+1
if n == 47:
next_point = 42
x2 = face_landmarks.part(next_point).x
y2 = face_landmarks.part(next_point).y
cv2.line(frame, (x, y), (x2, y2), (0, 255, 0), 1)
# THESE ARE THE POINTS ALLOCATION FOR THE
# RIGHT EYES IN .DAT FILE THAT ARE FROM 36 TO 41
for n in range(36, 42):
x = face_landmarks.part(n).x
y = face_landmarks.part(n).y
leftEye.append((x, y))
next_point = n+1
if n == 41:
next_point = 36
x2 = face_landmarks.part(next_point).x
y2 = face_landmarks.part(next_point).y
cv2.line(frame, (x, y), (x2, y2), (255, 255, 0), 1)
# CALCULATING THE ASPECT RATIO FOR LEFT
# AND RIGHT EYE
right_Eye = Detect_Eye(rightEye)
left_Eye = Detect_Eye(leftEye)
Eye_Rat = (left_Eye+right_Eye)/2
# NOW ROUND OF THE VALUE OF AVERAGE MEAN
# OF RIGHT AND LEFT EYES
Eye_Rat2 = round(Eye_Rat,2)
# THIS VALUE OF 0.25 (YOU CAN EVEN CHANGE IT)
# WILL DECIDE WHETHER THE PERSONS'S EYES ARE CLOSE OR NOT
if Eye_Rat2 < 0.25:
Score+=1
else :
Score=0
# CALLING THE AUDIO FUNCTION OF TEXT TO
# AUDIO FOR ALERTING THE PERSON
if Score > 100:
# Call the call.py script using subprocess
subprocess.call(['python', 'Main.py'])
# Draw a rectangle and display the drowsiness alert message
cv2.rectangle(frame, (0,0), (450, 60), (21, 56, 210), -1)
cv2.putText(frame, "HIGHLY DROWSY, MESSAGE IS BEING SENT", (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# Display the current score and stop message
cv2.putText(frame, 'Score:'+str(Score), (10,height-40), cv2.FONT_HERSHEY_COMPLEX, 1.5, (21, 56, 212), 1, cv2.LINE_AA)
cv2.putText(frame, 'Press X to stop', (500,height-450), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,0,0), 1, cv2.LINE_AA)
# Play the alert sound if it's not already playing
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play()
else:
True
elif Score > 15:
# Draw a rectangle and display the drowsiness alert message
cv2.rectangle(frame, (0,0), (450, 60), (21, 56, 210), -1)
cv2.putText(frame, "DROWSINESS DETECTED | ALERT! WAKE UP", (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# Display the current score and stop message
cv2.putText(frame, 'Score:'+str(Score), (10,height-40), cv2.FONT_HERSHEY_COMPLEX, 1.5, (21, 56, 212), 1, cv2.LINE_AA)
cv2.putText(frame, 'Press X to stop', (500,height-450), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,0,0), 1, cv2.LINE_AA)
# Play the alert sound if it's not already playing
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play()
else:
True
else:
# Reset the score to zero and display the current score and stop message
score = 0
cv2.putText(frame, 'Score:'+str(Score), (10,height-40), cv2.FONT_HERSHEY_COMPLEX, 1.5, (255,255,255), 1, cv2.LINE_AA)
cv2.putText(frame, 'Press X to stop', (500,height-450), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,0,0), 1, cv2.LINE_AA)
# Stop the alert sound if it's currently playing
pygame.mixer.music.stop()
cv2.imshow("Drowsiness DETECTOR IN OPENCV2", frame)
if cv2.waitKey(9) & 0xFF == ord('x'):
break
cap.release()
cv2.destroyAllWindows()
Main.py(它包含 twilio api 代码)
from twilio.rest import Client
import Keys
from Geo import latitude
from Geo import longitude
Client = Client(Keys.account_sid, Keys.auth_token)
message = Client.messages.create(
body = "**ALERT THE DRIVER IS DETECTED DROWSY OVER THE LIMIT** "+"\n"+"Live location link: "+"https://www.google.com/maps/place/"+str(latitude-0.003923)+','+str(longitude-0.003219),
from_=Keys.twilio_number,
to = Keys.my_phone_number
)
print(message.body)
和他们一起我也在使用 GEO.py 和 keys.py.
问题 - 当分数超过 100 时,在输出中我只想将一条消息发送到已注册的电话,但它会发送多条消息。此外,输出窗口也会滞后/卡住。
如果超过 100,我需要它只发送一条消息。我尝试添加 time.sleep 但它不起作用。