from PIL import Image import tkinter as tk def site(source, pred, names): img = Image.open(source) x1, x2 = img.size print(x1) print(x2) print(img.size) results = {} for i1 in pred: s = [] for i2 in i1.data.cpu().numpy(): s1 = [] s = list(i2) # 获取中心的(x,y)坐标 x = s[0] = float(round((s[0] + s[2]) / x1 / 2, 4)) y = s[1] = float(round((s[1] + s[3]) / x2 / 2, 4)) # 位置判断 if x < 0.5 and y < 0.5: w = "2 site" elif x < 0.5 and y > 0.5: w = "3 site" elif x > 0.5 and y > 0.5: w = "4 site" else: w = "1 site" s1.append(x) s1.append(y) s1.append(s[2] - s[0]) # 预测框的宽 s1.append(s[3] - s[1]) # 预测框的高 s1.append(names[int(s[5])]) if s[4] < 0.6: break s1.append(w) # 将信息按物体分组 if names[int(s[5])] not in results: results[names[int(s[5])]] = [] results[names[int(s[5])]].append(s1) # 创建GUI界面 window = tk.Tk() window.geometry("800x600") # 创建按钮 for name in results.keys(): tk.Label(window, text="Object " + name + ":").pack() button = tk.Button(window, text="Show " + name + " results", command=lambda name=name: show_results(results[name])) button.pack() # 创建确定按钮 confirm_button = tk.Button(window, text="Confirm and Exit", command=window.quit) confirm_button.pack() def show_results(results): # 创建子界面 win = tk.Toplevel() window.geometry("800x600") win.title("Results") # 创建表格 table = tk.Frame(win) table.pack() # 创建表头 tk.Label(table, text="x").grid(row=0, column=0) tk.Label(table, text="y").grid(row=0, column=1) tk.Label(table, text="width").grid(row=0, column=2) tk.Label(table, text="height").grid(row=0, column=3) tk.Label(table, text="class").grid(row=0, column=4) # 创建表格内容 for i, s1 in enumerate(results): tk.Label(table, text=s1[0]).grid(row=i + 1, column=0) tk.Label(table, text=s1[1]).grid(row=i + 1, column=1) tk.Label(table, text=s1[2]).grid(row=i + 1, column=2) tk.Label(table, text=s1[3]).grid(row=i + 1, column=3) tk.Label(table, text=s1[4]).grid(row=i + 1, column=4) # 创建选择按钮 select_button = tk.Button(table, text="Select", command=lambda s=s1: select_result(s)) select_button.grid(row=i + 1, column=5) # 定义选择结果函数 def select_result(result): print("Selected result:", result) window.mainloop()在这个程序的基础上,修改这个程序将控制台输出x,y,宽,高经摄像头不动机械臂动自动手眼标定后与类别一块输出
时间: 2023-06-17 10:03:15 浏览: 79
可以尝试如下修改:
```
from PIL import Image
import tkinter as tk
def site(source, pred, names):
img = Image.open(source)
x1, x2 = img.size
print("Image size:", img.size)
results = {}
for i1 in pred:
s = []
for i2 in i1.data.cpu().numpy():
s1 = []
s = list(i2)
# 获取中心的(x,y)坐标
x = s[0] = float(round((s[0] + s[2]) / x1 / 2, 4))
y = s[1] = float(round((s[1] + s[3]) / x2 / 2, 4))
# 位置判断
if x < 0.5 and y < 0.5:
w = "2 site"
elif x < 0.5 and y > 0.5:
w = "3 site"
elif x > 0.5 and y > 0.5:
w = "4 site"
else:
w = "1 site"
s1.append(x)
s1.append(y)
s1.append(s[2] - s[0]) # 预测框的宽
s1.append(s[3] - s[1]) # 预测框的高
s1.append(names[int(s[5])])
if s[4] < 0.6:
break
s1.append(w)
# 将信息按物体分组
if names[int(s[5])] not in results:
results[names[int(s[5])]] = []
results[names[int(s[5])]].append(s1)
# 输出结果
for k, v in results.items():
print("Object", k, "results:")
for i, s in enumerate(v):
print("Result", i+1, "x:", s[0], "y:", s[1], "width:", s[2], "height:", s[3], "class:", s[4], "position:", s[5])
# 创建GUI界面
window = tk.Tk()
window.geometry("800x600")
# 创建按钮
for name in results.keys():
tk.Label(window, text="Object " + name + ":").pack()
button = tk.Button(window, text="Show " + name + " results", command=lambda name=name: show_results(results[name]))
button.pack()
# 创建确定按钮
confirm_button = tk.Button(window, text="Confirm and Exit", command=window.quit)
confirm_button.pack()
def show_results(results):
# 创建子界面
win = tk.Toplevel()
window.geometry("800x600")
win.title("Results")
# 创建表格
table = tk.Frame(win)
table.pack()
# 创建表头
tk.Label(table, text="x").grid(row=0, column=0)
tk.Label(table, text="y").grid(row=0, column=1)
tk.Label(table, text="width").grid(row=0, column=2)
tk.Label(table, text="height").grid(row=0, column=3)
tk.Label(table, text="class").grid(row=0, column=4)
tk.Label(table, text="position").grid(row=0, column=5)
# 创建表格内容
for i, s1 in enumerate(results):
tk.Label(table, text=s1[0]).grid(row=i + 1, column=0)
tk.Label(table, text=s1[1]).grid(row=i + 1, column=1)
tk.Label(table, text=s1[2]).grid(row=i + 1, column=2)
tk.Label(table, text=s1[3]).grid(row=i + 1, column=3)
tk.Label(table, text=s1[4]).grid(row=i + 1, column=4)
tk.Label(table, text=s1[5]).grid(row=i + 1, column=5)
# 创建选择按钮
select_button = tk.Button(table, text="Select", command=lambda s=s1: select_result(s))
select_button.grid(row=i + 1, column=6)
# 定义选择结果函数
def select_result(result):
print("Selected result:", result)
window.mainloop()
# 使用示例
source = "test.jpg"
pred = [some_predictions]
names = ["class1", "class2", "class3"]
site(source, pred, names)
```
其中,`site` 函数的输出结果会在程序运行时打印到控制台上。如果需要将其保存到文件中,可以使用重定向符号 `>` 进行输出重定向,例如:`python your_program.py > output.txt`。
阅读全文