您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“python疲勞駕駛困倦低頭檢測功能怎么實(shí)現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“python疲勞駕駛困倦低頭檢測功能怎么實(shí)現(xiàn)”吧!
python疲勞駕駛困倦低頭檢測,代碼如下所示:
def get_head_pose(shape): # 頭部姿態(tài)估計(jì) # (像素坐標(biāo)集合)填寫2D參考點(diǎn) # 17左眉左上角/21左眉右角/22右眉左上角/26右眉右上角/36左眼左上角/39左眼右上角/42右眼左上角/ # 45右眼右上角/31鼻子左上角/35鼻子右上角/48左上角/54嘴右上角/57嘴中央下角/8下巴角 image_pts = np.float32([shape[17], shape[21], shape[22], shape[26], shape[36], shape[39], shape[42], shape[45], shape[31], shape[35], shape[48], shape[54], shape[57], shape[8]]) # solvePnP計(jì)算姿勢——求解旋轉(zhuǎn)和平移矩陣: # rotation_vec表示旋轉(zhuǎn)矩陣,translation_vec表示平移矩陣,cam_matrix與K矩陣對(duì)應(yīng),dist_coeffs與D矩陣對(duì)應(yīng)。 _, rotation_vec, translation_vec = cv2.solvePnP(object_pts, image_pts, cam_matrix, dist_coeffs) # projectPoints重新投影誤差:原2d點(diǎn)和重投影2d點(diǎn)的距離(輸入3d點(diǎn)、相機(jī)內(nèi)參、相機(jī)畸變、r、t,輸出重投影2d點(diǎn)) reprojectdst, _ = cv2.projectPoints(reprojectsrc, rotation_vec, translation_vec, cam_matrix, dist_coeffs) reprojectdst = tuple(map(tuple, reprojectdst.reshape(8, 2))) # 以8行2列顯示 # 計(jì)算歐拉角calc euler angle rotation_mat, _ = cv2.Rodrigues(rotation_vec) # 羅德里格斯公式(將旋轉(zhuǎn)矩陣轉(zhuǎn)換為旋轉(zhuǎn)向量) pose_mat = cv2.hconcat((rotation_mat, translation_vec)) # 水平拼接,vconcat垂直拼接 # decomposeProjectionMatrix將投影矩陣分解為旋轉(zhuǎn)矩陣和相機(jī)矩陣 _, _, _, _, _, _, euler_angle = cv2.decomposeProjectionMatrix(pose_mat) pitch, yaw, roll = [math.radians(_) for _ in euler_angle] pitch = math.degrees(math.asin(math.sin(pitch))) roll = -math.degrees(math.asin(math.sin(roll))) yaw = math.degrees(math.asin(math.sin(yaw))) print('pitch:{}, yaw:{}, roll:{}'.format(pitch, yaw, roll)) return reprojectdst, euler_angle # 投影誤差,歐拉角 def eye_aspect_ratio(eye): # 垂直眼標(biāo)志(X,Y)坐標(biāo) A = dist.euclidean(eye[1], eye[5]) # 計(jì)算兩個(gè)集合之間的歐式距離 B = dist.euclidean(eye[2], eye[4]) # 計(jì)算水平之間的歐幾里得距離 # 水平眼標(biāo)志(X,Y)坐標(biāo) C = dist.euclidean(eye[0], eye[3]) # 眼睛長寬比的計(jì)算 ear = (A + B) / (2.0 * C) # 返回眼睛的長寬比 return ear def mouth_aspect_ratio(mouth): # 嘴部 A = np.linalg.norm(mouth[2] - mouth[9]) # 51, 59 B = np.linalg.norm(mouth[4] - mouth[7]) # 53, 57 C = np.linalg.norm(mouth[0] - mouth[6]) # 49, 55 mar = (A + B) / (2.0 * C) return mar
到此,相信大家對(duì)“python疲勞駕駛困倦低頭檢測功能怎么實(shí)現(xiàn)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。