跳到主要内容

Limelight Lib Python

limelightlib-python 是与 Limelight 设备交互的最简单方法。它适用于所有操作系统(MacOS、Windows、Linux)和架构(x86、ARM)。

安装

pip install limelightlib-python

使用方法

import limelight
import limelightresults
import json
import time

discovered_limelights = limelight.discover_limelights(debug=True)
print("发现的 limelights:", discovered_limelights)

if discovered_limelights:
limelight_address = discovered_limelights[0]
ll = limelight.Limelight(limelight_address)
results = ll.get_results()
status = ll.get_status()
print("-----")
print("目标结果:", results)
print("-----")
print("状态:", status)
print("-----")
print("温度:", ll.get_temp())
print("-----")
print("名称:", ll.get_name())
print("-----")
print("帧率:", ll.get_fps())
print("-----")
print("硬件报告:", ll.hw_report())

ll.enable_websocket()

# 打印当前管道设置
print(ll.get_pipeline_atindex(0))

# 更新当前管道并刷新到磁盘
pipeline_update = {
'area_max': 98.7,
'area_min': 1.98778
}
ll.update_pipeline(json.dumps(pipeline_update),flush=1)

print(ll.get_pipeline_atindex(0))

# 切换到管道 1
ll.pipeline_switch(1)

# 更新自定义用户数据
ll.update_python_inputs([4.2,0.1,9.87])


try:
while True:
result = ll.get_latest_results()
parsed_result = limelightresults.parse_results(result)
if parsed_result is not None:
print("有效目标: ", parsed_result.validity, ", 管道索引: ", parsed_result.pipeline_id,", 目标延迟: ", parsed_result.targeting_latency)
#for tag in parsed_result.fiducialResults:
# print(tag.robot_pose_target_space, tag.fiducial_id)
time.sleep(1) # 设置为 0 以获得最大帧率


except KeyboardInterrupt:
print("程序被用户中断,正在关闭。")
finally:
ll.disable_websocket()

方法

基于 REST 的方法

  • get_results(): 通过 HTTP GET 获取最新结果。
  • capture_snapshot(snapname): 使用给定名称捕获快照。
  • upload_snapshot(snapname, image_path): 上传具有给定名称和图像文件的快照。
  • snapshot_manifest(): 通过 HTTP GET 检索快照清单。
  • delete_snapshots(): 通过 HTTP GET 删除所有快照。
  • upload_neural_network(nn_type, file_path): 上传指定类型的神经网络文件。
  • hw_report(): 通过 HTTP GET 获取硬件报告。
  • cal_default(): 通过 HTTP GET 获取默认校准数据。
  • cal_file(): 通过 HTTP GET 从文件获取校准数据。
  • cal_eeprom(): 通过 HTTP GET 从 EEPROM 获取校准数据。
  • cal_latest(): 通过 HTTP GET 获取最新校准数据。
  • update_cal_eeprom(cal_data): 通过 HTTP POST 更新 EEPROM 中的校准数据。
  • update_cal_file(cal_data): 通过 HTTP POST 更新文件中的校准数据。
  • delete_cal_latest(): 通过 HTTP DELETE 删除最新校准数据。
  • delete_cal_eeprom(): 通过 HTTP DELETE 删除 EEPROM 中的校准数据。
  • delete_cal_file(): 通过 HTTP DELETE 删除文件中的校准数据。

基于 WebSocket 的方法

  • enable_websocket(): 在另一个线程中初始化并启动 WebSocket 连接。
  • disable_websocket(): 关闭 WebSocket 连接并加入线程。
  • get_latest_results(): 返回从 WebSocket 接收到的最新结果。

解析

  • limelightresults.parse_results(): 解析结果并返回 GeneralResult 对象

结果类规范

class GeneralResult:
def __init__(self, results):
self.barcode = results.get("Barcode", [])
self.classifierResults = [ClassifierResult(item) for item in results.get("Classifier", [])]
self.detectorResults = [DetectorResult(item) for item in results.get("Detector", [])]
self.fiducialResults = [FiducialResult(item) for item in results.get("Fiducial", [])]
self.retroResults = [RetroreflectiveResult(item) for item in results.get("Retro", [])]
self.botpose = results.get("botpose", [])
self.botpose_wpiblue = results.get("botpose_wpiblue", [])
self.botpose_wpired = results.get("botpose_wpired", [])
self.capture_latency = results.get("cl", 0)
self.pipeline_id = results.get("pID", 0)
self.robot_pose_target_space = results.get("t6c_rs", [])
self.targeting_latency = results.get("tl", 0)
self.timestamp = results.get("ts", 0)
self.validity = results.get("v", 0)
self.parse_latency = 0.0


class RetroreflectiveResult:
def __init__(self, retro_data):
self.points = retro_data["pts"]
self.camera_pose_target_space = retro_data["t6c_ts"]
self.robot_pose_field_space = retro_data["t6r_fs"]
self.robot_pose_target_space = retro_data["t6r_ts"]
self.target_pose_camera_space = retro_data["t6t_cs"]
self.target_pose_robot_space = retro_data["t6t_rs"]
self.target_area = retro_data["ta"]
self.target_x_degrees = retro_data["tx"]
self.target_x_pixels = retro_data["txp"]
self.target_y_degrees = retro_data["ty"]
self.target_y_pixels = retro_data["typ"]

class FiducialResult:
def __init__(self, fiducial_data):
self.fiducial_id = fiducial_data["fID"]
self.family = fiducial_data["fam"]
self.points = fiducial_data["pts"]
self.skew = fiducial_data["skew"]
self.camera_pose_target_space = fiducial_data["t6c_ts"]
self.robot_pose_field_space = fiducial_data["t6r_fs"]
self.robot_pose_target_space = fiducial_data["t6r_ts"]
self.target_pose_camera_space = fiducial_data["t6t_cs"]
self.target_pose_robot_space = fiducial_data["t6t_rs"]
self.target_area = fiducial_data["ta"]
self.target_x_degrees = fiducial_data["tx"]
self.target_x_pixels = fiducial_data["txp"]
self.target_y_degrees = fiducial_data["ty"]
self.target_y_pixels = fiducial_data["typ"]

class DetectorResult:
def __init__(self, detector_data):
self.class_name = detector_data["class"]
self.class_id = detector_data["classID"]
self.confidence = detector_data["conf"]
self.points = detector_data["pts"]
self.target_area = detector_data["ta"]
self.target_x_degrees = detector_data["tx"]
self.target_x_pixels = detector_data["txp"]
self.target_y_degrees = detector_data["ty"]
self.target_y_pixels = detector_data["typ"]

class ClassifierResult:
def __init__(self, classifier_data):
self.class_name = classifier_data["class"]
self.class_id = classifier_data["classID"]
self.confidence = classifier_data["conf"]