Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add GestureControlledVolume script
  • Loading branch information
lisa1612 committed Oct 8, 2023
commit fd8ebeda5ef861cfb98abc89f5f8b312ffea52a2
196 changes: 196 additions & 0 deletions PYTHON APPS/GestureControlledVolume/GestureControlledVolume.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "bbcc9707",
"metadata": {},
"outputs": [],
"source": [
"conda install opencv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9503238e",
"metadata": {},
"outputs": [],
"source": [
"import cv2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7af804f",
"metadata": {},
"outputs": [],
"source": [
"import mediapipe as mp"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "953e7623",
"metadata": {},
"outputs": [],
"source": [
"import math "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "889f973c",
"metadata": {},
"outputs": [],
"source": [
"pip install pycaw"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0bbc1460",
"metadata": {},
"outputs": [],
"source": [
"from ctypes import cast, POINTER\n",
"from comtypes import CLSCTX_ALL\n",
"from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb4f9175",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f47550a4",
"metadata": {},
"outputs": [],
"source": [
"devices = AudioUtilities.GetSpeakers()\n",
"interface = devices.Activate(\n",
" IAudioEndpointVolume._iid_, CLSCTX_ALL, None)\n",
"volume = cast(interface, POINTER(IAudioEndpointVolume))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f9c586f",
"metadata": {},
"outputs": [],
"source": [
"#volume.GetMute()\n",
"#print(volume.GetMasterVolumeLevel())\n",
"#print(volume.GetVolumeRange())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a7cc9ac",
"metadata": {},
"outputs": [],
"source": [
"cap = cv2.VideoCapture(0)\n",
"mpHands = mp.solutions.hands\n",
"hands = mpHands.Hands()\n",
"mpDraw = mp.solutions.drawing_utils\n",
"\n",
"\n",
"while True:\n",
" success,img = cap.read()\n",
" imgRGB = cv2.cvtColor(img , cv2.COLOR_BGR2RGB)\n",
" results= hands.process(imgRGB)\n",
" #print(results.multi_hand_landmarks)\n",
" \n",
" if results.multi_hand_landmarks:\n",
" for handLms in results.multi_hand_landmarks:\n",
" lmList =[]\n",
" for id, lm in enumerate (handLms.landmark):\n",
" #print(id, lm)\n",
" h , w , c = img.shape\n",
" cx , cy = int(lm.x*w), int(lm.y*h)\n",
" #print(id, cx, cy)\n",
" lmList.append([id, cx, cy])\n",
" #print(lmList)\n",
" mpDraw.draw_landmarks(img,handLms,mpHands.HAND_CONNECTIONS)\n",
" \n",
" if lmList:\n",
" x1,y1 = lmList[4][1],lmList[4][2]\n",
" x2,y2 = lmList[8][1],lmList[8][2]\n",
" \n",
" cv2.circle(img,(x1,y1),10,(255,0,9),cv2.FILLED)\n",
" cv2.circle(img,(x2,y2),10,(255,0,9),cv2.FILLED)\n",
" cv2.line(img,(x1,y1),(x2,y2),(134,1,175),3)\n",
" \n",
" length = math.hypot(x2-x1,y2-y1)\n",
" print(length)\n",
" \n",
" if length <50:\n",
" z1 = (x1+x2)//2\n",
" z2 = (y1+y2)//2\n",
" cv2.circle(img,(z1,z2),10,(255,0,9),cv2.FILLED)\n",
" \n",
" volRange = volume.GetVolumeRange()\n",
" minVol = volRange[0]\n",
" maxVol = volRange[1]\n",
" vol = np.interp(length,[50,300],[minVol,maxVol])\n",
" volBar = np.interp(length, [50,300],[400,150])\n",
" volPer = np.interp(length, [50,300],[0,100])\n",
" volume.SetMasterVolumeLevel(vol,None)\n",
" cv2.rectangle(img,(50,150),(85,400),(191,62,255),3)\n",
" cv2.rectangle(img,(50,int(volBar)),(85,400),(191,62,255),cv2.FILLED)\n",
" cv2.putText(img,str(int(volPer)),(40,100),cv2.FONT_HERSHEY_DUPLEX,4,(126,58,234))\n",
" \n",
" \n",
" cv2.imshow(\"Image\",img) \n",
" cv2.waitKey(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5735e95",
"metadata": {},
"outputs": [],
"source": [
"#length 50-300\n",
"#volRange -24 to +21"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}