实时Log信息,图形化显示(Python matplotlib)

便于调试,可将log信息(时间戳)等实时以图形化显示。

免去写入文件、Excel分析等步骤。


关键技术:Python、matplotlib、Socket、正则表达式。


效果:

源代码:

import socket
import matplotlib.pyplot as plt
import thread
import re
import string

SHOW_DURATION = 100 # duration of X
Y_MAX = 1000
Y_MIN = -1000

# Init Socket for Receiving
address = ('127.0.0.1', 31500)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(address)


# Main Loop
def Loop():
    audio = [0]*300
    video = [0]*300
    diff = [0]*300
    reobj = re.compile(r"Audio PTS = ([\d]*), Video PTS = ([\d]*)")
    cnt = 0 # recv counter        
    plt.hold(False)# Hold Off
    while True:
        # Receive Data form Socket
        print 1
        data, addr = s.recvfrom(2048)
        print 1
        cnt += 1        
        # Parse the Data
        match = reobj.search(data)
        if match:
            curAudio = string.atol(match.group(1))
            curVideo = string.atol(match.group(2))
            audio += [curAudio]
            video += [curVideo]
            diff += [curAudio - curVideo]
        # Plot the figure every 15 packets received if we plot too frequently, it will block the new packet
        if(0 == cnt%15):
            iStart = len(diff) - SHOW_DURATION            
            if iStart < 0:
                iStart = 0                
            # plot the data
            plt.plot(x[iStart:-1],diff[iStart:-1])
            # To set the limit of y, ylim() should be called after plot, or it will lose efficacy
            plt.ylim(Y_MIN, Y_MAX)
            # to refresh the figure by force
            plt.draw()
            
            
# Thread for Receiving Data from Socket 
thread.start_new_thread(Loop,())

# show UI
plt.show()

s.close()


发送端(Debug程序),循环打印Log即可。

示例代码:

import socket
import random
import time

address = ('127.0.0.1', 31500)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
audio = 0;
video = 0;
index = 0

while True:
    time.sleep(0.02)    
    audio += 33333
    video = audio + random.randint(-300,300)
    index += 1
    if index%500 == 0:
        video += 5000
    msg = "Audio PTS = %d, Video PTS = %d\n" %(audio, video)
    s.sendto(msg, address)
    print index

s.close()
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值