1、输入数据:
数据为城管视频监控,违规行为包括垃圾桶满溢、机动车违停、非机动车违停等
标注文件为json,包括违规行为帧编号,违规行为id,违规行为类别和bbox
2、代码部分:
!/opt/miniconda/bin/pip install opencv-python pandas matplotlib ultralytics
pip install各种需要用到的包
在第3 个cell下载数据并解压
第6个cell
video_path = '训练集(有标注第一批)/视频/45.mp4'
cap = cv2.VideoCapture(video_path)
while True:
# 读取下一帧
ret, frame = cap.read()
if not ret:
break
break
使用cv2.VideoCapture读取帧,ret是一个bool,表示是否成功读取,frame是帧信息
第8-9个cell:
int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# 结果:422
bbox = [746, 494, 988, 786]
pt1 = (bbox[0], bbox[1])
pt2 = (bbox[2], bbox[3])
color = (0, 255, 0)
thickness = 2 # 线条粗细
cv2.rectangle(frame, pt1, pt2, color, thickness)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
plt.imshow(frame)
cap.get(cv2.CAP_PROP_FRAME_COUNT)获取帧数信息
cell 9 是把该帧上的框画出来并展示
cell10:创建yolo的yaml配置文件
if not os.path.exists('yolo-dataset/'):
os.mkdir('yolo-dataset/')
if not os.path.exists('yolo-dataset/train'):
os.mkdir('yolo-dataset/train')
if not os.path.exists('yolo-dataset/val'):
os.mkdir('yolo-dataset/val')
dir_path = os.path.abspath('./') + '/'
# 需要按照你的修改path
with open('yolo-dataset/yolo.yaml', 'w', encoding='utf-8') as up:
up.write(f'''
path: {dir_path}/yolo-dataset/
train: train/
val: val/
names:
0: 非机动车违停
1: 机动车违停
2: 垃圾桶满溢
3: 违法经营
''')
cell11:
train_annos = glob.glob('训练集(有标注第一批)/标注/*.json')
train_videos = glob.glob('训练集(有标注第一批)/视频/*.mp4')
train_annos.sort(); train_videos.sort();
category_labels = ["非机动车违停", "机动车违停", "垃圾桶满溢", "违法经营"]
train_annos, train_videos存储了标注和视频信息,并排序使得其一一对应
cell12:
#用zip把前五个标注和视频配对,
for anno_path, video_path in zip(train_annos[:5], train_videos[:5]):
print(video_path)
anno_df = pd.read_json(anno_path)
cap = cv2.VideoCapture(video_path)
frame_idx = 0
#并且逐帧处理视频,
while True:
ret, frame = cap.read()
if not ret:
break
img_height, img_width = frame.shape[:2]
#根据帧索引找到标注数据,
frame_anno = anno_df[anno_df['frame_id'] == frame_idx]
#将每一帧图像保存为jpg,
cv2.imwrite('./yolo-dataset/train/' + anno_path.split('/')[-1][:-5] + '_' + str(frame_idx) + '.jpg', frame)
if len(frame_anno) != 0:
#并且生成标签保存标注的类别和边框
with open('./yolo-dataset/train/' + anno_path.split('/')[-1][:-5] + '_' + str(frame_idx) + '.txt', 'w') as up:
for category, bbox in zip(frame_anno['category'].values, frame_anno['bbox'].values):
category_idx = category_labels.index(category)
x_min, y_min, x_max, y_max = bbox
x_center = (x_min + x_max) / 2 / img_width
y_center = (y_min + y_max) / 2 / img_height
width = (x_max - x_min) / img_width
height = (y_max - y_min) / img_height
if x_center > 1:
print(bbox)
up.write(f'{category_idx} {x_center} {y_center} {width} {height}\n')
frame_idx += 1
用zip把前五个标注和视频配对,并且逐帧处理视频,根据帧索引找到标注数据,将每一帧图像保存为jpeg,并且生成标签保存标注的类别和边框
cell14、15
# 下载预训练yolov8模型
!wget http://mirror.coggle.club/yolo/yolov8n-v8.2.0.pt -O yolov8n.pt
#下载字体
!mkdir -p ~/.config/Ultralytics/
!wget http://mirror.coggle.club/yolo/Arial.ttf -O ~/.config/Ultralytics/Arial.ttf
cell16
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import warnings
warnings.filterwarnings('ignore')
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
results = model.train(data="yolo-dataset/yolo.yaml", epochs=2, imgsz=1080, batch=16)
训练模型
cell18
from ultralytics import YOLO
# 加载yolo模型
model = YOLO("runs/detect/train/weights/best.pt")
import glob
# 遍历视频文件
for path in glob.glob('测试集/*.mp4'):
submit_json = []
# 使用模型进行目标检测
results = model(path, conf=0.05, imgsz=1080, verbose=False)
# 对每一帧检测结果进行处理
for idx, result in enumerate(results):
# 提取边界框、类别、概率等等
boxes = result.boxes # Boxes object for bounding box outputs
masks = result.masks # Masks object for segmentation masks outputs
keypoints = result.keypoints # Keypoints object for pose outputs
probs = result.probs # Probs object for classification outputs
obb = result.obb # Oriented boxes object for OBB outputs
if len(boxes.cls) == 0:
continue
xywh = boxes.xyxy.data.cpu().numpy().round()
cls = boxes.cls.data.cpu().numpy().round()
conf = boxes.conf.data.cpu().numpy()
# 将结果保存为json
for i, (ci, xy, confi) in enumerate(zip(cls, xywh, conf)):
submit_json.append(
{
'frame_id': idx,
'event_id': i+1,
'category': category_labels[int(ci)],
'bbox': list([int(x) for x in xy]),
"confidence": float(confi)
}
)
with open('./result/' + path.split('/')[-1][:-4] + '.json', 'w', encoding='utf-8') as up:
json.dump(submit_json, up, indent=4, ensure_ascii=False)
3、心得:虽然是一键运行的代码,但是不代表可以点一个运行就算结束。之前没有玩过yolo和目标检测,这次对目标检测有了一个初步的认识,也对训练流程有了个大致的理解。接下来打算抽空学习学习各种yolo模型和其他目标检测模型