软件:PyCharm 2021.1.3 (Professional Edition)
python环境包:放在文章结尾文件链接,其中 .yaml 文件
# functions.py
import cv2
import os
import glob
# video to img
def extract_frames(video_path, output_folder, interval):
cap = cv2.VideoCapture(video_path)
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if frame_count % interval == 0:
output_path = f"{output_folder}/frame_{interval}_{frame_count // interval}.jpg"
cv2.imwrite(output_path, frame)
frame_count += 1
cap.release()
# 计数文件夹里的文件个数
def count_files_in_directory(directory):
return len([f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))])
# 删除文件夹下的图片
def del_imgs(folder_path):
# 定义要删除的图片文件夹路径
# 获取文件夹中所有图片文件的路径
image_files = glob.glob(os.path.join(folder_path, '*.jpg')) + glob.glob(os.path.join(folder_path, '*.png'))
# 遍历所有图片文件并删除
for image_file in image_files:
os.remove(image_file)
# img_to_plot.py
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
def wyb_plot(Real_time_value, Maximum, Average, Minimum, fps, title, x_scale=2, y_scale=0.500):
x = [i for i in range(len(Real_time_value))]
plt.figure(dpi=200)
x_major_locator = MultipleLocator(x_scale)
# 把x轴的刻度间隔设置为2,并存在变量里
y_major_locator = MultipleLocator(y_scale)
# 把y轴的刻度间隔设置为0.0500,并存在变量里
ax = plt.gca()
# ax为两条坐标轴的实例
ax.xaxis.set_major_locator(x_major_locator)
# 把x轴的主刻度设置为1的倍数
ax.yaxis.set_major_locator(y_major_locator)
# 把y轴的主刻度设置为10的倍数
# # 绘制柱状图
# y = Real_time_value
# plt.bar(x, y)
# 绘制曲线图
plt.plot(x, Real_time_value, label='Real_time_value')
plt.plot(x, Maximum, label='Maximum')
plt.plot(x, Average, label='Average')
plt.plot(x, Minimum, label='Minimum')
# 绘制曲线图,并标出最大值和最小值
max_y = np.max(Real_time_value)
min_y = np.min(Real_time_value)
max_index = np.argmax(Real_time_value)
min_index = np.argmin(Real_time_value)
plt.annotate("(%s,%s)" % (x[max_index], max_y), xy=(x[max_index], max_y), xytext=(x[max_index], max_y + 0.5),
textcoords='offset points',
color='red')
plt.savefig('wyb_plot.png')
plt.annotate("(%s,%s)" % (x[min_index], min_y), xy=(x[min_index], min_y), xytext=(x[min_index], min_y - 0.5),
textcoords='offset points',
color='green')
max_y = np.max(Maximum)
min_y = np.min(Maximum)
max_index = np.argmax(Maximum)
min_index = np.argmin(Maximum)
plt.annotate("(%s,%s)" % (x[max_index], max_y), xy=(x[max_index], max_y), xytext=(x[max_index], max_y + 0.5),
textcoords='offset points',
color='red')
plt.savefig('wyb_plot.png')
plt.annotate("(%s,%s)" % (x[min_index], min_y), xy=(x[min_index], min_y), xytext=(x[min_index], min_y - 0.5),
textcoords='offset points',
color='green')
max_y = np.max(Average)
min_y = np.min(Average)
max_index = np.argmax(Average)
min_index = np.argmin(Average)
plt.annotate("(%s,%s)" % (x[max_index], max_y), xy=(x[max_index], max_y), xytext=(x[max_index], max_y + 0.5),
textcoords='offset points',
color='red')
plt.savefig('wyb_plot.png')
plt.annotate("(%s,%s)" % (x[min_index], min_y), xy=(x[min_index], min_y), xytext=(x[min_index], min_y - 0.5),
textcoords='offset points',
color='green')
max_y = np.max(Minimum)
min_y = np.min(Minimum)
max_index = np.argmax(Average)
min_index = np.argmin(Average)
plt.annotate("(%s,%s)" % (x[max_index], max_y), xy=(x[max_index], max_y), xytext=(x[max_index], max_y + 0.5),
textcoords='offset points',
color='red')
plt.savefig('wyb_plot.png')
plt.annotate("(%s,%s)" % (x[min_index], min_y), xy=(x[min_index], min_y), xytext=(x[min_index], min_y - 0.5),
textcoords='offset points',
color='green')
plt.xlabel('x/'+str(fps)+"fps")
plt.ylabel('y/A')
plt.title(title)
plt.legend()
plt.savefig('wyb_plot.png')
# plt.show()
# main.py
import functions
import numpy as np
import ocr_imgs
import img_to_plot
# 用户告知!/ Users informed!
print("Welcome to use wyb_project!")
print("Please place the video under the video folder")
# 逻辑判断 / logical judgment
video_path_lj = int(input("Whether to set the video_path( default video_path = ./video/wybdata.mp4)(1/0): "))
interval_lj = int(input("Whether to set the interval( default screenshot / fps = 30)(1/0): "))
video_path = "./video/wybdata.mp4"
output_folder = "./img"
# 输入 video name / Enter your video name
if video_path_lj:
vi_name = input("Enter a video name(mind add suffix): ")
video_path = "./video/" + vi_name
# screenshot/fps
interval = 30 # 默认每隔30帧截取一张图片
if interval_lj:
interval = int(input("screenshot / fps: "))
# screenshot
extract_frames = functions.extract_frames
extract_frames(video_path, output_folder, interval)
# 计数文件夹里的文件个数
directory = output_folder
count_files_in_directory = functions.count_files_in_directory
file_count = count_files_in_directory(directory) - 1
# 定义要遍历的文件夹路径
folder_path = output_folder
# 每帧计数
frame_count = 0
# 数据数组
data_str = []
# 丢失数组
data_lost = []
# ocr imgs
ocr_imgs = ocr_imgs.ocr_imgs(file_count, folder_path, interval, data_str, data_lost)
data_float = [float(x) for x in data_str]
# 绘图
# 定义万用表绘制的数据列表
Real_time_value = []
Maximum = []
Average = []
Minimum = []
for i in range(len(data_float)):
if i % 4 == 0:
Real_time_value.append(data_float[i])
Maximum.append(data_float[i + 1])
Average.append(data_float[i + 2])
Minimum.append(data_float[i + 3])
fps = interval # 30
Real_time_value = np.array(Real_time_value)
Maximum = np.array(Maximum)
Average = np.array(Average)
Minimum = np.array(Minimum)
x_y_lj = int(input("Whether to set x and y axis scale( default x_scale=2, y_scale=0.500)(1/0): "))
if x_y_lj:
x_scale = float(input("input x axis scale: "))
y_scale = float(input("input y axis scale: "))
title_lj = int(input("Whether to set the title of plot ( default \"wyb_plot\")(1/0): "))
if title_lj:
title = input("enter a title for plot: ")
title = "wyb_plot"
wyb_plot = img_to_plot.wyb_plot(Real_time_value, Maximum, Average, Minimum, fps, title, x_scale=2, y_scale=0.500)
img_del_lj = int(input("Whether to delete imgs of imgs folder( default delete)(1/0): "))
if img_del_lj:
folder_path = output_folder
del_imgs = functions.del_imgs(folder_path)
# ocr_imgs.py
from cnocr import CnOcr
import re
def ocr_imgs(file_count, folder_path, interval, data_str, data_lost):
# 遍历文件夹文件(图片),进行文字识别
for frame_count in range(file_count):
img_fp = f"{folder_path}/frame_{interval}_{frame_count}.jpg"
ocr = CnOcr() # 所有参数都使用默认值
out_list = ocr.ocr(img_fp)
data_list = []
for dict in out_list:
text = dict.get('text')
match = re.search(r'[0-9Oo][.,][0-9Oo][0-9Oo][0-9Oo][0-9Oo]|[Q][0-9Oo][0-9Oo][0-9Oo][0-9Oo]', text) # 正则化匹配
if match:
result = match.group()
# print(result)
# with open('output.txt', 'a') as f:
# print(result, file=f)
result = result.replace('O', '0').replace('o', '0').replace(',', '.') # 修正数据
data_list.append(result)
# with open('output.txt', 'a') as f:
# print(result, file=f)
if len(data_list) % 4 == 0:
data_str += data_list
else:
print("数据丢失," + "frame_" + str(interval) + "_" + str(frame_count) + ".jpg" + "未采集")
data_lost.append(frame_count)
# 手动采集图片数据 / manual capture
man_cap = int(input("Whether manual collection(1/0): "))
if man_cap:
for frame in data_lost:
data_ins = 0
for i in range(4):
if i == 0:
data_ins = (input("rea: "))
if i == 1:
data_ins = (input("max: "))
if i == 2:
data_ins = (input("ave: "))
if i == 3:
data_ins = (input("min: "))
data_str.insert((file_count - len(data_list) + 1) * 4 + i, data_ins)
wyb_project https://www.alipan.com/s/dKwQhvHpb4Z 提取码: 6mm1
点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。