You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python计算机视觉健身计数代码集成到React Native Expo?

可行集成方案

一、搭建Flask/Django API服务(跨平台通用推荐)

这是最稳妥的方案,无需修改React Native Expo工作流,Python代码可部署在本地局域网服务器或云服务器上,通过HTTP/WebSocket与RN应用通信。

步骤1:封装Python函数为API端点

以Flask为例快速搭建接口:

from flask import Flask, jsonify, request
from flask_socketio import SocketIO, emit
import threading
import your_fitness_module  # 导入你的健身计数代码模块

app = Flask(__name__)
# 允许跨域适配RN应用
socketio = SocketIO(app, cors_allowed_origins="*")

# 实时计数推送(适配摄像头实时检测场景)
@socketio.on('start_action')
def start_real_time_count(data):
    action_type = data.get('action')
    
    def count_thread():
        # 假设你的计数函数是迭代器,实时返回更新的计数值
        if action_type == 'bicep_curl':
            for count in your_fitness_module.bicep_curl_realtime_count():
                emit('count_update', {'count': count})
        elif action_type == 'sit_up':
            for count in your_fitness_module.sit_up_realtime_count():
                emit('count_update', {'count': count})
    
    # 启动线程避免阻塞API
    threading.Thread(target=count_thread).start()

# 单次计数结果返回(适配预录制视频或非实时场景)
@app.route('/get-count', methods=['POST'])
def get_single_count():
    action_type = request.json.get('action')
    if action_type == 'bicep_curl':
        count = your_fitness_module.bicep_curl_count()
        return jsonify({'status': 'success', 'count': count})
    elif action_type == 'sit_up':
        count = your_fitness_module.sit_up_count()
        return jsonify({'status': 'success', 'count': count})
    else:
        return jsonify({'status': 'error', 'message': '无效动作类型'})

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5000, debug=True)

步骤2:React Native Expo端调用

用socket.io-client处理实时推送,用axios处理单次请求:

import { useState } from 'react';
import { View, Button, Text } from 'react-native';
import io from 'socket.io-client';
import axios from 'axios';

// 连接本地Flask服务(替换为你的服务器IP)
const socket = io('http://192.168.1.100:5000');

const FitnessApp = () => {
    const [count, setCount] = useState(0);

    // 实时肱二头肌弯举计数
    const handleBicepCurl = () => {
        socket.emit('start_action', { action: 'bicep_curl' });
        socket.on('count_update', (data) => {
            setCount(data.count);
        });
    };

    // 单次仰卧起坐计数
    const handleSitUp = async () => {
        try {
            const res = await axios.post('http://192.168.1.100:5000/get-count', {
                action: 'sit_up'
            });
            setCount(res.data.count);
        } catch (err) {
            console.error(err);
        }
    };

    return (
        <View>
            <Button title="肱二头肌弯举计数" onPress={handleBicepCurl} />
            <Button title="仰卧起坐计数" onPress={handleSitUp} />
            <Text>当前计数:{count}</Text>
        </View>
    );
};

export default FitnessApp;

二、Expo外部进程执行(离线可用,需转Bare Workflow)

如果需要离线运行,可将Python代码打包成可执行文件,在RN应用中调用本地进程。注意iOS受沙箱限制,该方案更适合Android。

步骤1:打包Python代码为可执行文件

用PyInstaller打包:

# 生成单文件可执行程序
pyinstaller --onefile your_fitness_code.py

Android平台需交叉编译或用Termux环境打包,确保生成的二进制文件适配手机架构。

步骤2:Expo端调用外部进程

使用expo-file-system处理文件权限与执行:

import { useState } from 'react';
import { View, Button, Text } from 'react-native';
import * as FileSystem from 'expo-file-system';
import { Platform } from 'react-native';

const FitnessApp = () => {
    const [count, setCount] = useState(0);

    const runPythonCount = async (actionCode) => {
        try {
            // 可执行文件路径(需先将打包好的文件放入assets目录)
            const execPath = Platform.OS === 'android' 
                ? `${FileSystem.documentDirectory}your_fitness_code`
                : `${FileSystem.documentDirectory}your_fitness_code`;

            // 复制文件到可执行目录并设置权限
            await FileSystem.copyAsync({
                from: require('./assets/your_fitness_code'),
                to: execPath,
            });
            await FileSystem.makeFileExecutableAsync(execPath);

            // 执行程序并传递动作参数(如1=肱二头肌,2=仰卧起坐)
            const result = await FileSystem.executeAsync(execPath, [actionCode]);
            setCount(parseInt(result.stdout.trim()));
        } catch (err) {
            console.error('执行出错:', err);
        }
    };

    return (
        <View>
            <Button title="肱二头肌弯举" onPress={() => runPythonCount('1')} />
            <Button title="仰卧起坐" onPress={() => runPythonCount('2')} />
            <Text>计数:{count}</Text>
        </View>
    );
};

export default FitnessApp;

三、方案对比与注意事项

  • API方案:跨平台兼容性强,无需修改RN工作流,Python代码可利用服务器性能处理复杂CV任务;缺点是需要网络(本地局域网或云服务)。
  • 外部进程方案:支持离线运行,但移动端性能有限,iOS沙箱限制多,需转Bare Workflow,打包与权限处理繁琐。
  • 实时摄像头检测场景:优先选API方案,通过WebSocket将RN摄像头采集的帧数据(base64编码)传给Python后端处理,避免移动端资源占用过高。

内容的提问来源于stack exchange,提问作者Junaid Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:57:51