做网站时怎么透明化,hao爱做网站,广州平面设计培训机构,微博网页版登录入口文章目录 1. txt读写读综合案例 日志文件读写 2. excel读写读取csv读取xlsx 3. matplotlib 案例折线图多个折现图散点图柱状图饼状图 4 opencv 案例加载与展示图片缩放图片旋转图片保存图片读取摄像头视频保存opencv 综合案例 5 pickle 案例 1. txt读写
读 file.read() file.r… 文章目录 1. txt读写读综合案例 日志文件读写 2. excel读写读取csv读取xlsx 3. matplotlib 案例折线图多个折现图散点图柱状图饼状图 4 opencv 案例加载与展示图片缩放图片旋转图片保存图片读取摄像头视频保存opencv 综合案例 5 pickle 案例 1. txt读写
读 file.read() file.readlines() file.readline() # # 使用 read 方法读取文件的所有内容
with open(resources/training_log.txt, r) as file:content file.read()print(content)# 使用 readline 方法逐行读取文件
with open(.\\resources/training_log.txt, r) as file:line file.readline()# print(line)while line:print(line, end)line file.readline()# 使用 readlines 方法读取文件的所有行
with open(.\\resources/training_log.txt, r) as file:lines file.readlines()# print(lines)for line in lines:print(line, end)## 写f.write()f.writelines()python
# 使用 write 方法写入文件
# with open(resources/example_1.txt, w) as file:
# file.write(Hello, World!)# 使用 writelines 方法写入文件
lines [Hello, World!, Welcome to Python programming.]
with open(resources/example_2.txt, w) as file:file.writelines(line \n for line in lines)综合案例 日志文件读写 f.write 写一段代码模拟生成accuracy逐步上升、loss逐步下降的训练日志并将日志信息记录到 training_log.txt中
import randomepoch 100
accuracy 0.5
loss 0.9with open(training_log.txt, w) as f:f.write(Epoch\tAccuracy\tLoss\n)for epoch_i in range(1, epoch1):accuracy random.uniform(0, 0.005)loss - random.uniform(0, 0.005)accuracy min(1, accuracy)loss max(0, loss)f.write(f{epoch_i}\t{accuracy:.3f}\t{loss:.3f}\n)print(fEpoch:{epoch_i}, Accuracy:{accuracy}, Loss:{loss})
2. excel读写
读取csv pd.read_csv() 读取xlsx pd.read_excel() 3. matplotlib 案例
折线图
import numpy as np
import matplotlib.pyplot as plt# 创建一个x值的数组从-2π到2π步长为0.01
x np.arange(-2 * np.pi, 2 * np.pi, 0.01)# 计算每个x值对应的sin(x)值
y np.sin(x)# 使用matplotlib来绘制图像
plt.figure() # 创建一个新的图像窗口
plt.plot(x, y) # 绘制折线图
plt.title(sin(x)) # 设置图像的标题
plt.xlabel(x) # 设置x轴的标签
plt.ylabel(sin(x)) # 设置y轴的标签
plt.grid(True) # 显示网格
plt.show() # 显示图像##读取YOLO数据后画出曲线图
import pandas as pd
import matplotlib.pyplot as pltdata_loc rresources/yolov5s.csvdata pd.read_csv(data_loc, index_col0)train_bbox_loss data[ train/box_loss]x_list [i for i in range(len(train_bbox_loss))]
plt.plot(x_list, train_bbox_loss)
plt.xlabel(Epoch)
plt.ylabel(Loss)
plt.title(YOLOv5s)
plt.show()多个折现图
import pandas as pd
import matplotlib.pyplot as pltfile_1_loc ./resources/yolov5l.csv
file_2_loc C:\WORK\xuxiu\learn\AI\class\【0】源码PDF课件电子书\【0】源码PDF课件电子书\源码PDF课件\第3周资料\第三周课程代码\代码\3.matplotlib_demos\resources\yolov5m.csv
file_3_loc ./resources/yolov5s.csvfile_1 pd.read_csv(file_1_loc)
file_2 pd.read_csv(file_2_loc)
file_3 pd.read_csv(file_3_loc)file_1_train_box_loss file_1[ train/box_loss]
file_2_train_box_loss file_2[ train/box_loss]
file_3_train_box_loss file_3[ train/box_loss]x_list [i for i in range(len(file_1_train_box_loss))]plt.plot(x_list, file_1_train_box_loss)
plt.plot(x_list, file_2_train_box_loss)
plt.plot(x_list, file_3_train_box_loss)plt.xlabel(Epoch)
plt.ylabel(Loss)
plt.title(Train box_loss)
plt.grid()plt.legend([yolov5l, yolov5m, yolov5s])plt.show()
散点图 plt.scatter 柱状图 plt.bar plt.bar(x, values, color‘blue’, align‘center’, alpha0.7) 饼状图 plt.pie(sizes, explodeexplode, labelslabels, colorscolors, autopct‘%1.1f%%’, shadowTrue, startangle140) import matplotlib.pyplot as plt# 数据
sizes [15, 30, 45, 10] # 各部分的大小
labels [A, B, C, D] # 各部分的标签
colors [gold, yellowgreen, lightcoral, lightskyblue] # 各部分的颜色
explode (0.1, 0, 0, 0) # 突出显示第一个部分# 绘制扇形图
plt.pie(sizes, explodeexplode, labelslabels, colorscolors, autopct%1.1f%%, shadowTrue, startangle140)# 设置为等比例这样扇形图就是一个圆
plt.axis(equal)# 显示图像
plt.show()
4 opencv 案例
加载与展示图片 cv2.imread() cv2.imshow() import cv2img_path rresources/food.png# 以彩色模式读取图片
image_color cv2.imread(img_path)# 以灰度模式读取图片
image_gray cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)# 显示图片
cv2.imshow(Color Image, image_color)
# cv2.imshow(Grayscale Image, image_gray)# 等待用户按键然后关闭窗口
cv2.waitKey(0)
cv2.destroyAllWindows()
缩放图片 cv2.resize() 旋转图片 cv2.rotate() # 使用cv2.rotate()函数旋转图片
rotated_90 cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) # 顺时针旋转90度
rotated_180 cv2.rotate(image, cv2.ROTATE_180) # 顺时针旋转180度
rotated_270 cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE) # 顺时针旋转270度保存图片 cv2.imwrite() import cv2# 读取图像
image cv2.imread(resources/food.png)# 如果图像不为空则保存图像
if image is not None:cv2.imwrite(output_image.png, image)
else:print(无法读取图像)读取摄像头 cv2.VideoCapture import cv2# 创建一个 VideoCapture 对象参数 0 表示使用默认的摄像头, 也可以传入一个视频文件的路径
cap cv2.VideoCapture(resources/piano.mp4) # resources/piano.mp4while True:# 读取一帧ret, frame cap.read()# 如果读取成功显示这一帧if ret:cv2.imshow(Frame, frame)# 按 q 键退出循环if cv2.waitKey(15) 0xFF ord(q):break# 释放资源并关闭窗口
cap.release()
cv2.destroyAllWindows()释放资源并关闭窗口 cap.release() cv2.destroyAllWindows() 视频保存 cv2.VideoWriter() import cv2# 定义视频捕获对象
cap cv2.VideoCapture(0)# 检查是否成功打开摄像头
if not cap.isOpened():print(Error: Could not open camera.)exit()# 获取摄像头的帧宽度和帧高度
frame_width int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# 定义视频编码器和输出文件
fourcc cv2.VideoWriter_fourcc(*mp4v) # 或者使用 XVID
out cv2.VideoWriter(output.mp4, fourcc, 20.0, (frame_width, frame_height))while True:ret, frame cap.read()if not ret:print(Failed to grab frame.)break# 将当前帧写入输出视频文件out.write(frame)# 显示当前帧cv2.imshow(frame, frame)# 按q键退出循环if cv2.waitKey(1) 0xFF ord(q):break# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()opencv 综合案例
import cv2
import numpy as npdef add_gaussian_noise(image):row, col image.shapemean 0sigma 15gauss np.random.normal(mean, sigma, (row, col))noisy image gaussnoisy_img np.clip(noisy, 0, 255)return noisy_img.astype(np.uint8)# 输入和输出视频文件名
input_video resources/outdoor.mp4
output_video resources/output.mp4# 打开输入视频
cap cv2.VideoCapture(input_video)# 获取视频的帧率和帧大小
fps int(cap.get(cv2.CAP_PROP_FPS))
frame_width int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# 计算新的帧大小 (540p)
new_height 540
new_width int((new_height / frame_height) * frame_width)# 创建视频写入对象
fourcc cv2.VideoWriter_fourcc(*mp4v)
out cv2.VideoWriter(output_video, fourcc, fps, (new_width, new_height), isColorFalse)while True:ret, frame cap.read()if not ret:break# 调整帧大小frame cv2.resize(frame, (new_width, new_height))# 转换为灰度图像frame cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# 垂直翻转画面frame cv2.flip(frame, 1)# 添加高斯噪声frame add_gaussian_noise(frame)# 写入输出视频out.write(frame)# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
5 pickle 案例
# 使用 pickle 保存数据
with open(data.pkl, wb) as file:pickle.dump(data, file)# 使用 pickle 加载数据
with open(data.pkl, rb) as file:loaded_data pickle.load(file)
文章转载自: http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.tgtsg.cn.gov.cn.tgtsg.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.jydky.cn.gov.cn.jydky.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.rhnn.cn.gov.cn.rhnn.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.plqkz.cn.gov.cn.plqkz.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.snjpj.cn.gov.cn.snjpj.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.kyjyt.cn.gov.cn.kyjyt.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.kxltf.cn.gov.cn.kxltf.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.mltsc.cn.gov.cn.mltsc.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.trzzm.cn.gov.cn.trzzm.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.svtxeu.com.gov.cn.svtxeu.com http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.skql.cn.gov.cn.skql.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.wspjn.cn.gov.cn.wspjn.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn