万用表数据导出变化曲线图——pycharm实现视频数据导出变化曲线图

2024-03-10 07:44

本文主要是介绍万用表数据导出变化曲线图——pycharm实现视频数据导出变化曲线图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

万用表数据导出变化曲线图——pycharm实现视频数据导出变化曲线图

  • 一、效果展示
  • 二、环境配置
  • 三、代码构思
  • 四、代码展示
  • 五、代码、python环境包链接

一、效果展示

图1.1 效果展示
(左图:万用表视频截图;右图:表中数据变化曲线图)

二、环境配置

软件:PyCharm 2021.1.3 (Professional Edition)

python环境包:放在文章结尾文件链接,其中 .yaml 文件

三、代码构思

Created with Raphaël 2.3.0 Start 预备工作:拍摄一段万用表视频 预备工作:裁剪视频、读取视频每秒帧数 代码1:将视频按帧数截屏至某文件夹下 代码2:ocr 截屏文件夹下所有文件 代码3:正则表达式筛选截图中数字数据,并修正数据 代码4:绘图 End

四、代码展示

# 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 = 0while cap.isOpened():ret, frame = cap.read()if not ret:breakif frame_count % interval == 0:output_path = f"{output_folder}/frame_{interval}_{frame_count // interval}.jpg"cv2.imwrite(output_path, frame)frame_count += 1cap.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 MultipleLocatordef 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_folderdel_imgs  = functions.del_imgs(folder_path)
# ocr_imgs.py
from cnocr import CnOcr
import redef 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_listelse:print("数据丢失," + "frame_" + str(interval) + "_" + str(frame_count) + ".jpg" + "未采集")data_lost.append(frame_count)# 手动采集图片数据 / manual captureman_cap = int(input("Whether manual collection(1/0): "))if man_cap:for frame in data_lost:data_ins = 0for 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)

五、代码、python环境包链接

wyb_project https://www.alipan.com/s/dKwQhvHpb4Z 提取码: 6mm1
点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

这篇关于万用表数据导出变化曲线图——pycharm实现视频数据导出变化曲线图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/793536

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

Spring Gateway动态路由实现方案

《SpringGateway动态路由实现方案》本文主要介绍了SpringGateway动态路由实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前沿何为路由RouteDefinitionRouteLocator工作流程动态路由实现尾巴前沿S