上传文件至 /
这个提交包含在:
25
config.py
普通文件
25
config.py
普通文件
@ -0,0 +1,25 @@
|
|||||||
|
# 配置文件 - 存储所有可调整的参数
|
||||||
|
|
||||||
|
# 数据集路径
|
||||||
|
POSITIVE_IMAGES_DIR = "data/positive"
|
||||||
|
NEGATIVE_IMAGES_DIR = "data/negative"
|
||||||
|
TEST_IMAGES_DIR = "data/test"
|
||||||
|
VIDEO_PATH = "data/videos/sample.mp4"
|
||||||
|
MODEL_PATH = "models/cascade.xml"
|
||||||
|
|
||||||
|
# 训练参数
|
||||||
|
OBJECT_WIDTH = 50
|
||||||
|
OBJECT_HEIGHT = 50
|
||||||
|
NUM_POSITIVE_SAMPLES = 1000
|
||||||
|
NUM_NEGATIVE_SAMPLES = 2000
|
||||||
|
NUM_STAGES = 15
|
||||||
|
|
||||||
|
# 检测参数
|
||||||
|
SCALE_FACTOR = 1.1
|
||||||
|
MIN_NEIGHBORS = 5
|
||||||
|
MIN_SIZE = (30, 30)
|
||||||
|
MAX_SIZE = (500, 500)
|
||||||
|
|
||||||
|
# 导出设置
|
||||||
|
EXPORT_DIR = "exports"
|
||||||
|
EXPORT_IMAGE_QUALITY = 95
|
||||||
46
data_preparation.py
普通文件
46
data_preparation.py
普通文件
@ -0,0 +1,46 @@
|
|||||||
|
import cv2
|
||||||
|
import os
|
||||||
|
import glob
|
||||||
|
from config import *
|
||||||
|
from utils import create_directory
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_positive_samples():
|
||||||
|
"""准备正样本数据"""
|
||||||
|
create_directory(POSITIVE_IMAGES_DIR)
|
||||||
|
|
||||||
|
# 这里应该是收集正样本图像的代码
|
||||||
|
# 在实际项目中,您可能需要从视频中提取或下载图像
|
||||||
|
print(f"请将正样本图像放在 {POSITIVE_IMAGES_DIR} 目录中")
|
||||||
|
print("正样本应该是包含待检测物体的图像")
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_negative_samples():
|
||||||
|
"""准备负样本数据"""
|
||||||
|
create_directory(NEGATIVE_IMAGES_DIR)
|
||||||
|
|
||||||
|
# 这里应该是收集负样本图像的代码
|
||||||
|
print(f"请将负样本图像放在 {NEGATIVE_IMAGES_DIR} 目录中")
|
||||||
|
print("负样本应该是不包含待检测物体的图像")
|
||||||
|
|
||||||
|
|
||||||
|
def create_description_files():
|
||||||
|
"""创建训练描述文件"""
|
||||||
|
# 创建正样本描述文件
|
||||||
|
with open('positive.txt', 'w') as f:
|
||||||
|
for img_path in glob.glob(f"{POSITIVE_IMAGES_DIR}/*.jpg"):
|
||||||
|
f.write(f"{img_path} 1 0 0 {OBJECT_WIDTH} {OBJECT_HEIGHT}\n")
|
||||||
|
|
||||||
|
# 创建负样本列表文件
|
||||||
|
with open('negatives.txt', 'w') as f:
|
||||||
|
for img_path in glob.glob(f"{NEGATIVE_IMAGES_DIR}/*.jpg"):
|
||||||
|
f.write(f"{img_path}\n")
|
||||||
|
|
||||||
|
print("描述文件已创建: positive.txt 和 negatives.txt")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
prepare_positive_samples()
|
||||||
|
prepare_negative_samples()
|
||||||
|
create_description_files()
|
||||||
|
print("数据准备完成!")
|
||||||
53
utils.py
普通文件
53
utils.py
普通文件
@ -0,0 +1,53 @@
|
|||||||
|
import cv2
|
||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
from config import *
|
||||||
|
|
||||||
|
|
||||||
|
def create_directory(path):
|
||||||
|
"""创建目录如果不存在"""
|
||||||
|
if not os.path.exists(path):
|
||||||
|
os.makedirs(path)
|
||||||
|
|
||||||
|
|
||||||
|
def resize_image(image, width=None, height=None):
|
||||||
|
"""调整图像大小"""
|
||||||
|
h, w = image.shape[:2]
|
||||||
|
|
||||||
|
if width is None and height is None:
|
||||||
|
return image
|
||||||
|
|
||||||
|
if width is None:
|
||||||
|
ratio = height / float(h)
|
||||||
|
dim = (int(w * ratio), height)
|
||||||
|
else:
|
||||||
|
ratio = width / float(w)
|
||||||
|
dim = (width, int(h * ratio))
|
||||||
|
|
||||||
|
return cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
|
||||||
|
|
||||||
|
|
||||||
|
def draw_detection(frame, x, y, w, h, label, confidence=None):
|
||||||
|
"""在图像上绘制检测结果"""
|
||||||
|
color = (0, 255, 0) # 绿色
|
||||||
|
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
|
||||||
|
|
||||||
|
label_text = label
|
||||||
|
if confidence is not None:
|
||||||
|
label_text += f" {confidence:.2f}%"
|
||||||
|
|
||||||
|
cv2.putText(frame, label_text, (x, y - 10),
|
||||||
|
cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
|
||||||
|
|
||||||
|
return frame
|
||||||
|
|
||||||
|
|
||||||
|
def preprocess_image(image):
|
||||||
|
"""图像预处理"""
|
||||||
|
# 转换为灰度图
|
||||||
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
|
# 直方图均衡化
|
||||||
|
gray = cv2.equalizeHist(gray)
|
||||||
|
|
||||||
|
return gray
|
||||||
97
video_detection.py
普通文件
97
video_detection.py
普通文件
@ -0,0 +1,97 @@
|
|||||||
|
import cv2
|
||||||
|
import time
|
||||||
|
from config import *
|
||||||
|
from utils import draw_detection, preprocess_image, create_directory
|
||||||
|
from export_data import export_detection_data
|
||||||
|
|
||||||
|
|
||||||
|
def video_detection():
|
||||||
|
"""视频文件检测"""
|
||||||
|
# 加载训练好的分类器
|
||||||
|
cascade = cv2.CascadeClassifier(MODEL_PATH)
|
||||||
|
|
||||||
|
if cascade.empty():
|
||||||
|
print("错误: 无法加载分类器文件")
|
||||||
|
return
|
||||||
|
|
||||||
|
# 打开视频文件
|
||||||
|
cap = cv2.VideoCapture(VIDEO_PATH)
|
||||||
|
|
||||||
|
if not cap.isOpened():
|
||||||
|
print(f"错误: 无法打开视频文件 {VIDEO_PATH}")
|
||||||
|
return
|
||||||
|
|
||||||
|
# 获取视频属性
|
||||||
|
fps = cap.get(cv2.CAP_PROP_FPS)
|
||||||
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||||
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||||
|
|
||||||
|
# 创建视频写入器
|
||||||
|
create_directory("exports")
|
||||||
|
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
||||||
|
out = cv2.VideoWriter('exports/output_video.avi', fourcc, fps, (width, height))
|
||||||
|
|
||||||
|
detection_data = []
|
||||||
|
frame_count = 0
|
||||||
|
|
||||||
|
print("视频检测已启动,按 'q' 退出")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
ret, frame = cap.read()
|
||||||
|
if not ret:
|
||||||
|
break
|
||||||
|
|
||||||
|
frame_count += 1
|
||||||
|
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000.0
|
||||||
|
|
||||||
|
# 预处理图像
|
||||||
|
gray = preprocess_image(frame)
|
||||||
|
|
||||||
|
# 检测物体
|
||||||
|
objects = cascade.detectMultiScale(
|
||||||
|
gray,
|
||||||
|
scaleFactor=SCALE_FACTOR,
|
||||||
|
minNeighbors=MIN_NEIGHBORS,
|
||||||
|
minSize=MIN_SIZE,
|
||||||
|
maxSize=MAX_SIZE
|
||||||
|
)
|
||||||
|
|
||||||
|
# 绘制检测结果
|
||||||
|
for i, (x, y, w, h) in enumerate(objects):
|
||||||
|
frame = draw_detection(frame, x, y, w, h, "Object")
|
||||||
|
|
||||||
|
# 记录检测数据
|
||||||
|
detection_data.append({
|
||||||
|
"frame": frame_count,
|
||||||
|
"timestamp": timestamp,
|
||||||
|
"object_id": i,
|
||||||
|
"x": x,
|
||||||
|
"y": y,
|
||||||
|
"width": w,
|
||||||
|
"height": h
|
||||||
|
})
|
||||||
|
|
||||||
|
# 写入输出视频
|
||||||
|
out.write(frame)
|
||||||
|
|
||||||
|
# 显示结果
|
||||||
|
cv2.imshow('Video Object Detection', frame)
|
||||||
|
|
||||||
|
# 按'q'退出
|
||||||
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||||
|
break
|
||||||
|
|
||||||
|
# 释放资源
|
||||||
|
cap.release()
|
||||||
|
out.release()
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
# 导出检测数据
|
||||||
|
export_detection_data(detection_data, "video_detection.csv")
|
||||||
|
print("检测完成!")
|
||||||
|
print(f"输出视频已保存到: exports/output_video.avi")
|
||||||
|
print(f"检测数据已导出到: exports/video_detection.csv")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
video_detection()
|
||||||
32
说明
普通文件
32
说明
普通文件
@ -0,0 +1,32 @@
|
|||||||
|
## 项目结构
|
||||||
|
|
||||||
|
- `data_preparation.py`: 数据准备和预处理
|
||||||
|
- `train_model.py`: 模型训练
|
||||||
|
- `realtime_detection.py`: 实时摄像头检测
|
||||||
|
- `video_detection.py`: 视频文件检测
|
||||||
|
- `export_data.py`: 数据导出功能
|
||||||
|
- `utils.py`: 公用工具函数
|
||||||
|
- `config.py`: 配置文件
|
||||||
|
|
||||||
|
## 安装依赖
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
|
||||||
|
python data_preparation.py
|
||||||
|
python train_model.py
|
||||||
|
python realtime_detection.py
|
||||||
|
python video_detection.py
|
||||||
|
python export_data.py
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 使用说明
|
||||||
|
|
||||||
|
1. 安装依赖:`pip install -r requirements.txt`
|
||||||
|
2. 准备正样本和负样本图像到指定目录
|
||||||
|
3. 运行 `data_preparation.py` 创建描述文件
|
||||||
|
4. 运行 `train_model.py` 训练模型(需要较长时间)
|
||||||
|
5. 运行 `realtime_detection.py` 进行实时检测或 `video_detection.py` 进行视频检测
|
||||||
|
6. 使用 `export_data.py` 导出检测结果
|
||||||
在新工单中引用
屏蔽一个用户