week 2020.12.27-2020.12.31

自由学习,大佬给了一个脚本,用在yolov5中,实现预测完图像生成图像对应的xml文件,方便在labelimage中进行查看:

View Code

直接调用模型生成的.pt,检测完结果生成跟检测图像对应得xml文件。

学习到了glob函数的使用:是python自己带的一个文件操作相关模块,用它可以查找符合自己目的的文件,就类似于Windows下的文件搜索,支持通配符操作,*,?,[]这三个通配符,*代表0个或多个字符,?代表一个字符,[]匹配指定范围内的字符,如[0-9]匹配数字。

比如:

glob.glob(r'c:\*.txt')

我这里就是获得C盘下的所有txt文件

glob.glob(r'E:\pic\*\*.jpg')

获得指定目录下的所有jpg文件

使用相对路径:

glob.glob(r'../*.py')

接着就是遍历文件夹下所有路径,可以插入使用tqdm(进度条功能):

image_path = glob.glob('data/input/CardDetection/images/*.jpg')
for i in tqdm(image_path):
    image_path = i.replace('\\', '/')
    # print(image_path)
    print(predict(image_path), '/n')
.replace负责将路径中'\'改为‘/’
一个视屏按照帧来分成图片的脚本:
#coding: utf-8
import os
import cv2
from tqdm import tqdm
from glob import glob

if __name__ == "__main__":
    filelists = glob('./little/*.mp4')
    save = './pic/'
    os.makedirs(save, exist_ok=True)
    FPS = 25
    for item in filelists:
        cap = cv2.VideoCapture(item)
        frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)  # 获得视频文件的帧数

        fps = 50
        # 保存图片的帧率间隔
        count = 50
        num = 0
        while(cap.isOpened()):
            ret, img = cap.read()
            if img is None:
                break

            if num % 100*FPS == 0:
                cv2.imwrite(save + str(len(os.listdir(save)) + 1).zfill(5) + '.jpg', img)
            num += 1
            if num == 1:
                print(filelists)
            # print(num)

        cap.release()

写了一个标签文件为.csv格式的转化为yolo格式的脚本,标签文件为下图样式:

View Code


 

posted @ 2020-12-31 16:56  cn_gzb  阅读(154)  评论(0编辑  收藏  举报