first commit
This commit is contained in:
commit
2d244a1858
|
|
@ -0,0 +1,4 @@
|
|||
.venv
|
||||
.idea
|
||||
yolo
|
||||
runs
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
from ultralytics import YOLO
|
||||
|
||||
yolo = YOLO('models/yolov8n.pt')
|
||||
# yolo('./resources/01.png',show=True,save=True)
|
||||
# yolo('./resources/FHC09594.jpg',show=True,save=True)
|
||||
yolo('./resources/input.mp4',show=True,save=True)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
from ultralytics import YOLO
|
||||
|
||||
# 加载预训练模型
|
||||
yolo = YOLO('models/yolov8n.pt')
|
||||
# 训练模型
|
||||
# data:数据集路径
|
||||
# epochs:训练轮数
|
||||
# imgsz: 图片大小
|
||||
# batch: 批次大小
|
||||
# device: 使用设备 0:GPU 'cpu':CPU
|
||||
|
||||
yolo.train(data='train.yaml', epochs=200, imgsz=640,batch=32,device='cpu')
|
||||
print("训练完成")
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
from ultralytics import YOLO
|
||||
|
||||
# 加载训练好的模型
|
||||
# best.pt: 最佳模型,适用于生产
|
||||
# last.pt: 最后一轮训练的模型,适用于继续训练
|
||||
yolo = YOLO('runs/detect/train4/weights/best.pt')
|
||||
# yolo('resources/input.mp4', show=True, save=True)
|
||||
yolo('dataset/val/2.png', show=True, save=True)
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
from ultralytics import YOLO
|
||||
import cv2
|
||||
import pyautogui
|
||||
import numpy as np
|
||||
|
||||
|
||||
|
||||
# 加载训练好的模型
|
||||
# best.pt: 最佳模型,适用于生产
|
||||
# last.pt: 最后一轮训练的模型,适用于继续训练
|
||||
yolo = YOLO('runs/detect/train2/weights/best.pt')
|
||||
|
||||
# 指定屏幕范围
|
||||
# x,y,width,height 全屏None
|
||||
window = None
|
||||
while True:
|
||||
# 获取屏幕截图
|
||||
screenshot = pyautogui.screenshot()
|
||||
frame = np.array(screenshot)
|
||||
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
||||
|
||||
# 使用YOLO检测
|
||||
results = yolo(frame)
|
||||
|
||||
# 显示结果
|
||||
annotated_frame = results[0].plot()
|
||||
cv2.imshow("Screen Detection", annotated_frame)
|
||||
|
||||
# 按'q'退出
|
||||
if cv2.waitKey(1) == ord('q'):
|
||||
break
|
||||
|
||||
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
import cv2
|
||||
import numpy as np
|
||||
from ultralytics import YOLO
|
||||
from moviepy import VideoFileClip # 用于处理音频(仅视频文件需要)
|
||||
|
||||
def apply_gaussian_blur(region, blur_strength=25):
|
||||
"""对目标区域应用高斯模糊"""
|
||||
return cv2.GaussianBlur(region, (blur_strength, blur_strength), 0)
|
||||
|
||||
def process_video(input_video, output_video, model, blur_strength=25, target_classes=None):
|
||||
"""处理视频并应用高斯模糊"""
|
||||
# 提取原始视频的音频(仅用于视频文件)
|
||||
original_clip = VideoFileClip(input_video)
|
||||
audio = original_clip.audio
|
||||
|
||||
# 读取视频
|
||||
cap = cv2.VideoCapture(input_video)
|
||||
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
||||
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||
|
||||
# 创建临时无音频视频
|
||||
temp_output = "temp_blurred_no_audio.mp4"
|
||||
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
||||
out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))
|
||||
|
||||
while cap.isOpened():
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
# YOLO 检测
|
||||
results = model(frame)
|
||||
|
||||
# 遍历检测结果
|
||||
for box in results[0].boxes:
|
||||
x1, y1, x2, y2 = map(int, box.xyxy[0]) # 检测框坐标
|
||||
cls_id = int(box.cls.item()) # 类别ID
|
||||
|
||||
# 如果指定了目标类别,只模糊这些类别
|
||||
if target_classes is None or cls_id in target_classes:
|
||||
# 提取检测区域并模糊
|
||||
roi = frame[y1:y2, x1:x2]
|
||||
blurred_roi = apply_gaussian_blur(roi, blur_strength)
|
||||
frame[y1:y2, x1:x2] = blurred_roi
|
||||
|
||||
# 写入输出视频
|
||||
out.write(frame)
|
||||
|
||||
# 显示实时结果(可选)
|
||||
cv2.imshow("YOLO Blur Detection", frame)
|
||||
if cv2.waitKey(1) == ord('q'):
|
||||
break
|
||||
|
||||
cap.release()
|
||||
out.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
# 合并音频(仅视频文件需要)
|
||||
blurred_clip = VideoFileClip(temp_output)
|
||||
final_clip = blurred_clip.with_audio(audio)
|
||||
final_clip.write_videofile(output_video, codec="libx264", audio_codec="aac")
|
||||
|
||||
# 删除临时文件(可选)
|
||||
import os
|
||||
os.remove(temp_output)
|
||||
|
||||
def main():
|
||||
# 加载 YOLO 模型(可以是 yolov8n.pt, yolov8s.pt 等)
|
||||
model = YOLO("runs/detect/train4/weights/best.pt") # 替换成你的模型
|
||||
|
||||
# 输入视频文件(如果是摄像头,设为 0)
|
||||
input_video = "resources/input1.mp4" # 或 0(摄像头)
|
||||
output_video = "output_blurred.mp4"
|
||||
|
||||
# 设置高斯模糊强度(越大越模糊)
|
||||
blur_strength = 79
|
||||
|
||||
# 指定要模糊的类别(可选,None 表示模糊所有检测到的目标)
|
||||
# COCO 数据集类别:0: person, 1: bicycle, 2: car, ..., 参见 https://cocodataset.org
|
||||
target_classes = [0] # 示例:只模糊人(person)
|
||||
|
||||
if input_video == 0:
|
||||
# 摄像头实时检测(无音频)
|
||||
cap = cv2.VideoCapture(0)
|
||||
while cap.isOpened():
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
break
|
||||
|
||||
# YOLO 检测 + 模糊
|
||||
results = model(frame)
|
||||
for box in results[0].boxes:
|
||||
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
||||
cls_id = int(box.cls.item())
|
||||
if target_classes is None or cls_id in target_classes:
|
||||
roi = frame[y1:y2, x1:x2]
|
||||
frame[y1:y2, x1:x2] = apply_gaussian_blur(roi, blur_strength)
|
||||
|
||||
cv2.imshow("Live Blur Detection", frame)
|
||||
if cv2.waitKey(1) == ord('q'):
|
||||
break
|
||||
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
else:
|
||||
# 处理视频文件(保留音频)
|
||||
process_video(input_video, output_video, model, blur_strength, target_classes)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -0,0 +1 @@
|
|||
logo
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 390 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 454 KiB |
Binary file not shown.
|
|
@ -0,0 +1 @@
|
|||
0 0.267786 0.495737 0.158273 0.103739
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
0 0.268585 0.198019 0.163070 0.034106
|
||||
0 0.499600 0.501421 0.001599 0.001421
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 390 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 454 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 1.6 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 19 MiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,5 @@
|
|||
path: ./dataset # 数据集根目录
|
||||
train: images # 训练集目录名
|
||||
val: val # 验证集目录名
|
||||
nc: 1 # 类别数
|
||||
names: [ 'logo' ] # 类别名称
|
||||
Loading…
Reference in New Issue