最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python圖像處理篇之opencv實(shí)現(xiàn)坐姿檢測(cè)

 更新時(shí)間:2025年05月16日 08:27:17   作者:Ronin-Lotus  
這篇文章主要給大家介紹了關(guān)于Python圖像處理篇之opencv實(shí)現(xiàn)坐姿檢測(cè)的相關(guān)資料,講解了使用Python和OpenCV、MediaPipe實(shí)現(xiàn)坐姿檢測(cè)的方法和完整代碼,需要的朋友可以參考下

前言

坐姿檢測(cè)是計(jì)算機(jī)視覺(jué)中的一個(gè)應(yīng)用,可以通過(guò)分析人體姿態(tài)來(lái)判斷是否保持正確坐姿。下面我將介紹使用Python實(shí)現(xiàn)坐姿檢測(cè)的方法和完整代碼。

一、方法概述

使用OpenCV和MediaPipe

使用OpenCV和MediaPipe:MediaPipe提供了現(xiàn)成的人體姿態(tài)估計(jì)模型

關(guān)鍵點(diǎn)檢測(cè)

關(guān)鍵點(diǎn)檢測(cè):檢測(cè)身體關(guān)鍵點(diǎn)(如肩膀、耳朵、臀部等)

角度計(jì)算

角度計(jì)算:計(jì)算關(guān)鍵點(diǎn)之間的角度來(lái)判斷坐姿

姿態(tài)評(píng)估

姿勢(shì)評(píng)估:根據(jù)角度閾值判斷坐姿是否正確

二、完整代碼實(shí)現(xiàn)

import cv2
import mediapipe as mp
import numpy as np
import time

class PostureDetector:
    def __init__(self, mode=False, upBody=False, smooth=True, 
                 detectionCon=0.5, trackCon=0.5):
        """
        初始化姿勢(shì)檢測(cè)器
        參數(shù):
            mode: 是否靜態(tài)圖像模式 (False表示視頻流)
            upBody: 是否只檢測(cè)上半身
            smooth: 是否平滑處理
            detectionCon: 檢測(cè)置信度閾值
            trackCon: 跟蹤置信度閾值
        """
        self.mode = mode
        self.upBody = upBody
        self.smooth = smooth
        self.detectionCon = detectionCon
        self.trackCon = trackCon
        
        self.mpDraw = mp.solutions.drawing_utils
        self.mpPose = mp.solutions.pose
        self.pose = self.mpPose.Pose(
            static_image_mode=self.mode,
            model_complexity=1,
            smooth_landmarks=self.smooth,
            enable_segmentation=False,
            smooth_segmentation=self.smooth,
            min_detection_confidence=self.detectionCon,
            min_tracking_confidence=self.trackCon
        )
        
        # 坐姿評(píng)估參數(shù)
        self.good_posture_time = 0
        self.bad_posture_time = 0
        self.posture_status = "Unknown"
        self.last_posture_change = time.time()
        
    def find_pose(self, img, draw=True):
        """
        檢測(cè)圖像中的姿勢(shì)關(guān)鍵點(diǎn)
        參數(shù):
            img: 輸入圖像 (BGR格式)
            draw: 是否繪制關(guān)鍵點(diǎn)和連接線
        返回:
            帶標(biāo)注的圖像和姿勢(shì)關(guān)鍵點(diǎn)
        """
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.pose.process(img_rgb)
        
        if self.results.pose_landmarks and draw:
            self.mpDraw.draw_landmarks(
                img, self.results.pose_landmarks, 
                self.mpPose.POSE_CONNECTIONS)
        
        return img
    
    def get_landmarks(self, img):
        """
        獲取所有姿勢(shì)關(guān)鍵點(diǎn)的坐標(biāo)
        參數(shù):
            img: 輸入圖像
        返回:
            關(guān)鍵點(diǎn)坐標(biāo)列表 (x,y,z) 或 None
        """
        self.landmarks = []
        if self.results.pose_landmarks:
            for id, lm in enumerate(self.results.pose_landmarks.landmark):
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                self.landmarks.append([id, cx, cy, lm.z])
        return self.landmarks
    
    def calculate_angle(self, a, b, c):
        """
        計(jì)算三個(gè)點(diǎn)之間的角度
        參數(shù):
            a, b, c: 三個(gè)點(diǎn)的坐標(biāo) (x,y)
        返回:
            角度 (degrees)
        """
        a = np.array(a)
        b = np.array(b)
        c = np.array(c)
        
        radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
        angle = np.abs(radians * 180.0 / np.pi)
        
        if angle > 180.0:
            angle = 360 - angle
            
        return angle
    
    def evaluate_posture(self, img, draw=True):
        """
        評(píng)估坐姿是否正確
        參數(shù):
            img: 輸入圖像
            draw: 是否在圖像上繪制評(píng)估結(jié)果
        返回:
            圖像和坐姿評(píng)估結(jié)果
        """
        current_time = time.time()
        posture_changed = False
        
        if not self.landmarks or len(self.landmarks) < 33:
            return img, "No person detected"
        
        # 獲取需要的關(guān)節(jié)點(diǎn)
        left_shoulder = self.landmarks[11][1:3]  # 11: 左肩
        right_shoulder = self.landmarks[12][1:3]  # 12: 右肩
        left_ear = self.landmarks[7][1:3]  # 7: 左耳
        right_ear = self.landmarks[8][1:3]  # 8: 右耳
        left_hip = self.landmarks[23][1:3]  # 23: 左髖
        right_hip = self.landmarks[24][1:3]  # 24: 右髖
        
        # 計(jì)算肩膀中點(diǎn)
        shoulder_mid = (
            (left_shoulder[0] + right_shoulder[0]) // 2,
            (left_shoulder[1] + right_shoulder[1]) // 2
        )
        
        # 計(jì)算耳朵中點(diǎn)
        ear_mid = (
            (left_ear[0] + right_ear[0]) // 2,
            (left_ear[1] + right_ear[1]) // 2
        )
        
        # 計(jì)算髖部中點(diǎn)
        hip_mid = (
            (left_hip[0] + right_hip[0]) // 2,
            (left_hip[1] + right_hip[1]) // 2
        )
        
        # 計(jì)算脊柱角度 (肩膀-髖部-垂直線)
        spine_angle = self.calculate_angle(
            (shoulder_mid[0], shoulder_mid[1] - 100),  # 肩膀上方一點(diǎn)
            shoulder_mid,
            hip_mid
        )
        
        # 計(jì)算頸部角度 (耳朵-肩膀-水平線)
        neck_angle = self.calculate_angle(
            (ear_mid[0] - 100, ear_mid[1]),
            ear_mid,
            shoulder_mid
        )
        
        # 坐姿評(píng)估標(biāo)準(zhǔn)
        good_spine = 160 < spine_angle < 200  # 脊柱應(yīng)該接近垂直
        good_neck = 70 < neck_angle < 110  # 頸部應(yīng)該接近垂直
        
        # 判斷坐姿
        new_status = "Good" if good_spine and good_neck else "Bad"
        
        # 更新姿勢(shì)狀態(tài)時(shí)間
        if new_status != self.posture_status:
            posture_changed = True
            self.last_posture_change = current_time
            self.posture_status = new_status
        else:
            if new_status == "Good":
                self.good_posture_time += 1
            else:
                self.bad_posture_time += 1
        
        # 在圖像上繪制結(jié)果
        if draw:
            # 繪制關(guān)鍵點(diǎn)和連接線
            cv2.circle(img, ear_mid, 8, (255, 0, 0), cv2.FILLED)
            cv2.circle(img, shoulder_mid, 8, (255, 0, 0), cv2.FILLED)
            cv2.circle(img, hip_mid, 8, (255, 0, 0), cv2.FILLED)
            cv2.line(img, ear_mid, shoulder_mid, (255, 0, 0), 3)
            cv2.line(img, shoulder_mid, hip_mid, (255, 0, 0), 3)
            
            # 顯示角度信息
            cv2.putText(img, f"Spine Angle: {int(spine_angle)}", 
                        (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
            cv2.putText(img, f"Neck Angle: {int(neck_angle)}", 
                        (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
            
            # 顯示坐姿狀態(tài)
            color = (0, 255, 0) if new_status == "Good" else (0, 0, 255)
            cv2.putText(img, f"Posture: {new_status}", 
                        (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
            
            # 顯示時(shí)間統(tǒng)計(jì)
            cv2.putText(img, f"Good Time: {self.good_posture_time//10}s", 
                        (10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
            cv2.putText(img, f"Bad Time: {self.bad_posture_time//10}s", 
                        (10, 140), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
            
            # 如果姿勢(shì)不良,添加警告
            if new_status == "Bad":
                cv2.putText(img, "WARNING: Bad Posture!", 
                            (img.shape[1]//2 - 150, 30), 
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
        
        return img, new_status

def main():
    cap = cv2.VideoCapture(0)  # 使用攝像頭
    detector = PostureDetector()
    
    while True:
        success, img = cap.read()
        if not success:
            break
            
        img = detector.find_pose(img)
        landmarks = detector.get_landmarks(img)
        
        if landmarks:
            img, posture = detector.evaluate_posture(img)
        
        cv2.imshow("Posture Detection", img)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

三、代碼說(shuō)明

PostureDetector類(lèi)

PostureDetector類(lèi):核心坐姿檢測(cè)類(lèi)

find_pose()

find_pose(): 檢測(cè)圖像中的人體姿勢(shì)

get_landmarks()

get_landmarks(): 獲取姿勢(shì)關(guān)鍵點(diǎn)坐標(biāo)

cakculate_angle()

calculate_angle(): 計(jì)算三個(gè)關(guān)鍵點(diǎn)之間的角度

evaluate_posture()

evaluate_posture(): 評(píng)估坐姿是否正確

坐姿評(píng)估標(biāo)準(zhǔn)(可進(jìn)行參數(shù)調(diào)整):

脊柱角度應(yīng)在160-200度之間(接近垂直)
頸部角度應(yīng)在70-110度之間(接近垂直)
滿足以上條件判斷為"Good"坐姿,否則為"Bad"

可視化功能:

  • 繪制關(guān)鍵點(diǎn)和連接線
  • 顯示角度數(shù)值
  • 顯示坐姿狀態(tài)和時(shí)間統(tǒng)計(jì)
  • 不良坐姿時(shí)顯示警告

如何使用

安裝依賴庫(kù):

pip install opencv-python mediapipe numpy

運(yùn)行腳本:

python posture_detection.py

調(diào)整攝像頭位置,確保能清晰看到上半身

四、改進(jìn)方向

  • 添加更多評(píng)估標(biāo)準(zhǔn)(如肩膀是否前傾)
  • 實(shí)現(xiàn)坐姿歷史記錄和統(tǒng)計(jì)分析
  • 添加聲音提醒功能
  • 優(yōu)化性能(如降低圖像分辨率)
  • 添加校準(zhǔn)功能,適應(yīng)不同體型
    這個(gè)實(shí)現(xiàn)提供了基本的坐姿檢測(cè)功能,你可以根據(jù)需要進(jìn)一步擴(kuò)展完善。

總結(jié) 

到此這篇關(guān)于Python圖像處理篇之opencv實(shí)現(xiàn)坐姿檢測(cè)的文章就介紹到這了,更多相關(guān)Python opencv坐姿檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python去除所有html標(biāo)簽的方法

    python去除所有html標(biāo)簽的方法

    這篇文章主要介紹了python去除所有html標(biāo)簽的方法,涉及Python正則替換的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-05-05
  • python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例

    python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例

    本篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • python獲取時(shí)間戳的實(shí)現(xiàn)示例(10位和13位)

    python獲取時(shí)間戳的實(shí)現(xiàn)示例(10位和13位)

    這篇文章主要介紹了python獲取時(shí)間戳的實(shí)現(xiàn)示例(10位和13位),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 初步解析Python中的yield函數(shù)的用法

    初步解析Python中的yield函數(shù)的用法

    這篇文章主要介紹了Python中的yield函數(shù),yield函數(shù)是生成器中的一個(gè)常用函數(shù),本文來(lái)自于IBM官方網(wǎng)站的開(kāi)發(fā)者文檔的翻譯,需要的朋友可以參考下
    2015-04-04
  • 淺析python多線程中的鎖

    淺析python多線程中的鎖

    這篇文章主要介紹了淺析python多線程中的鎖,鎖由Python的threading模塊提供,并且它最多被一個(gè)線程所持有,當(dāng)一個(gè)線程試圖獲取一個(gè)已經(jīng)鎖在資源上的鎖時(shí),該線程通常會(huì)暫停運(yùn)行,直到這個(gè)鎖被釋放,需要的朋友可以參考下
    2023-07-07
  • python-web根據(jù)元素屬性進(jìn)行定位的方法

    python-web根據(jù)元素屬性進(jìn)行定位的方法

    這篇文章主要介紹了python-web根據(jù)元素屬性進(jìn)行定位的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 使用Python處理PDF文件的實(shí)踐分享

    使用Python處理PDF文件的實(shí)踐分享

    在現(xiàn)代數(shù)字化時(shí)代,PDF(Portable?Document?Format)文件已經(jīng)成為廣泛使用的電子文檔格式,這篇文章主要為分享了Python處理PDF文件的簡(jiǎn)介與實(shí)踐,需要的可以參考下
    2023-06-06
  • Python利用字節(jié)串或字節(jié)數(shù)組來(lái)加載和保存PDF文檔

    Python利用字節(jié)串或字節(jié)數(shù)組來(lái)加載和保存PDF文檔

    處理PDF文件的可以直接讀取和寫(xiě)入文件系統(tǒng)中的PDF文件,然而,通過(guò)字節(jié)串(byte string)或字節(jié)數(shù)組(byte array)來(lái)加載和保存PDF文檔在某些情況下更高效,本文將介紹如何使用Python通過(guò)字節(jié)串或字節(jié)數(shù)組來(lái)加載和保存PDF文檔,需要的朋友可以參考下
    2024-09-09
  • python文本進(jìn)度條實(shí)例

    python文本進(jìn)度條實(shí)例

    這篇文章主要為大家介紹了python文本進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • 與Django結(jié)合利用模型對(duì)上傳圖片預(yù)測(cè)的實(shí)例詳解

    與Django結(jié)合利用模型對(duì)上傳圖片預(yù)測(cè)的實(shí)例詳解

    今天小編就為大家分享一篇與Django結(jié)合利用模型對(duì)上傳圖片預(yù)測(cè)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08

最新評(píng)論

武汉市| 普定县| 土默特右旗| 双柏县| 象山县| 梅州市| 扶风县| 罗城| 祁门县| 余江县| 东乡县| 义马市| 扎兰屯市| 大兴区| 山丹县| 华宁县| 班玛县| 社旗县| 岢岚县| 双流县| 安陆市| 永康市| 维西| 旬邑县| 明水县| 故城县| 沙田区| 八宿县| 鱼台县| 平阳县| 临桂县| 关岭| 永福县| 宁晋县| 揭东县| 沁水县| 潍坊市| 平远县| 察雅县| 荃湾区| 敦化市|