第一次用csdn,想问一下(如果格式之类的有错,我在这里提前说声抱歉)
我这里用了https://blog.csdn.net/qq_46554815/article/details/122778429 里的办法把yolov5的detect做成api在别的文件里调用,和dlib一起用来检测结果。
但是现在检测摄像头比较正常,检测本地文件就特别卡,整个视频特别慢
请问能看出原因吗?我的配置是R5 5600H+RTX3050 laptop,一运行就是cpu95%,内存88%。
然后有什么解决办法吗?
因为是python初学者,所以下面的代码肯定有挺多问题的,如果是单纯带不动的话,那有哪里能优化的吗?
环境是python3.9,torch1.9.1+cu111,cuda11.1,cudnn8005,因为yolo那边的关系这几个的版本很难改。
提前表示感谢。
from scipy.spatial import distance as dist
from imutils import face_utils
import numpy as np
import imutils
import dlib
import cv2
import detect
# 初始化人脸检测器
a = detect.detectapi(weights='weights/best.pt')
# 定义全局变量
global NUMtotal
def write_list_to_js(file_path, variable_name, data):
with open(file_path, 'w') as js_file:
js_file.write(f"const {variable_name} = {data};\n")
# 定义函数:计算眼睛长宽比
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
# 定义函数:计算嘴巴长宽比
def mouth_aspect_ratio(mouth):
A = np.linalg.norm(mouth[2] - mouth[9])
B = np.linalg.norm(mouth[4] - mouth[7])
C = np.linalg.norm(mouth[0] - mouth[6])
mar = (A + B) / (2.0 * C)
return mar
# 定义函数:处理摄像头读取和数据处理
def process_camera(Address):
global NUMtotal
conf_Ts, conf_Tp, conf_Td, ts, tp, td, tus, tud, tup = 0, 0, 0, 0, 0, 0, 0, 0, 0
N_s, N_p, N_d, TOTAL, mTOTAL, PERCLOS, Tired = 0, 0, 0, 0, 0, 0, False
EYE_AR_THRESH, EYE_AR_CONSEC_FRAMES, MAR_THRESH, MOUTH_AR_CONSEC_FRAMES = 0.15, 3, 0.5, 3
COUNTER, TOTAL, mCOUNTER, mTOTAL, pernum, PERCLOS = 0, 0, 0, 0, 0, 0
per = [0] * 30
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('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["mouth"]
cap = cv2.VideoCapture(Address)
while True:
ret, frame = cap.read()
frame = imutils.resize(frame, width=720)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
result, names = a.detect([frame])
frame = result[0][0]
t = dict(enumerate(result))
confi_total = result[0][1:3]
confi_total = confi_total[0]
confi_total = np.asarray(confi_total)
if len(confi_total) == 0:
conf = [0, 0, 0, 0]
elif len(confi_total) == 1:
conf = np.array([confi_total[0, 0], 0, 0, 0])
elif len(confi_total) == 2:
conf = np.array(confi_total[0:2, 0])
elif len(confi_total) == 3:
conf = np.array(confi_total[0:3, 0])
elif len(confi_total) == 4:
conf = np.array(confi_total[0:4])
conf_As = np.any(conf == 1)
conf_Ap = np.any(conf == 2)
conf_Ad = np.any(conf == 3)
if conf_As:
if conf_Ts == 0 and ts < 5:
ts += 1
elif conf_Ts == 0 and ts == 5:
N_s += 1
conf_Ts = 1
ts = 0
elif tus > 0:
tus = 0
if not conf_As:
if conf_Ts == 1 and tus < 7:
tus += 1
elif conf_Ts == 1 and tus == 7:
conf_Ts = 0
tus = 0
elif ts > 0:
ts = 0
if conf_Ap:
if conf_Tp == 0 and tp < 5:
tp += 1
elif conf_Tp == 0 and tp == 5:
conf_Tp = 1
N_p += 1
tp = 0
elif tup > 0:
tup = 0
if not conf_Ap:
if conf_Tp == 1 and tup < 7:
tup += 1
elif conf_Tp == 1 and tup == 7:
conf_Tp = 0
tup = 0
elif tp > 0:
tp = 0
if conf_Ad:
if conf_Td == 0 and td < 5:
td += 1
elif conf_Td == 0 and td == 5:
N_d += 1
conf_Td = 1
td = 0
elif tud > 0:
tud = 0
if not conf_Ad:
if conf_Td == 1 and tud < 7:
tud += 1
elif conf_Td == 1 and tud == 7:
conf_Td = 0
tud = 0
elif td > 0:
td = 0
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
mouth = shape[mStart:mEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0
mar = mouth_aspect_ratio(mouth)
if ear < EYE_AR_THRESH:
COUNTER += 1
else:
if COUNTER >= EYE_AR_CONSEC_FRAMES:
TOTAL += 1
COUNTER = 0
if pernum < 30:
if ear < EYE_AR_THRESH:
per[pernum] = 1
else:
per[pernum] = 0
pernum = pernum + 1
else:
pernum = 0
PERCLOS = sum(per) / 30
if mar > MAR_THRESH:
mCOUNTER += 1
else:
if mCOUNTER >= MOUTH_AR_CONSEC_FRAMES:
mTOTAL += 1
mCOUNTER = 0
if TOTAL >= 10 or mTOTAL >= 5 or PERCLOS > 0.12:
Tired = 1
else:
Tired = 0
cv2.imshow("Frame", frame)
NUMtotal = [N_s, N_p, N_d, TOTAL, mTOTAL, PERCLOS, Tired]
if cv2.waitKey(1) & 0xFF == 27:
cap.release()
cv2.destroyAllWindows()
break
process_camera('test.mp4')