欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python如何實現(xiàn)控制電腦音量

 更新時間:2023年09月16日 14:48:48   作者:阿軻愛玩Python  
這篇文章主要介紹了python如何實現(xiàn)控制電腦音量問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

python控制電腦音量

總共兩個文件,放在同一個目錄下,運行第二個就行,把1這個文件命名為“主函數(shù)”,第二個文件隨意名。

1、

import cv2
import mediapipe as mp
import time
class handDect():
    def __init__(self,model=False,maxHands=2,detectionCon=0.5,trackCon=0.5):
        self.mode=model
        self.maxHands=maxHands
        self.dectionCon=detectionCon
        self.trackCon=trackCon
        self.myhand = mp.solutions.hands
        self.hands = self.myhand.Hands(False)
        self.mpDraw = mp.solutions.drawing_utils
    def findHands(self,img,draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.hands.process(imgRGB)
        if self.results.multi_hand_landmarks:
            for hanglms in self.results.multi_hand_landmarks:
                if draw:
                 self.mpDraw.draw_landmarks(img, hanglms, self.myhand.HAND_CONNECTIONS)
        return img
    def findPosition(self, img, handNo=0, draw=True):
        lmList = []
        if self.results.multi_hand_landmarks:
            if self.results.multi_hand_landmarks[handNo]:
             myHand = self.results.multi_hand_landmarks[handNo]
             for id, lm in enumerate(myHand.landmark):
                # print(id, lm)
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                # print(id, cx, cy)
                lmList.append([id, cx, cy])
                # if draw:
                #cv2.putText(img, str(int(id)), (cx,cy), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
        return lmList
def main():
    pTime = 0
    cTime = 0
    cap = cv2.VideoCapture(0)
    detector=handDect()
    while True:
        success, img = cap.read()
        img=detector.findHands(img)
        lmList = detector.findPosition(img)
        if len(lmList) != 0:
            mm=abs(lmList[4][1] - lmList[8][1])
            kk=abs(lmList[8][2] - lmList[12][2])
            if(mm<6 and kk>50):
              nn="hello,word "
              cv2.putText(img, str(nn), (60, 80), cv2.FONT_HERSHEY_PLAIN, 3, (60, 60, 255), 3)
              # import win32com.client
              #
              # speaker = win32com.client.Dispatch("SAPI.SpVoice")
              #
              # speaker.Speak("你好, 許軻,你今天看起來鄭帥")
        cTime = time.time()
        fps = 1 / (cTime - pTime)
        pTime = cTime
        cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
        cv2.imshow("image", img)
        cv2.waitKey(1)
if __name__=="__main__":
    main()

2、

```python
import cv2
import mediapipe as mp
import time
from ctypes import cast, POINTER
import numpy as np
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import 主函數(shù) as htm
import math
pTime=0
cTime=0
cap=cv2.VideoCapture(0)
detector=htm.handDect()
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
    IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volume.GetMute()
volume.GetMasterVolumeLevel()
volRange = volume.GetVolumeRange()
print(volRange)
minVol = volRange[0]
maxVol = volRange[1]
vol = 0
volBar = 400
volPer = 0
while True:
    success,img=cap.read()
    img=detector.findHands(img)
    lmlist=detector.findPosition(img,draw=False)
    if len(lmlist) != 0:
        x1,y1=lmlist[4][1],lmlist[4][2]
        x2, y2 = lmlist[8][1], lmlist[8][2]
        cv2.circle(img,(x1,y1),10,(255,0,255),cv2.FILLED)
        cv2.circle(img, (x2, y2), 10, (255, 0, 255), cv2.FILLED)
        cx=int((x1+x2)/2)
        cy = int((y1 + y2) / 2)
        length=math.hypot(x2-x1,y2-y1)
        print(length)
        vol=np.interp(length,[0,200],[minVol,maxVol])
        volBar = np.interp(length, [50, 300], [400, 150])
        volPer = np.interp(length, [50, 300], [0, 100])
        volume.SetMasterVolumeLevel(vol, None)
        cv2.rectangle(img, (50, 150), (85, 400), (0, 255, 0), 3)
        cv2.rectangle(img, (50, int(volBar)), (85, 400), (0, 255, 0), cv2.FILLED)
        if(length<10):
            cv2.circle(img, (cx, cy), 10, (0, 255, 0), cv2.FILLED)
        else:
            cv2.circle(img, (cx, cy), 10, (255, 0, 255), cv2.FILLED)
        cv2.line(img,(x1,y1),(x2,y2),(255,0,255),3)
    cTime = time.time()
    fps = 1 / (cTime - pTime)
    pTime = cTime
    cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
    cv2.putText(img, str("music"+str(int(volPer))), (170, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
    cv2.imshow("image", img)
    cv2.waitKey(1)

手勢控制電腦音量

在tiktok看到的計算機視覺大佬恩培,然后跟著一起完成了這個簡單的計算機視覺的小項目。

"""
Date: 2021-11-16
功能:手勢操作電腦音量
1、使用OpenCV讀取攝像頭視頻流;
2、識別手掌關鍵點像素坐標;
3、根據(jù)拇指和食指指尖的坐標,利用勾股定理計算距離;
4、將距離等比例轉為音量大小,控制電腦音量
"""
# 導入OpenCV
import cv2
# 導入mediapipe
import mediapipe as mp
# 導入電腦音量控制模塊
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
# 導入其他依賴包
import time
import math
import numpy as np
class HandControlVolume:
    def __init__(self):
        # 初始化medialpipe
        self.mp_drawing = mp.solutions.drawing_utils
        self.mp_drawing_styles = mp.solutions.drawing_styles
        self.mp_hands = mp.solutions.hands
        # 獲取電腦音量范圍
        devices = AudioUtilities.GetSpeakers()
        interface = devices.Activate(
            IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
        self.volume = cast(interface, POINTER(IAudioEndpointVolume))
        self.volume.SetMute(0, None)
        self.volume_range = self.volume.GetVolumeRange()
    # 主函數(shù)
    def recognize(self):
        # 計算刷新率
        fpsTime = time.time()
        # OpenCV讀取視頻流
        cap = cv2.VideoCapture(0)
        # 視頻分辨率
        resize_w = 640
        resize_h = 480
        # 畫面顯示初始化參數(shù)
        rect_height = 0
        rect_percent_text = 0
        with self.mp_hands.Hands(min_detection_confidence=0.7,
                                 min_tracking_confidence=0.5,
                                 max_num_hands=2) as hands:
            while cap.isOpened():
                success, image = cap.read()
                image = cv2.resize(image, (resize_w, resize_h))
                if not success:
                    print("空幀.")
                    continue
                # 提高性能
                image.flags.writeable = False
                # 轉為RGB
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                # 鏡像
                image = cv2.flip(image, 1)
                # mediapipe模型處理
                results = hands.process(image)
                image.flags.writeable = True
                image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
                # 判斷是否有手掌
                if results.multi_hand_landmarks:
                    # 遍歷每個手掌
                    for hand_landmarks in results.multi_hand_landmarks:
                        # 在畫面標注手指
                        self.mp_drawing.draw_landmarks(
                            image,
                            hand_landmarks,
                            self.mp_hands.HAND_CONNECTIONS,
                            self.mp_drawing_styles.get_default_hand_landmarks_style(),
                            self.mp_drawing_styles.get_default_hand_connections_style())
                        # 解析手指,存入各個手指坐標
                        landmark_list = []
                        for landmark_id, finger_axis in enumerate(
                                hand_landmarks.landmark):
                            landmark_list.append([
                                landmark_id, finger_axis.x, finger_axis.y,
                                finger_axis.z
                            ])
                        if landmark_list:
                            # 獲取大拇指指尖坐標
                            thumb_finger_tip = landmark_list[4]
                            thumb_finger_tip_x = math.ceil(thumb_finger_tip[1] * resize_w)
                            thumb_finger_tip_y = math.ceil(thumb_finger_tip[2] * resize_h)
                            # 獲取食指指尖坐標
                            index_finger_tip = landmark_list[8]
                            index_finger_tip_x = math.ceil(index_finger_tip[1] * resize_w)
                            index_finger_tip_y = math.ceil(index_finger_tip[2] * resize_h)
                            # 中間點
                            finger_middle_point = (thumb_finger_tip_x + index_finger_tip_x) // 2, (
                                    thumb_finger_tip_y + index_finger_tip_y) // 2
                            # print(thumb_finger_tip_x)
                            thumb_finger_point = (thumb_finger_tip_x, thumb_finger_tip_y)
                            index_finger_point = (index_finger_tip_x, index_finger_tip_y)
                            # 畫指尖2點
                            image = cv2.circle(image, thumb_finger_point, 10, (255, 0, 255), -1)
                            image = cv2.circle(image, index_finger_point, 10, (255, 0, 255), -1)
                            image = cv2.circle(image, finger_middle_point, 10, (255, 0, 255), -1)
                            # 畫2點連線
                            image = cv2.line(image, thumb_finger_point, index_finger_point, (255, 0, 255), 5)
                            # 勾股定理計算長度
                            line_len = math.hypot((index_finger_tip_x - thumb_finger_tip_x),
                                                  (index_finger_tip_y - thumb_finger_tip_y))
                            # 獲取電腦最大最小音量
                            min_volume = self.volume_range[0]
                            max_volume = self.volume_range[1]
                            # 將指尖長度映射到音量上
                            vol = np.interp(line_len, [50, 300], [min_volume, max_volume])
                            # 將指尖長度映射到矩形顯示上
                            rect_height = np.interp(line_len, [50, 300], [0, 200])
                            rect_percent_text = np.interp(line_len, [50, 300], [0, 100])
                            # 設置電腦音量
                            self.volume.SetMasterVolumeLevel(vol, None)
                # 顯示矩形
                cv2.putText(image, str(math.ceil(rect_percent_text)) + "%", (10, 350),
                            cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
                image = cv2.rectangle(image, (30, 100), (70, 300), (255, 0, 0), 3)
                image = cv2.rectangle(image, (30, math.ceil(300 - rect_height)), (70, 300), (255, 0, 0), -1)
                # 顯示刷新率FPS
                cTime = time.time()
                fpsTime = cTime
                cv2.putText(image, "FPS: " + str(int(fps_text)), (10, 70),
                            cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
                # 顯示畫面
                cv2.imshow('MediaPipe Hands', image)
                    break
            cap.release()
# 開始程序
control = HandControlVolume()
control.recognize()

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Python實現(xiàn)爬取百度貼吧帖子所有樓層圖片的爬蟲示例

    Python實現(xiàn)爬取百度貼吧帖子所有樓層圖片的爬蟲示例

    這篇文章主要介紹了Python實現(xiàn)爬取百度貼吧帖子所有樓層圖片的爬蟲,涉及基于urllib的網(wǎng)頁訪問與正則匹配相關操作技巧,需要的朋友可以參考下
    2018-04-04
  • Python中sorted()函數(shù)的強大排序技術實例探索

    Python中sorted()函數(shù)的強大排序技術實例探索

    排序在編程中是一個基本且重要的操作,而Python的sorted()函數(shù)則為我們提供了強大的排序能力,在本篇文章中,我們將深入研究不同排序算法、sorted()?函數(shù)的靈活性,以及各種排序場景下的最佳實踐
    2024-01-01
  • python設計模式大全

    python設計模式大全

    這篇文章主要介紹了python設計模式,通過簡單的代碼實現(xiàn)了Python常見的各種設計模式,包括橋接模式、觀測者模式、適配器模式、工廠模式、單例模式等,需要的朋友可以參考下
    2016-06-06
  • 華為2019校招筆試題之處理字符串(python版)

    華為2019校招筆試題之處理字符串(python版)

    這篇文章主要為大家詳細介紹了華為2019校招筆試題之處理字符串,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Django 聯(lián)表查詢操作方法

    Django 聯(lián)表查詢操作方法

    作為一個django使用的新手,在做練手項目中對聯(lián)表查詢感覺比較生疏,最近兩天整理了一些連表查詢應用場景和使用方法以及無法使用django中ORM操作的原生查詢,對Django 聯(lián)表查詢操作感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • YOLOv5構建安全帽檢測和識別系統(tǒng)使用詳解

    YOLOv5構建安全帽檢測和識別系統(tǒng)使用詳解

    這篇文章主要為大家介紹了YOLOv5構建安全帽檢測和識別系統(tǒng)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Pytorch中torch.argmax()函數(shù)使用及說明

    Pytorch中torch.argmax()函數(shù)使用及說明

    這篇文章主要介紹了Pytorch中torch.argmax()函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 示例詳解python中的排序

    示例詳解python中的排序

    本文詳細介紹了如何使用Python實現(xiàn)兩組數(shù)據(jù)的縱向排序,包括開發(fā)思想、開發(fā)流程和代碼示例,感興趣的朋友一起看看吧
    2024-12-12
  • python實現(xiàn)簡單的井字棋游戲(gui界面)

    python實現(xiàn)簡單的井字棋游戲(gui界面)

    這篇文章主要介紹了python如何實現(xiàn)簡單的井字棋游戲,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • Python中的代碼異常處理你掌握了嗎

    Python中的代碼異常處理你掌握了嗎

    Python?使用稱為異常(exception)的特殊對象來管理程序執(zhí)行期間發(fā)生的錯誤,那Python中代碼異常處理的相關操作你都了解了嗎,本文為大家進行了詳細整理,需要的可以參考下
    2023-06-06

最新評論