代码拉取完成,页面将自动刷新
同步操作将从 KX/疲劳驾驶预测 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import cv2
import dlib
import imutils
from imutils import face_utils
from scipy.spatial import distance as dist
import time
import ffmpeg
import subprocess
# VideoCapture方法是cv2库提供的读取视频方法
video_file = 'input.mp4'
audio_file = 'audio.mp3'
output_file = 'output.mp4'
output2_file = 'C:/Users/admin/Desktop/output.mp4'
cap = cv2.VideoCapture(video_file)
ffmpeg_path = 'G:/ffmpeg-master-latest-win64-gpl/bin/ffmpeg.exe'
# 设置需要保存视频的格式“xvid”
# 该参数是MPEG-4编码类型,文件名后缀为.avi
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# 设置视频帧频
fps = cap.get(cv2.CAP_PROP_FPS)
# 设置视频大小
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# VideoWriter方法是cv2库提供的保存视频方法
# 按照设置的格式来out输出
out = cv2.VideoWriter(output_file, fourcc, fps, size)
subprocess.run([ffmpeg_path, '-i', video_file, '-vn', '-acodec', 'libmp3lame', '-ab', '128k', audio_file])
def eye_aspect_ratio(eye):
# compute the euclidean distances between the two sets of
# vertical eye landmarks (x, y)-coordinates
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
# compute the euclidean distance between the horizontal
# eye landmark (x, y)-coordinates
C = dist.euclidean(eye[0], eye[3])
# compute the eye aspect ratio
ear = (A + B) / (2.0 * C)
# return the eye aspect ratio
return ear
# Load the pre-trained face detector
face_detector = dlib.get_frontal_face_detector()
# Load the pre-trained facial landmark predictor
landmark_predictor = dlib.shape_predictor("G:\\test\\shape_predictor_68_face_landmarks.dat")
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
(mStart, mEnd) = face_utils.FACIAL_LANDMARKS_IDXS["inner_mouth"]
(nStart,nEnd) = face_utils.FACIAL_LANDMARKS_IDXS["nose"]
# Start the webcam
EYE_AR_THRESH = 0.2
EYE_AR_CONSEC_FRAMES = 1
# initialize the frame counters and the total number of blinks
COUNTER = 0
TOTAL = 0
dura = 0.0
FPS = 0.0
start_time = cv2.getTickCount()
def eye_aspect_ratio(eye):
# compute the euclidean distances between the two sets of
# vertical eye landmarks (x, y)-coordinates
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
# compute the euclidean distance between the horizontal
# eye landmark (x, y)-coordinates
C = dist.euclidean(eye[0], eye[3])
# compute the eye aspect ratio
ear = (A + B) / (2.0 * C)
# return the eye aspect ratio
return ear
# Loop over each frame from the webcam
while True:
t1 = cv2.getTickCount()
ret, frame = cap.read()
if not ret: break
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
rects = face_detector(gray)
# Loop over the faces
for rect in rects:
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
shape = landmark_predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# extract the left and right eye coordinates, then use the
# coordinates to compute the eye aspect ratio for both eyes
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
mouse = shape[mStart:mEnd]
nose = shape[nStart:nEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
# average the eye aspect ratio together for both eyes
ear = (leftEAR + rightEAR) / 2.0
# compute the convex hull for the left and right eye, then
# visualize each of the eyes
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
mouseHull = cv2.convexHull(mouse)
noseHull = cv2.convexHull(nose)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [mouseHull], -1, (255, 0, 0), 1)
# check to see if the eye aspect ratio is below the blink
# threshold, and if so, increment the blink frame counter
cv2.rectangle(frame, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (255, 0, 0), 4)
if ear < EYE_AR_THRESH:
COUNTER += 1
# otherwise, the eye aspect ratio is not below the blink
# threshold
else:
# if the eyes were closed for a sufficient number of
# then increment the total number of blinks
if COUNTER >= EYE_AR_CONSEC_FRAMES:
dura = COUNTER / FPS
TOTAL += 1
# reset the eye frame
COUNTER = 0
# Display the resulting frame
cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 2)
cv2.putText(frame, "EAR: {:.2f}".format(ear), (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 2)
cv2.putText(frame, "dura: {:.2f}".format(dura), (400, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 2)
if TOTAL >= 20:
cv2.putText(frame, "SLEEP!!!", (400, 200), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2)
t2 = cv2.getTickCount()
spendTime = (t2 - t1) / (cv2.getTickFrequency())
FPS = 1 / spendTime
t3 = cv2.getTickCount() - start_time
print(t3)
if t3 >= 600000000:
start_time = cv2.getTickCount()
TOTAL = 0
cv2.imshow("Face and Facial Landmark Detection", frame)
out.write(frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# Release the webcam and close the window
time.sleep(5)
subprocess.run([ffmpeg_path, '-i', output_file, '-i', audio_file, '-c:v', 'copy', '-c:a', 'aac','-strict', 'experimental', output2_file])
cap.release()
cv2.destroyAllWindows()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。