• 万用表数据导出变化曲线图——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

    四、代码展示

    # 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)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    # 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()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    # 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)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    # 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)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    五、代码、python环境包链接

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

  • 相关阅读:
    一种简单通用的获取函数栈空间大小的方法
    多媒体数据处理实验2:PCA
    这些4K手机、电脑壁纸网站,你一定要知道
    Be Your Own Teacher 论文阅读
    美团笔试题目(Java后端5题2小时)
    呼叫中心系统角色功能的应用
    华为云物联网平台对接之MQTT设备接入
    集群容器部署和管理(Docker&K8S)
    java毕业设计天天好药店管理系统源码+lw文档+mybatis+系统+mysql数据库+调试
    JAVA成都某4S店销售管理系统计算机毕业设计Mybatis+系统+数据库+调试部署
  • 原文地址:https://blog.csdn.net/weixin_46239293/article/details/136577747