Reputation: 1042
On Windows 10, how can I determine if an attached web camera is currently active without turning the camera on if it's off?
Currently, I'm able to attempt to take a photo with the camera, and if it fails, assume that the camera is in use. However, this means that the activity LED for the camera will turn on (since the camera is now in use). Since I'd like to check the status of the camera every few seconds, it's not feasible to use this method to determine if the camera is in use.
I've used both the Win32 and UWP tags, and will accept a solution that uses either API.
Upvotes: 2
Views: 2962
Reputation: 506
( update: See the answer below by Timmo for a version that works with newer 'Windows Apps'. Changes have been integrated into the gist in this post )
I found a tip here that mentions that registry keys under Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam\NonPackaged change when the webcam is in use.
There are keys under here that track timestamps on when an application uses the webcam. When the webcam is actively in use, it appears that the 'LastUsedTimeStop' is 0, so to tell if the webcam is actively in use we may be able to simply check if any application has a LastUsedTimeStop==0.
Here's a quick Python class to poll the registry for webcam usage: https://gist.github.com/cobryan05/8e191ae63976224a0129a8c8f376adc6
Example usage:
import time
from webcamDetect import WebcamDetect
webcamDetect = WebcamDetect()
while True:
print("Applications using webcam:")
for app in webcamDetect.getActiveApps():
print(app)
print("---")
time.sleep(1)
Upvotes: 6
Reputation: 2334
Here is a more comprehensive solution extended from @Chris O'Bryan 's solution for future readers, which takes into account the "modern" apps such as the Windows camera app:
"""Webcam usage"""
import winreg
KEY = winreg.HKEY_CURRENT_USER
SUBKEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CapabilityAccessManager\\ConsentStore\\webcam"
WEBCAM_TIMESTAMP_VALUE_NAME = "LastUsedTimeStop"
def get_active_camera_applications() -> list[str]:
"""Returns a list of apps that are currently using the webcam."""
def get_subkey_timestamp(subkey) -> int | None:
"""Returns the timestamp of the subkey"""
try:
value, _ = winreg.QueryValueEx(subkey, WEBCAM_TIMESTAMP_VALUE_NAME)
return value
except OSError:
pass
return None
active_apps = []
try:
key = winreg.OpenKey(KEY, SUBKEY)
# Enumerate over the subkeys of the webcam key
subkey_count, _, _ = winreg.QueryInfoKey(key)
# Recursively open each subkey and check the "LastUsedTimeStop" value.
# A value of 0 means the camera is currently in use.
for idx in range(subkey_count):
subkey_name = winreg.EnumKey(key, idx)
subkey_name_full = f"{SUBKEY}\\{subkey_name}"
subkey = winreg.OpenKey(KEY, subkey_name_full)
if subkey_name == "NonPackaged":
# Enumerate over the subkeys of the "NonPackaged" key
subkey_count, _, _ = winreg.QueryInfoKey(subkey)
for np_idx in range(subkey_count):
subkey_name_np = winreg.EnumKey(subkey, np_idx)
subkey_name_full_np = f"{SUBKEY}\\NonPackaged\\{subkey_name_np}"
subkey_np = winreg.OpenKey(KEY, subkey_name_full_np)
if get_subkey_timestamp(subkey_np) == 0:
active_apps.append(subkey_name_np)
else:
if get_subkey_timestamp(subkey) == 0:
active_apps.append(subkey_name)
winreg.CloseKey(subkey)
winreg.CloseKey(key)
except OSError:
pass
return active_apps
active_apps = get_active_camera_applications()
if len(active_apps) > 0:
print("Camera is in use by the following apps:")
for app in active_apps:
print(f" {app}")
else:
print("Camera is not in use.")
Upvotes: 2
Reputation: 32775
Great question, but I'm afraid there is no such api to return the camera state for each second, after research I found the similar case here, and our member has provided a sample way to detect the camera state from FileLoadException
, I think this is only way to check the camera state currently.
Upvotes: 0