重构目录结构:调整项目布局
This commit is contained in:
67
vision/camera.py
Normal file
67
vision/camera.py
Normal file
@ -0,0 +1,67 @@
|
||||
# vision/camera.py
|
||||
import cv2
|
||||
|
||||
|
||||
class CameraController:
|
||||
def __init__(self):
|
||||
self.camera = None
|
||||
self.camera_type = "ip"
|
||||
self.camera_ip = "192.168.1.51"
|
||||
self.camera_port = 554
|
||||
self.camera_username = "admin"
|
||||
self.camera_password = "XJ123456"
|
||||
self.camera_channel = 1
|
||||
|
||||
def set_config(self, camera_type="ip", ip=None, port=None, username=None, password=None, channel=1):
|
||||
"""
|
||||
设置摄像头配置
|
||||
"""
|
||||
self.camera_type = camera_type
|
||||
if ip:
|
||||
self.camera_ip = ip
|
||||
if port:
|
||||
self.camera_port = port
|
||||
if username:
|
||||
self.camera_username = username
|
||||
if password:
|
||||
self.camera_password = password
|
||||
self.camera_channel = channel
|
||||
|
||||
def setup_capture(self, camera_index=0):
|
||||
"""
|
||||
设置摄像头捕获
|
||||
"""
|
||||
try:
|
||||
rtsp_url = f"rtsp://{self.camera_username}:{self.camera_password}@{self.camera_ip}:{self.camera_port}/streaming/channels/{self.camera_channel}01"
|
||||
self.camera = cv2.VideoCapture(rtsp_url)
|
||||
|
||||
if not self.camera.isOpened():
|
||||
print(f"无法打开网络摄像头: {rtsp_url}")
|
||||
return False
|
||||
print(f"网络摄像头初始化成功,地址: {rtsp_url}")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"摄像头设置失败: {e}")
|
||||
return False
|
||||
|
||||
def capture_frame(self):
|
||||
"""捕获当前帧并返回numpy数组"""
|
||||
try:
|
||||
if self.camera is None:
|
||||
print("摄像头未初始化")
|
||||
return None
|
||||
|
||||
ret, frame = self.camera.read()
|
||||
if ret:
|
||||
return frame
|
||||
else:
|
||||
print("无法捕获图像帧")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"图像捕获失败: {e}")
|
||||
return None
|
||||
|
||||
def release(self):
|
||||
"""释放摄像头资源"""
|
||||
if self.camera is not None:
|
||||
self.camera.release()
|
||||
Reference in New Issue
Block a user