天天看點

python常用程式代碼(持續更新收集)多張圖檔生成視訊代碼删除檔案和檔案夾資料集元素分割一張圖檔均勻切割

檔案夾周遊和檔案周遊

# 周遊檔案夾和檔案,傳回檔案的路徑和對應的檔案的名稱,同時可以根據自己的應用需要進行更改
def get_img_file(file_name):
    imagelist = []
    for parent, dirnames, filenames in os.walk(file_name):
        for filename in filenames:
            if filename.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')):
                imagelist.append(os.path.join(parent, filename))
        return imagelist,filenames
           

隻周遊目前檔案或者隻周遊目前檔案夾

def GetDirectory(path):
    directory=os.listdir(path)
    directory_list=[]
    for dirs in directory:
        if not os.path.isfile(os.path.join(path,dirs)):
           directory_list.append(os.path.join(path,dirs))
    return directory_list
def GetFile(path):
    files=os.listdir(path)
    file_list=[]
    for file in files:
        if os.path.isfile(os.path.join(path,file)):
           file_list.append(os.path.join(path,file))
    return file_list


           

和上段代碼的功能類似,周遊根目錄下的所有檔案夾和檔案,隻是這個是組個周遊,可以人為的調整和修改:

def preprocess(src_root, dst_root):
    """
    :param src_root:
    :param dst_root:
    :return:
    """
    if not os.path.isdir(src_root):
        print("[Err]: invalid source root")
        return

    if not os.path.isdir(dst_root):
        os.makedirs(dst_root)
        print("{} made".format(dst_root))

    # 建立用于訓練MOT的目錄結構
    dst_img_dir_train = dst_root + '/images/train'
    dst_img_dir_test = dst_root + '/images/test'
    dst_labels_with_ids = dst_root + '/labels_with_ids'
    if not os.path.isdir(dst_img_dir_train):
        os.makedirs(dst_img_dir_train)
    if not os.path.isdir(dst_img_dir_test):
        os.makedirs(dst_img_dir_test)
    if not os.path.isdir(dst_labels_with_ids):
        os.makedirs(dst_labels_with_ids)

    # 周遊src_root, 進一步完善訓練目錄并拷貝檔案
    for x in os.listdir(src_root):
        x_path = src_root + '/' + x
        if os.path.isdir(x_path):
            for y in os.listdir(x_path):
                if y.endswith('.jpg'):
                    y_path = x_path + '/' + y
                    if os.path.isfile(y_path):
                        # 建立用于訓練的圖檔目标目錄
                        dst_img1_dir = dst_img_dir_train + '/' + x + '/img1'
                        if not os.path.isdir(dst_img1_dir):
                            os.makedirs(dst_img1_dir)

                        # copy image to train image dir
                        dst_f_path = dst_img1_dir + y
                        if os.path.isfile(dst_f_path):
                            shutil.copy(y_path, dst_img1_dir)
                            print('{} cp to {}'.format(y, dst_img1_dir))
                        else:
                            print('{} already exists.'.format(dst_f_path))
           

多張圖檔生成視訊代碼

import os
import cv2

def generate_video(path,size,fps=25):
    # fps = 24                  #幀率
    # size = (640, 480)
    videowriter = cv2.VideoWriter("./resultout/test.mp4",cv2.VideoWriter_fourcc(*'mp4v'),fps,size)
    #path = r'F:/data/predict_landmark/'
    for rootpath, dirs, names in os.walk(path):
        names.sort()
        for name in names:
            img = cv2.imread(os.path.join(rootpath, name))
            print("--", name)
            videowriter.write(img)
    videowriter.release()

generate_video('./resultout/frame/',(1920, 1080),25)  
           

删除檔案和檔案夾

需要在執行某些代碼前清空指定的檔案夾,如果直接用os.remove(),可能出現因檔案夾中檔案被占用而無法删除,解決方法也很簡單,先強制删除檔案夾,再重建立同名檔案夾即可 

import shutil
shutil.rmtree('要清空的檔案夾名')
os.mkdir('要清空的檔案夾名')
           

把一個檔案從一個檔案夾移動到另一個檔案夾,并同時重命名,用shutil也很簡單

shutil.move('原檔案夾/原檔案名','目标檔案夾/目标檔案名')
           

同理一個檔案夾的檔案複制到另一個檔案夾可以使用下面的代碼:

shutil.copy("源目錄/檔案名", "目的目錄/檔案名")
           

資料集元素分割

這裡有多種方式,先上一個簡單的,然後來一個比較完全的。

import os
import random


def main():
    random.seed(0)  # 設定随機種子,保證随機結果可複現

    files_path = "F:\\車道線大圖"
    assert os.path.exists(files_path), "path: '{}' does not exist.".format(files_path)

    val_rate = 0.5
    # 讀取資料,并周遊圖檔名稱,通過'.'進行分割字元串,取第一部分,然後排序
    files_name = sorted([file.split(".")[0] for file in os.listdir(files_path)])
    files_num = len(files_name)
    # 生成随機索引,k表示從輸入的序列中随機選擇k個無重複的元素
    val_index = random.sample(range(0, files_num), k=int(files_num*val_rate))
    train_files = []
    val_files = []
    for index, file_name in enumerate(files_name):
        if index in val_index:
            val_files.append(file_name)
        else:
            train_files.append(file_name)

    try:
        train_f = open("train.txt", "x")
        eval_f = open("val.txt", "x")
        train_f.write("\n".join(train_files))
        eval_f.write("\n".join(val_files))
    except FileExistsError as e:
        print(e)
        exit(1)
           
'''
Description: 學習使用
Version: 1.0
Author: 趙守風
Email: [email protected]
Date: 2021-03-24 14:17:19
LastEditors: zsf
LastEditTime: 2021-03-24 17:06:01
'''

import torch
import os
import random
import shutil
import math



'''
@description: 直接切分資料集,copy資料到對應的訓練集、測試集和驗證集,這裡不考慮類别的問題
@param {*} datapath: 原始資料路徑
@param {*} rootpath: 儲存切分後的路徑root
@param {*} train_rate: 訓練集占總數的百分比
@param {*} val_rate: 驗證集占總數百分比
@param {*} test_rate: 測試集占總數的百分比
@return {*}
@author: zsf
'''
def data_split(datapath, rootpath="./data_split", train_rate=0.8, val_rate=0.1, test_rate=0.1):
    for parent, dirnames, filenames in os.walk(datapath):
        for dirname in dirnames:
            filepaths = os.listdir(os.path.join(parent, dirname))
            # 删除其他的檔案
            filepaths = [x for x in filepaths if x.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')) ]           
            # 開始切分
            random.shuffle(filepaths)    
            for i in range(len(filepaths)):
                if i < math.floor(train_rate*len(filepaths)):
                    sub_path = os.path.join(rootpath, 'train_set', dirname)
                elif i < math.floor((train_rate+val_rate)*len(filepaths)):
                    sub_path = os.path.join(rootpath, 'val_set', dirname)
                elif i < len(filepaths):
                    sub_path = os.path.join(rootpath, 'test_set', dirname)
                if os.path.exists(sub_path) == 0:
                    os.makedirs(sub_path)
                shutil.copy(os.path.join(datapath, dirname, filepaths[i]), os.path.join(sub_path, filepaths[i]))   # 複制圖檔,從源到目的地




'''
@description: 這裡生成對應的txt檔案不挪動圖檔
@param {*} datapath: 原始資料路徑
@param {*} rootpath: 儲存切分後的路徑root
@param {*} train_rate: 訓練集占總數的百分比
@param {*} val_rate: 驗證集占總數百分比
@param {*} test_rate: 測試集占總數的百分比
@return {*}
@author: zsf
'''
def data_split_txt(datapath, rootpath="./data_split", train_rate=0.8, val_rate=0.1, test_rate=0.1):
    for parent, dirnames, filenames in os.walk(datapath):
        index = 0
        for dirname in dirnames:
            filepaths = os.listdir(os.path.join(parent, dirname))
            # 删除其他的檔案
            filepaths = [x for x in filepaths if x.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')) ]           
            # 開始切分
            random.shuffle(filepaths)    
            for i in range(len(filepaths)):
                if i < math.floor(train_rate*len(filepaths)):
                    txt_path = 'train_set.txt'
                elif i < math.floor((train_rate+val_rate)*len(filepaths)):
                    txt_path = 'val_set.txt'
                elif i < len(filepaths):
                    txt_path = 'test_set.txt'
                with open(os.path.join(rootpath, txt_path), mode='a') as file:
                    file.write(str(index) + ' ' + os.path.join(datapath, dirname, filepaths[i]) + '\n')     
            index += 1 
           

一張圖檔均勻切割

import os
from PIL import Image
path="D:\\SR\\USRNet-master\\USRNet-master\\testsets\\image\\20.jpg"
smallpath='D:\\SR\\USRNet-master\\USRNet-master\\testsets\\small'
def splitimage(src, rownum, colnum, dstpath):
    img = Image.open(src)
    w, h = img.size
    if rownum <= h and colnum <= w:
        print('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode))
        print('開始處理圖檔切割, 請稍候...')

        s = os.path.split(src)
        if dstpath == '':
            dstpath = s[0]
        fn = s[1].split('.')
        basename = fn[0]
        ext = fn[-1]

        num = 0
        rowheight = h // rownum
        colwidth = w // colnum
        for r in range(rownum):
            for c in range(colnum):
                box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)
                p = os.path.join(dstpath, basename + '_' + str(num) + '.' + ext)
                img.crop(box).save(p)
                num = num + 1
                print("all = {}, num = {}".format(rownum*colnum, num))

        print('圖檔切割完畢,共生成 %s 張小圖檔。' % num)
    else:
        print('不合法的行列切割參數!')

splitimage(path, 10, 10, smallpath)
           

視訊轉圖檔

cap = cv2.VideoCapture('D:/SR/src/2.mp4')
i =0
while(1):
    ret, frame = cap.read()
    #cv2.imshow("image",frame)
    cv2.imwrite("D:/SR/USRNet-master/USRNet-master/testsets/image/"+str(i)+'.jpg',frame)
    i+=1
           

路徑字元串處理 

def save_image(output_dir, image_path):
    if not os.path.exists(output_dir):
            os.makedirs(output_dir)
    image_name = os.path.split(image_path)[-1]
    name, ext = os.path.splitext(image_name)
    return os.path.join(output_dir, "{}".format(name)) + ext
           

繼續閱讀