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 08:02:48 浏览: 206
Python各类图像库的图片读写方式总结(推荐)
首先,需要修改site函数中对预测框的信息处理部分,将其保存到一个字典中,其中键为类别,值为包含该类别所有预测框信息的列表。具体修改如下:
```python
def site(source, pred, names):
img = Image.open(source)
x1, x2 = img.size
results = {}
for i1 in pred:
s = []
for i2 in i1.data.cpu().numpy():
s1 = []
s = list(i2)
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)
return results
```
然后,在GUI界面中,需要修改按钮的回调函数,使其调用show_results函数显示所有预测框的信息,同时在表格中添加一列用于显示预测框的类别。具体修改如下:
```python
def button_callback(results):
win = tk.Toplevel()
win.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="location").grid(row=0, column=5)
i = 1
for name, boxes in results.items():
tk.Label(table, text="Object " + name + ":").grid(row=i, column=0, columnspan=5)
i += 1
for box in boxes:
tk.Label(table, text=box[0]).grid(row=i, column=0)
tk.Label(table, text=box[1]).grid(row=i, column=1)
tk.Label(table, text=box[2]).grid(row=i, column=2)
tk.Label(table, text=box[3]).grid(row=i, column=3)
tk.Label(table, text=box[4]).grid(row=i, column=4)
tk.Label(table, text=box[5]).grid(row=i, column=5)
i += 1
confirm_button = tk.Button(win, text="Confirm and Exit", command=win.quit)
confirm_button.pack()
```
最后,在主程序中调用site函数获取预测框信息,并将其传递给按钮回调函数。具体修改如下:
```python
results = site(source, pred, names)
button = tk.Button(window, text="Show Results", command=lambda: button_callback(results))
button.pack()
```
这样,程序就会在GUI界面中显示所有预测框的信息,包括它们的位置、大小、类别和在图像中的位置。
阅读全文