python解析佳明fit文件

2023-11-16

使用 fitparse 解析 佳明 fit 文件

以下示例测试环境为:

python 3.8
fitparse 1.2

fitparse

安装

pip3 install fitparse

使用方式

import fitparse
from datetime import timedelta

def get_message(file_name):
    file = fitparse.FitFile(file_name)
    # message = file.get_messages()	 # 返回一个生成器  其中每一项都可以看作是一行数据
    message = file.messages  # 等同于 list(file.get_messages())
    for item in message:
        # 获取每一行数据的名称,其中有几个名称非常关键:
        # item.name == 'file_id' (唯一) 一般位于第一行,里面包含该fit文件的编号ID
        # item.name == 'session' (唯一) 一般位于倒数第几行,里面包含该fit文件的所有聚合信息,各种平均数,极值,功率区间,起止时间,运动类型,等等
        # item.name == 'record' (非常多) 每一个record就代表一个GPS点的信息
        print(item.name)
        
        # 获取每一行的所有信息
        print(item.as_dict())
        
        # 获取特定行的所有信息,比如 file_id 这行
        # 注:实际使用时,建议 将 'file_id','session' 都打印出来放在一个py文件里,随时查看使用 'record'也可以随意打印一个
        if item.name == 'file_id':
            print(item.as_dict())
        
        # 获取行中某个特定的值 比如 获取该fit文件的运动类型 和起止时间
        # 注: 运动类型包含在 名称为 'session' 的行里面,键名为 'sport'
        if item.name == 'session':
            print(item.get_value('sport'))	# 运动类型 >>> 'cycling'
            # 转成国内时间需要加8小时
            # 如果你直接获取时间戳 item.get_raw_value('start_time'),请注意,佳明的时间戳是从1989.12.31 00:00:00 开始的
            start_time = item.get_value('start_time') + timedelta(hours=8)
        	end_time = item.get_value('timestamp') + timedelta(hours=8)
        
        """
        	获取所有点的GPS数据
        	佳明在fit文件中使用的是 semicircles(圆度值) 来记录GPS,比通用的wgs84要更加精准,转换关系如下
        	degrees = semicircles * ( 180 / 2^31 )
			semicircles = degrees * ( 2^31 / 180 )
        """
        try:
            print(item.get_value('position_lat') * (180 / 2 ** 31), item.get_value('position_long') * (180 / 2 ** 31))
            print(item.as_dict())
        except TypeError:
            continue

附件

file_id 行内(部分)信息展示

该信息只作为参考,实际使用时,请自己打印该行信息:

import datetime

file_id = {'name': 'file_id', 'fields': [
    # serial_number 文件编号 类型:int 
    {'name': 'serial_number', 'def_num': 3, 'base_type': 'uint32z', 'type': 'uint32z', 'units': None, 'value': 3950...83, 'raw_value': 3950...83},
    {'name': 'time_created', 'def_num': 4, 'base_type': 'uint32', 'type': 'date_time', 'units': None, 'value': datetime.datetime(2018, 1, 24, 4, 52, 23), 'raw_value': 885703943},
    {'name': 'manufacturer', 'def_num': 1, 'base_type': 'uint16', 'type': 'manufacturer', 'units': None, 'value': 'garmin', 'raw_value': 1},
    {'name': 'garmin_product', 'def_num': 2, 'base_type': 'uint16', 'type':'garmin_product', 'units': None, 'value': 2533, 'raw_value': 2533},]}

session 行内(部分)信息展示

session 内都是当前fit文件的聚集信息

该信息只作为参考,实际使用时,请自己打印该行信息:

session = {'name': 'session', 'fields': [
    # 结束时间
    {'name': 'timestamp', 'def_num': 253, 'base_type': 'uint32', 'type': 'date_time', 'units': None, 'value': datetime.datetime(2018, 3, 25, 10, 26, 57), 'raw_value': 890908017},
    # 开始时间
    {'name': 'start_time', 'def_num': 2, 'base_type': 'uint32', 'type': 'date_time', 'units': None, 'value': datetime.datetime(2018, 3, 25, 0, 17, 20), 'raw_value': 890871440},
	.
    .
    .
    # 运动时间
    {'name': 'total_timer_time', 'def_num': 8, 'base_type': 'uint32', 'type': 'uint32', 'units': 's','value': 25048.871, 'raw_value': 25048871},
    # 总里程
    {'name': 'total_distance', 'def_num': 9, 'base_type': 'uint32', 'type': 'uint32', 'units': 'm', 'value': 88924.04,'raw_value': 8892404},
    .
    .
    .
    {'name': 'time_in_hr_zone', 'def_num': 65, 'base_type': 'uint32', 'type': 'uint32', 'units': 's',
     'value': (778.726, 5631.959, 7464.129, 4913.151, 4478.913, 1204.132, 0.0),
     'raw_value': (778726, 5631959, 7464129, 4913151, 4478913, 1204132, 0)},
    # 功率区间
    {'name': 'time_in_power_zone', 'def_num': 68, 'base_type': 'uint32', 'type': 'uint32', 'units': 's',
     'value': (13527.302, 1585.171, 1841.759, 990.07, 935.986, 813.014, 1078.521, 1631.342, 0.0, 0.0),
     'raw_value': (13527302, 1585171, 1841759, 990070, 935986, 813014, 1078521, 1631342, 0, 0)},
    .
    .
    .
    # 消耗卡路里
    {'name': 'total_calories', 'def_num': 11, 'base_type': 'uint16', 'type': 'uint16', 'units': 'kcal', 'value': 1979,'raw_value': 1979},
    # 速度
    {'name': 'avg_speed', 'def_num': 14, 'base_type': 'uint16', 'type': 'uint16', 'units': 'm/s', 'value': 3.55,'raw_value': 3550},
    {'name': 'max_speed', 'def_num': 15, 'base_type': 'uint16', 'type': 'uint16', 'units': 'm/s', 'value': 15.76,'raw_value': 15760},
    # 功率
    {'name': 'avg_power', 'def_num': 20, 'base_type': 'uint16', 'type': 'uint16', 'units': 'watts', 'value': 83,'raw_value': 83},
    {'name': 'max_power', 'def_num': 21, 'base_type': 'uint16', 'type': 'uint16', 'units': 'watts', 'value': 1591,'raw_value': 1591},
    # 总爬升 与下降
    {'name': 'total_ascent', 'def_num': 22, 'base_type': 'uint16', 'type': 'uint16', 'units': 'm', 'value': 626,'raw_value': 626},
    {'name': 'total_descent', 'def_num': 23, 'base_type': 'uint16', 'type': 'uint16', 'units': 'm', 'value': 647,'raw_value': 647},
    # 得分?
    {'name': 'training_stress_score', 'def_num': 35, 'base_type': 'uint16', 'type': 'uint16', 'units': 'tss','value': 770.3, 'raw_value': 7703},
    {'name': 'intensity_factor', 'def_num': 36, 'base_type': 'uint16', 'type': 'uint16', 'units': 'if', 'value': 1.113,'raw_value': 1113},
    # 运动类型
    {'name': 'sport', 'def_num': 5, 'base_type': 'enum', 'type': 'sport', 'units': None, 'value': 'cycling','raw_value': 2},
    # 子类型?
    {'name': 'sub_sport', 'def_num': 6, 'base_type': 'enum', 'type': 'sub_sport', 'units': None, 'value': 'generic','raw_value': 0},
    # 心率
    {'name': 'avg_heart_rate', 'def_num': 16, 'base_type': 'uint8', 'type': 'uint8', 'units': 'bpm', 'value': 128,'raw_value': 128},
    {'name': 'max_heart_rate', 'def_num': 17, 'base_type': 'uint8', 'type': 'uint8', 'units': 'bpm', 'value': 179,'raw_value': 179},
    # 踏频
    {'name': 'avg_cadence', 'def_num': 18, 'base_type': 'uint8', 'type': 'uint8', 'units': 'rpm', 'value': 68,'raw_value': 68},
    {'name': 'max_cadence', 'def_num': 19, 'base_type': 'uint8', 'type': 'uint8', 'units': 'rpm', 'value': 172,'raw_value': 172},]}
    .
    .
    .

record 行内(部分)信息展示

每个record都是一个GPS点, 里面都是当前GPS点的瞬时信息

该信息只作为参考,实际使用时,请自己打印该行信息:

record = {'name': 'record', 'fields': [
    # 时间戳 和 经纬度
    {'name': 'timestamp', 'def_num': 253, 'base_type': 'uint32', 'type': 'date_time', 'units': None,'value': datetime.datetime(2018, 3, 25, 10, 19, 11), 'raw_value': 890907551},
    {'name': 'position_lat', 'def_num': 0, 'base_type': 'sint32', 'type': 'sint32', 'units': 'semicircles','value': 365908323, 'raw_value': 365908323},
    {'name': 'position_long', 'def_num': 1, 'base_type': 'sint32', 'type': 'sint32', 'units': 'semicircles','value': 1241201927, 'raw_value': 1241201927},
    # 当前里程
    {'name': 'distance', 'def_num': 5, 'base_type': 'uint32', 'type': 'uint32', 'units': 'm', 'value': 86430.14,'raw_value': 8643014},
    {'name': 'accumulated_power', 'def_num': 29, 'base_type': 'uint32', 'type': 'uint32', 'units': 'watts','value': 1801134, 'raw_value': 1801134},
    # 海拔
    {'name': 'enhanced_altitude', 'def_num': 78, 'base_type': 'uint32', 'type': 'uint32', 'units': 'm','value': 452.79999999999995, 'raw_value': 452.79999999999995},
    {'name': 'altitude', 'def_num': 2, 'base_type': 'uint16', 'type': 'uint16', 'units': 'm','value': 452.79999999999995, 'raw_value': 4764},
    # 速度
    {'name': 'enhanced_speed', 'def_num': 73, 'base_type': 'uint32', 'type': 'uint32', 'units': 'm/s', 'value': 8.659,'raw_value': 8.659},
    {'name': 'speed', 'def_num': 6, 'base_type': 'uint16', 'type': 'uint16', 'units': 'm/s', 'value': 8.659,'raw_value': 8659},
    # 功率
    {'name': 'power', 'def_num': 7, 'base_type': 'uint16', 'type': 'uint16', 'units': 'watts', 'value': 191,'raw_value': 191},
    # 心率
    {'name': 'heart_rate', 'def_num': 3, 'base_type': 'uint8', 'type': 'uint8', 'units': 'bpm', 'value': 151,
     'raw_value': 151},
    # 踏频
    {'name': 'cadence', 'def_num': 4, 'base_type': 'uint8', 'type': 'uint8', 'units': 'rpm', 'value': 95,'raw_value': 95},
    # 温度
    {'name': 'temperature', 'def_num': 13, 'base_type': 'sint8', 'type': 'sint8', 'units': 'C', 'value': 20,'raw_value': 20},]}

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python解析佳明fit文件 的相关文章

随机推荐

  • 【Java】JDK 1.8新特性

    Lambda 表达式 在没有 Lambda 表达式的时候 在 Java 中只能使用匿名内部类代替 Lambda 表达式 以下面的代码为例 查看 Lambda 表达式的使用 匿名内部类方式排序 List
  • c语言输入输出函数printf与scanf的用法格式

    c语言输入输出函数printf与scanf的用法格式 格式化规则例如 5 4f等类似问题的说明 Turbo C2 0 标准库提供了两个控制台格式化输入 输出函数printf 和scanf 这两个函数可以在标准输入输出设备上以各种不同的格式读
  • SAP LSMW日志信息如何导出到Excel里?

    SAP LSMW日志信息如何导出到Excel里 在SAP系统中 数据迁移LSMW运行的日志 是可以下载到本地Excel文件里的 方式如下所示 双击某个会话 点击打印机图标 就可以导出到Excel文件里了 输入文件名 指定文件保存的目录 完
  • 送书

    今天是周三 又到了给大家送书的时刻啦 这次给大家带来的是 OpenCV图像处理入门与实践 文末查看送书规则 简介 OpenCV 是一个开源的计算机视觉库 可以实现计算机视觉算法 本书从 OpenCV 用 Python 实现的基础语法讲起 逐
  • C51定时器和计数器 timer and counter

    代码 include
  • Seven Different Linux/BSD Firewalls Reviewed

    Seven Different Linux BSD Firewalls Reviewed Firewall November 14th 2007 Did you know more than 500 million computers in
  • 鲸鱼优化算法论文【matlab代码与仿真】

    一 算法流程 通过回声定位并相互传递探寻猎物信息 研究表明 鲸鱼大脑的某些区域与人类相似 有一种叫做梭形细胞的共同细胞 这些细胞负责人类的判断 情感和社会行为 这种细胞的数量是成年人的两倍 事实证明 鲸鱼可以像人类一样思考 学习 判断 交流
  • 从Kubernetes 1.14 发布,看技术社区演进方向

    Kubernetes 1 14 正式发布已经过去了一段时间 相信你已经从不同渠道看过了各种版本的解读 不过 相比于代码 Release 马上就要迎来5周岁生日的Kubernetes 项目接下来如何演进 其实也是一个让人着迷的话题 而作为一个
  • 在linux中,&和&&,

    对应刚接触linux命令的小伙伴们来说 这些符号一定是很困扰的下面我们一起来看这些符号区别和用法 表示任务在后台执行 如要在后台运行 如 root localhost local java jar test jar gt log txt 运
  • 大型 SaaS 平台产品架构设计

    当我们去搜索 架构 可以得到很多的架构图片 比如组织架构 业务架构 数据架构 技术架构 安全架构 产品架构 部署架构等 什么是架构 通常大家说架构一般指软件架构 架构是指软件的基础结构 创造这些基础结构的准则 以及对这些结构的描述 在这个定
  • IAR常见报错

    右键进入函数出错 project gt clean
  • 利用python进行数据分析之数据聚合和分组运算--小白笔记

    GroupBy机制 split apply combine 拆分 应用 合并 import pandas as pd import numpy as np df pd DataFrame key1 a a b b a key2 one tw
  • 找不到匹配的host key算法

    vim etc ssh sshd config
  • 《WebRTC系列》实战 Web 端支持 h265 硬解

    1 背景 Web 端实时预览 H 265 需求一直存在 但由于之前 Chrome 本身不支持 H 265 硬解 软解性能消耗大 仅能支持一路播放 该需求被搁置 去年 9 月份 Chrome 发布 M106 版本 默认开启 H 265 硬解
  • raw格式详解

    raw格式是camera sensor直接输出的格式 每个像素点表示一个颜色分量B G或R 注意 这句话不准确 红外相机的sensor和彩色相机的sensor有些不同 有的红外相机的sensor输出的raw data就是亮度值 即灰度值 输
  • Android Flutter开发环境搭建

    1 搭建 Flutter 开发环境 本栏亦在快速上手Android Flutter Flutter框架就不介绍了 框架这个东西怎么说呢 对于大部分人来说只是了解即可 如需了解的话 可以度娘资料很多 本节我们主要看下如何在Windwos下搭建
  • Kotlin协程实现原理:CoroutineScope,看完不懂你砍我!墙裂建议收藏。

    今天我们来聊聊Kotlin的协程Coroutine 文末有为大家准备的彩蛋 如果你还没有接触过协程 推荐你先阅读这篇入门级文章What 你还不知道Kotlin Coroutine 如果你已经接触过协程 相信你都有过以下几个疑问 协程到底是个
  • 一个码稿人自述:什么样的文档产品适合我?|深度吐槽

    关注ITValue 看企业级最新鲜 最价值报道 图片来源 Unsplash 钛媒体打工人 媒体相关从业经验4 5年 文档使用重度患者 今天以我曾经用过的 和现在主流的一些文档产品为例 来谈谈我的使用体验 以及什么样的文档适合我 一 我与文档
  • [编程工具]MarkDown编辑查看以及使用语法

    目录 0 前言 1 markDown语法 2 markDown 3 MD正确打开方式 4 结尾 结束啦感谢观看 5 参考连接 0 前言 本文介绍了markDown的编辑查看 使用浏览器查看以及Vscode中查看编辑MD 最后介绍了MD的常用
  • python解析佳明fit文件

    使用 fitparse 解析 佳明 fit 文件 以下示例测试环境为 python 3 8 fitparse 1 2 fitparse 安装 pip3 install fitparse 使用方式 import fitparse from d