feat: v1.1.0 - 图片按日期分文件夹保存、设置面板、序号显示模式、自动刷新
This commit is contained in:
27
camera.py
27
camera.py
@@ -2,22 +2,22 @@
|
||||
摄像头拍照模块 - Windows 兼容
|
||||
"""
|
||||
import cv2
|
||||
import os
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
from config import IMAGES_DIR, CAMERA_INDEX
|
||||
from config import config_mgr
|
||||
|
||||
|
||||
class CameraCapture:
|
||||
"""摄像头拍照管理"""
|
||||
|
||||
def __init__(self, camera_index=CAMERA_INDEX):
|
||||
self.camera_index = camera_index
|
||||
def __init__(self):
|
||||
self.camera_index = config_mgr.get('camera_index', 0)
|
||||
self.cap = None
|
||||
|
||||
def open(self):
|
||||
"""打开摄像头"""
|
||||
if self.cap is None or not self.cap.isOpened():
|
||||
self.camera_index = config_mgr.get('camera_index', 0)
|
||||
self.cap = cv2.VideoCapture(self.camera_index, cv2.CAP_DSHOW) # Windows DirectShow
|
||||
if not self.cap.isOpened():
|
||||
raise Exception(f"无法打开摄像头 {self.camera_index}")
|
||||
@@ -33,10 +33,10 @@ class CameraCapture:
|
||||
self.cap = None
|
||||
|
||||
def capture(self, save_path=None):
|
||||
"""拍照并保存
|
||||
"""拍照并保存(按日期文件夹组织)
|
||||
|
||||
Returns:
|
||||
dict: {'success': bool, 'path': str, 'timestamp': str, 'error': str}
|
||||
dict: {'success': bool, 'path': str, 'timestamp': str, 'date_folder': str, 'error': str}
|
||||
"""
|
||||
try:
|
||||
self.open()
|
||||
@@ -49,11 +49,17 @@ class CameraCapture:
|
||||
if not ret:
|
||||
return {'success': False, 'error': '无法捕获图像'}
|
||||
|
||||
# 生成文件名
|
||||
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
# 获取当前日期
|
||||
now = datetime.datetime.now()
|
||||
date_str = now.strftime('%Y-%m-%d')
|
||||
timestamp = now.strftime('%Y%m%d_%H%M%S')
|
||||
|
||||
# 获取按日期的保存目录
|
||||
date_folder = config_mgr.get_images_dir(date_str)
|
||||
|
||||
if save_path is None:
|
||||
filename = f"capture_{timestamp}.jpg"
|
||||
save_path = IMAGES_DIR / filename
|
||||
save_path = date_folder / filename
|
||||
|
||||
# 保存图片
|
||||
cv2.imwrite(str(save_path), frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
|
||||
@@ -62,6 +68,7 @@ class CameraCapture:
|
||||
'success': True,
|
||||
'path': str(save_path),
|
||||
'timestamp': timestamp,
|
||||
'date_folder': date_str,
|
||||
'filename': save_path.name
|
||||
}
|
||||
|
||||
@@ -112,7 +119,7 @@ if __name__ == "__main__":
|
||||
|
||||
if cams:
|
||||
print("\n测试拍照...")
|
||||
camera = CameraCapture(cams[0])
|
||||
camera = CameraCapture()
|
||||
result = camera.capture()
|
||||
print(f"结果: {result}")
|
||||
camera.close()
|
||||
Reference in New Issue
Block a user