如何构建网站,网站如何规划,厦门网站建设是什么意思,怎样做网站分析总结本篇文章给大家谈谈Python的代码c语言可以用吗#xff0c;以及python代码大全和用法#xff0c;希望对各位有所帮助#xff0c;不要忘了收藏本站喔。 深度学习的图片等比resize后#xff0c;再把图片反向resize回来#xff0c;验证通过 import cv2
import numpy as npdef …本篇文章给大家谈谈Python的代码c语言可以用吗以及python代码大全和用法希望对各位有所帮助不要忘了收藏本站喔。 深度学习的图片等比resize后再把图片反向resize回来验证通过 import cv2
import numpy as npdef restore_image(image, original_size):# 获取图像的原始宽度和高度height, width original_size# 计算缩放比例scale min(image.shape[1]/width, image.shape[0]/height)# 计算缩放后的宽度和高度new_width int(width * scale)new_height int(height * scale)# 计算填充位置x_offset (image.shape[1] - new_width) // 2y_offset (image.shape[0] - new_height) // 2# 将调整后的图像放置在原始图像中心restored_image image[y_offset:y_offset new_height, x_offset:x_offset new_width]# 调整图像大小# restored_image cv2.resize(restored_image, None,None,1/scale,1/scale,interpolationcv2.INTER_LANCZOS4)restored_image cv2.resize(restored_image,(width,height),interpolationcv2.INTER_LANCZOS4)return restored_imagedef resize_image(image, target_size, fill_value):# 获取图像的原始宽度和高度height, width image.shape[:2]# 计算缩放比例scale min(target_size[0] / width, target_size[1] / height)# 计算缩放后的宽度和高度new_width int(width * scale)new_height int(height * scale)# print( new_width {}, new_height {} .format(new_width, new_height))# 调整图像大小resized_image cv2.resize(image, (new_width, new_height),interpolationcv2.INTER_LANCZOS4)# 创建目标大小的空白图像并填充特定值padded_image np.full((target_size[1], target_size[0], image.shape[2]), fill_value, dtypenp.uint8)# 计算填充位置x_offset (target_size[0] - new_width) // 2y_offset (target_size[1] - new_height) // 2# print( x_offset {}, y_offset {} .format(x_offset, y_offset))# 将调整后的图像放置在填充图像中心padded_image[y_offset:y_offset new_height, x_offset:x_offset new_width] resized_imagereturn padded_image# 读取图像
image cv2.imread(1.jpg)# 设置目标大小这里设置为300x300
target_size (512, 512)# 设置填充值
fill_value 114# 进行图像缩放和填充
resized_image resize_image(image, target_size, fill_value)# 将缩放后的图像恢复到原始大小
restored_image restore_image(resized_image, (image.shape[0],image.shape[1]))cv2.imwrite(1_reszie.jpg,resized_image )
save_params [cv2.IMWRITE_JPEG_QUALITY, 100]
cv2.imwrite(1_restored.jpg,restored_image,save_params)
# # 显示原始图像和缩放后的图像
# cv2.imshow(Original Image, image)
# cv2.imshow(Resized Image, resized_image)
# cv2.waitKey(0)
# cv2.destroyAllWindows() 文件夹遍历和文件遍历 # 遍历文件夹和文件返回文件的路径和对应的文件的名称同时可以根据自己的应用需要进行更改
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):directoryos.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):filesos.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)returnif not os.path.isdir(dst_root):os.makedirs(dst_root)print({} made.format(dst_root))# 创建用于训练MOT的目录结构dst_img_dir_train dst_root /images/traindst_img_dir_test dst_root /images/testdst_labels_with_ids dst_root /labels_with_idsif 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 / xif os.path.isdir(x_path):for y in os.listdir(x_path):if y.endswith(.jpg):y_path x_path / yif os.path.isfile(y_path):# 创建用于训练的图片目标目录dst_img1_dir dst_img_dir_train / x /img1if not os.path.isdir(dst_img1_dir):os.makedirs(dst_img1_dir)# copy image to train image dirdst_f_path dst_img1_dir yif 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)) 等比resize import cv2
import matplotlib.pyplot as plt# 封装resize函数
def resize_img_keep_ratio(img_name,target_size):img cv2.imread(img_name) # 读取图片old_size img.shape[0:2] # 原始图像大小ratio min(float(target_size[i])/(old_size[i]) for i in range(len(old_size))) # 计算原始图像宽高与目标图像大小的比例并取其中的较小值new_size tuple([int(i*ratio) for i in old_size]) # 根据上边求得的比例计算在保持比例前提下得到的图像大小img cv2.resize(img,(new_size[1], new_size[0])) # 根据上边的大小进行放缩pad_w target_size[1] - new_size[1] # 计算需要填充的像素数目图像的宽这一维度上pad_h target_size[0] - new_size[0] # 计算需要填充的像素数目图像的高这一维度上top,bottom pad_h//2, pad_h-(pad_h//2)left,right pad_w//2, pad_w -(pad_w//2)img_new cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,None,(0,0,0)) return img_newif __name__ __main__:img rD:\SAMM\crop\006_1\1.jpg # 待处理的图片地址, 替换成你的地址就好target_size[224, 224] # 目标图像大小resized_img resize_img_keep_ratio(img, target_size) plt.imshow(resized_img)plt.show()多张图片生成视频代码 import os
import cv2def generate_video(path,size,fps25):# fps 24 #帧率# size (640, 480)videowriter cv2.VideoWriter(./resultout/test.mp4,cv2.VideoWriter_fourcc(*mp4v),fps,size)#path rF:/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 randomdef 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), kint(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)
Deion: 学习使用
Version: 1.0
Author: 赵守风
Email: 1583769112qq.com
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
deion: 直接切分数据集copy数据到对应的训练集、测试集和验证集这里不考虑类别的问题
param {*} datapath: 原始数据路径
param {*} rootpath: 保存切分后的路径root
param {*} train_rate: 训练集占总数的百分比
param {*} val_rate: 验证集占总数百分比
param {*} test_rate: 测试集占总数的百分比
return {*}
author: zsfdef data_split(datapath, rootpath./data_split, train_rate0.8, val_rate0.1, test_rate0.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_rateval_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])) # 复制图片从源到目的地
deion: 这里生成对应的txt文件不挪动图片
param {*} datapath: 原始数据路径
param {*} rootpath: 保存切分后的路径root
param {*} train_rate: 训练集占总数的百分比
param {*} val_rate: 验证集占总数百分比
param {*} test_rate: 测试集占总数的百分比
return {*}
author: zsfdef data_split_txt(datapath, rootpath./data_split, train_rate0.8, val_rate0.1, test_rate0.1):for parent, dirnames, filenames in os.walk(datapath):index 0for 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.txtelif i math.floor((train_rateval_rate)*len(filepaths)):txt_path val_set.txtelif i len(filepaths):txt_path test_set.txtwith open(os.path.join(rootpath, txt_path), modea) as file:file.write(str(index) os.path.join(datapath, dirname, filepaths[i]) \n) index 1 一张图片均匀切割 import os
from PIL import Image
pathD:\\SR\\USRNet-master\\USRNet-master\\testsets\\image\\20.jpg
smallpathD:\\SR\\USRNet-master\\USRNet-master\\testsets\\small
def splitimage(src, rownum, colnum, dstpath):img Image.open(src)w, h img.sizeif 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 0rowheight h // rownumcolwidth w // colnumfor 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 1print(all {}, num {}.format(rownum*colnum, num))print(图片切割完毕共生成 %s 张小图片学python有用吗。 % 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)i1 路径字符串处理 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 两种文件缺少彼此的进行互删如jpg对应xml文件有时候会缺少jpg或者xml需要删除多余的 import osimport shutil
imagelist[]
for parent, dirnames, filenames in os.walk(E:\\MOTdata\\helmet_20210908\\data1):for filename in filenames:if filename.lower().endswith((.bmp, .dib, .png, .jpg, .jpeg, .pbm, .pgm, .ppm, .tif, .tiff)):path os.path.join(parent, filename)print(filename[:-4].json)if filename[:-4].json not in filenames:os.remove(path)jsonlist[]
for parent, dirnames, filenames in os.walk(E:\\MOTdata\\helmet_20210908\\data1):for filename in filenames:if filename.lower().endswith((.json)):path os.path.join(parent, filename)if filename[:-5].jpg not in filenames:os.remove(path) Python ——保存字典到文件 在 Python 中使用 pickle 模块的 dump 函数将字典保存到文件中 import picklemy_dict { Apple: 4, Banana: 2, Orange: 6, Grapes: 11}
# 保存文件
with open(myDictionary.pkl, wb) as tf:pickle.dump(my_dict,tf)
# 读取文件
with open(myDictionary.pkl, wb) as tf:new_dict pickle.load(tf)print(new_dict.item())在 Python 中使用 NumPy 库的 save 函数将一个字典保存到文件中 import numpy as npmy_dict { Apple: 4, Banana: 2, Orange: 6, Grapes: 11}
# 保存文件
np.save(file.npy, my_dict)
# 读取文件
new_dict np.load(file.npy, allow_pickleTRUE)
print(new_dict)在 Python 中使用 json 模块的 dump 函数将一个字典保存到文件中 import jsonmy_dict { Apple: 4, Banana: 2, Orange: 6, Grapes: 11}
# 保存文件
tf open(myDictionary.json, w)
json.dump(my_dict,tf)
tf.close()
# 读取文件
tf open(myDictionary.json, r)
new_dict json.load(tf)
print(new_dict) 大数据集的遍历数据分块使用进程并行处理 from ntpath import realpath
import os
from pathlib import Path
from random import shuffle
import shutil
import pandas as pd
from multiprocessing import Process,Queue
from time import sleep# root /data/FaceData/FaceTrainData
# relative_path /data/FaceData/FaceTrainData_Mask# file_path os.path.join(root,FaceTrainDataMask.txt)# with open(file_path) as files:
# for item in files:
# temp item.strip(\n).split( )
# p Path(item).parts
# p1 os.path.join(relative_path,p[0])
# if(not os.path.exists(p1)):
# os.mkdir(p1)
# p2 os.path.join(p1,p[1])
# if(not os.path.exists(p2)):
# os.mkdir(p2)# name p[2].strip(\n).split( )# image_path os.path.join(root, temp[0])
# save_path os.path.join(p2, name[0])
# shutil.copy(image_path,save_path)def process_data(my_queue):root /data/FaceData/FaceTrainDatarelative_path /data/FaceData/FaceTrainData_NotMask# print(df)file_path os.path.join(root,FaceTrainDataNotMask.txt)# for row in df.itertuples():# print(row[1])# with open(file_path) as files:df my_queue.get()i0;for row in df.itertuples(): item row[1]temp item.strip(\n).split( ) p Path(item).partsp1 os.path.join(relative_path,p[0])if(not os.path.exists(p1)):os.mkdir(p1)p2 os.path.join(p1,p[1])if(not os.path.exists(p2)):os.mkdir(p2)name p[2].strip(\n).split( )image_path os.path.join(root, temp[0])save_path os.path.join(p2, name[0])# print(image_path)shutil.copy(image_path,save_path)i1print(-------------------------------------i{}--------------------------.format(i))root /data/FaceData/FaceTrainData
file_path os.path.join(root,FaceTrainDataNotMask.txt)x int(20979331/40)
chunksize x # 每100万行处理一次数据
reader pd.read_table(file_path, encoding utf-8, iteratorTrue, chunksizechunksize,names[A])
chunk_num 0
for chunk in reader:my_queue Queue()print(Chunk: str(chunk_num) *30 \n)df chunkmy_queue.put(df)# print(df)# for row in df.itertuples():# print(row[1])p Process(targetprocess_data,args(my_queue,))p.start()chunk_num 1while True:sleep(10)print(------------------)