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

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

一、效果展示


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

二、环境配置

软件:PyCharm 2021.1.3 (Professional Edition)

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

三、代码构思

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

四、代码展示

python 复制代码
# 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)
python 复制代码
# 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()
python 复制代码
# 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)
python 复制代码
# 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)

五、代码、python环境包链接

wyb_project https://www.alipan.com/s/dKwQhvHpb4Z 提取码: 6mm1

点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

相关推荐
MediaTea4 小时前
Pr 视频过渡:沉浸式视频 - VR 色度泄漏
音视频·vr
LNTON羚通5 小时前
算法定制LiteAIServer摄像机实时接入分析平台烟火检测算法的主要功能
音视频·视频监控
EasyCVR10 小时前
EHOME视频平台EasyCVR视频融合平台使用OBS进行RTMP推流,WebRTC播放出现抖动、卡顿如何解决?
人工智能·算法·ffmpeg·音视频·webrtc·监控视频接入
神里大人12 小时前
idea、pycharm等软件的文件名红色怎么变绿色
java·pycharm·intellij-idea
冷凝女子12 小时前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
张小生18012 小时前
PyCharm中 argparse 库 的使用方法
python·pycharm
叶知安12 小时前
如何用pycharm连接sagemath?
ide·python·pycharm
安步当歌13 小时前
【WebRTC】视频编码链路中各个类的简单分析——VideoStreamEncoder
音视频·webrtc·视频编解码·video-codec
顾北川_野13 小时前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
顶呱呱程序13 小时前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法