我正在尝试根据是否在网络摄像头源中检测到人脸来自动切换OBS场景。我正在使用OBS WebSocket API连接到OBS并在场景之间切换。但是,我遇到了一个问题,即使检测到人脸,场景也不会切换。程序运行没有任何错误,但是调试输出显示当前场景总是空字典{}。下面是我的代码示例:
import cv2
import time
import numpy as np
from obswebsocket import obsws, requests, events
# OBS WebSocket connection settings
host = "localhost"
port = 4455
password = "PLACEHOLDER"
# Scene names
live_scene = "LIVE"
afk_scene = "AFK"
# Global variable to store the current scene name
current_scene_name = ""
# Connect to OBS
ws = obsws(host, port, password)
print("Connecting to OBS WebSocket at {}:{}".format(host, port))
ws.connect()
try:
version_info = ws.call(requests.GetVersion())
print("Connected to OBS WebSocket version: {}.{}.{}".format(
version_info.data().get("majorVersion", "Unknown"),
version_info.data().get("minorVersion", "Unknown"),
version_info.data().get("patchVersion", "Unknown")
))
scenes = ws.call(requests.GetSceneList())
print("Available scenes:", [scene["name"] for scene in scenes.data()["scenes"]]) # Added debug output
except Exception as e:
print("Error connecting to OBS WebSocket:")
print(type(e))
print(e.args)
print(e)
# Initialize webcam
cap = cv2.VideoCapture(0)
# Initialize face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
while True:
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
if len(faces) > 0:
print("Face detected")
current_scene = ws.call(requests.GetCurrentScene())
print("Current scene:", current_scene.data()) # Added debug output
current_scene_name = current_scene.data().get("name", "Unknown")
ws.call(requests.SetCurrentScene(scene_name=live_scene))
print("Switching to scene:", live_scene)
else:
print("No face detected")
current_scene = ws.call(requests.GetCurrentScene())
print("Current scene:", current_scene.data()) # Added debug output
current_scene_name = current_scene.data().get("name", "Unknown")
ws.call(requests.SetCurrentScene(scene_name=afk_scene))
print("Switching to scene:", afk_scene)
time.sleep(1)
else:
print("Error reading frame from webcam")
break
cap.release()
cv2.destroyAllWindows()
字符串
我从OBS WebSocket得到了这样的回复:
已连接OBS WebSocket版本:未知.未知.未知当前场景:{}切换到场景:LIVE
似乎GetCurrentScene()函数没有返回正确的场景。有人能帮助我了解导致此问题的原因以及如何修复它吗?
1条答案
按热度按时间xriantvc1#
这里有一些我发现,可能会有所帮助:
字符串