<?xml version="1.0" encoding="UTF-8"?>
<CalibInfo><CalibInputParam><CalibParam ParamName="CreateCalibTime" DataType="string"><ParamValue>2024-04-23 19:17:09</ParamValue></CalibParam><CalibParam ParamName="CalibType" DataType="string"><ParamValue>NPointCalib</ParamValue></CalibParam><CalibParam ParamName="TransNum" DataType="int"><ParamValue>9</ParamValue></CalibParam><CalibParam ParamName="RotNum" DataType="int"><ParamValue>3</ParamValue></CalibParam><CalibParam ParamName="CalibErrStatus" DataType="int"><ParamValue>269496395</ParamValue></CalibParam><CalibParam ParamName="TransError" DataType="float"><ParamValue>1.863625</ParamValue></CalibParam><CalibParam ParamName="RotError" DataType="float"><ParamValue>0.99647576</ParamValue></CalibParam><CalibParam ParamName="TransWorldError" DataType="float"><ParamValue>0.24252573</ParamValue></CalibParam><CalibParam ParamName="RotWorldError" DataType="float"><ParamValue>0.12967792</ParamValue></CalibParam><CalibParam ParamName="PixelPrecisionX" DataType="float"><ParamValue>0.12997331</ParamValue></CalibParam><CalibParam ParamName="PixelPrecisionY" DataType="float"><ParamValue>0.13029985</ParamValue></CalibParam><CalibParam ParamName="PixelPrecision" DataType="float"><ParamValue>0.13013655</ParamValue></CalibParam><CalibPointFListParam ParamName="ImagePointLst" DataType="CalibPointList"><PointF><X>1216.51</X><Y>779.13611</Y><R>0</R></PointF><PointF><X>2908.1958</X><Y>787.07654</Y><R>0</R></PointF><PointF><X>4600.999</X><Y>784.99518</Y><R>0</R></PointF><PointF><X>4599.3091</X><Y>1714.9545</Y><R>0</R></PointF><PointF><X>2903.8613</X><Y>1714.681</Y><R>0</R></PointF><PointF><X>1209.8541</X><Y>1706.0481</Y><R>0</R></PointF><PointF><X>1203.0206</X><Y>2635.2905</Y><R>0</R></PointF><PointF><X>2898.4265</X><Y>2643.5801</Y><R>0</R></PointF><PointF><X>4596.5581</X><Y>2643.3296</Y><R>0</R></PointF><PointF><X>3520.8601</X><Y>1254.7367</Y><R>-60</R></PointF><PointF><X>2903.677</X><Y>1714.9659</Y><R>0</R></PointF><PointF><X>2992.3616</X><Y>2481.5713</Y><R>60</R></PointF></CalibPointFListParam><CalibPointFListParam ParamName="WorldPointLst" DataType="CalibPointList"><PointF><X>475.71002</X><Y>-122.05</Y><R>0</R></PointF><PointF><X>475.71002</X><Y>97.949997</Y><R>0</R></PointF><PointF><X>475.71002</X><Y>317.95001</Y><R>0</R></PointF><PointF><X>595.71002</X><Y>317.95001</Y><R>0</R></PointF><PointF><X>595.71002</X><Y>97.949997</Y><R>0</R></PointF><PointF><X>595.71002</X><Y>-122.05</Y><R>0</R></PointF><PointF><X>715.71002</X><Y>-122.05</Y><R>0</R></PointF><PointF><X>715.71002</X><Y>97.949997</Y><R>0</R></PointF><PointF><X>715.71002</X><Y>317.95001</Y><R>0</R></PointF><PointF><X>595.71002</X><Y>97.949997</Y><R>-60</R></PointF><PointF><X>595.71002</X><Y>97.949997</Y><R>0</R></PointF><PointF><X>595.71002</X><Y>97.949997</Y><R>60</R></PointF></CalibPointFListParam></CalibInputParam><CalibOutputParam><CalibParam ParamName="RotDirectionState" DataType="int"><ParamValue>-1</ParamValue></CalibParam><CalibParam ParamName="IsRightCoorA" DataType="int"><ParamValue>-1</ParamValue></CalibParam><PointF ParamName="RotCenterImagePoint" DataType="CalibPointF"><RotCenterImagePointX>3611.1401</RotCenterImagePointX><RotCenterImagePointY>2018.9244</RotCenterImagePointY><RotCenterImageR>-999</RotCenterImageR></PointF><PointF ParamName="RotCenterWorldPoint" DataType="CalibPointF"><RotCenterWorldPointX>261.2843</RotCenterWorldPointX><RotCenterWorldPointY>470.94211</RotCenterWorldPointY><RotCenterWorldR>-999</RotCenterWorldR></PointF><CalibFloatListParam ParamName="CalibMatrix" DataType="FloatList"><ParamValue>-0.00049192336</ParamValue><ParamValue>0.13029745</ParamValue><ParamValue>-261.2843</ParamValue><ParamValue>0.12997237</ParamValue><ParamValue>0.0007893648</ParamValue><ParamValue>-470.94211</ParamValue><ParamValue>-2.5900243e-007</ParamValue><ParamValue>1.2700635e-006</ParamValue><ParamValue>1</ParamValue></CalibFloatListParam></CalibOutputParam>
</CalibInfo>
思路是转json ;json在python里面简直就是字典一样的存在所以直接当字典操作即可;最后读到的点位转成".tup"文件方便hdev助手以及其他调用hdev的程序读取当然稍加修改就可以存成其他格式;上程序:
python">#pip install xmltodictimport os,sys,time,json,xmltodict
from datetime import datetime
def cmd(s="pause"):os.system(s)
def xml_to_json(xml_file_path, json_file_path):try:with open(xml_file_path, 'r') as file:# 从文件中读取XML数据xml_data = file.read()dict_data = xmltodict.parse(xml_data)# 将XML转换为字典json_data = json.dumps(dict_data, indent=4)# 将字典转换为JSON#print(f"JSON{json_data}")with open(json_file_path, 'w') as json_file:# 将JSON数据写入文件json_file.write(json_data)print(f"JSON file has been saved to {json_file_path}")return json_dataexcept Exception as e:print(f" error : {e}")xml_file_path = 'h15.8r1.xml'
json_file_path = 'output.json'
json_data=xml_to_json(xml_file_path, json_file_path)# 调用函数PX=[];
PY=[];
PR=[];
QX=[];
QY=[];
QR=[]; def get_ppp_params(obj):if isinstance(obj, dict):pic=obj['CalibInfo']['CalibInputParam']['CalibPointFListParam'][0]['PointF']#图像点wor=obj['CalibInfo']['CalibInputParam']['CalibPointFListParam'][1]['PointF']#物理点print('\n\n\n')for item in pic:print(f"{item}; {item['X']} {item['Y']} {item['R']}")PX.append(item['X']);PY.append(item['Y']);PR.append(item['R']);print('\n\n\n')for item in wor:print(f"{item}; {item['X']} {item['Y']} {item['R']}")QX.append(item['X']);QY.append(item['Y']);QR.append(item['R']);#with open('ppp.json', 'w') as json_file:# json_file.write(f"{iii}")data = json.loads(json_data)get_ppp_params(data)# 取出来了 显示一下
print(f"""
{PX},{PY},{PR}, /
{QX}, {QY}, {QR} """)print('\n\n\n')import halcon as haha.write_tuple(PX,'PX.tup')
ha.write_tuple(PY,'PY.tup')
ha.write_tuple(PR,'PR.tup')
ha.write_tuple(QX,'QX.tup')
ha.write_tuple(QY,'QY.tup')
ha.write_tuple(QR,'QR.tup')cmd(s="pause")
好了 运行起来文件就会保存到程序目录;
其他语言 比如C++
我有一个损招; 不说了看程序:
#include <iostream>
#include<Python.h>
int main()
{Py_Initialize();//Py_Finalize();PyRun_SimpleString("print('C++ call python!')");PyRun_SimpleString("import os");//PyRun_SimpleString("os.system('pause')");PyRun_SimpleString("from tkinter import *");PyRun_SimpleString("import tkinter as tk");//PyRun_SimpleString("window = Tk()");PyRun_SimpleString("def callback() :print('click me!')");//import tkinter as tkPyRun_SimpleString("b = tk.Button(window, text='hj', command=callback).pack()");PyRun_SimpleString("window.mainloop()");
}
你要非说这还是python不是C++ 我要告诉你 那你自己想办法;
你如果是C# 那么
public string _CMD(string strInput){string strOuput;Process p = new Process();p.StartInfo.FileName = "cmd.exe";//设置要启动的应用程序p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动p.StartInfo.RedirectStandardInput = true;// 接受来自调用程序的输入信息p.StartInfo.RedirectStandardOutput = true;//输出信息p.StartInfo.RedirectStandardError = true;// 输出错误p.StartInfo.CreateNoWindow = true;//不显示程序窗口// p.StartInfo.CreateNoWindow = false; //显示程序窗口p.Start();//启动程序p.StandardInput.WriteLine(strInput + "&exit");//向cmd窗口发送输入信息p.StandardInput.AutoFlush = true;strOuput = p.StandardOutput.ReadToEnd(); //获取输出信息p.WaitForExit(); //等待程序执行完退出进程p.Close();return strOuput;}// string str = _CMD(@"python xxx.py");
这样能用; 我更倾向于pythonnet 不过C#里面到处 dynamic 感觉不太好;
当然如果你是JS那么js比python灵活 我就不搞笑了;