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:03:03 浏览: 79
PIL.Image.open和cv2.imread的比较与相互转换的方法
5星 · 资源好评率100%
要实现将控制台输出 x、y、宽、高、类别,需要修改 site 函数中的输出语句:
```python
print("x:", x, "y:", y, "width:", s[2] - s[0], "height:", s[3] - s[1], "class:", names[int(s[5])])
```
同时,在创建表格内容时,也需要将输出内容修改为相应的变量:
```python
tk.Label(table, text=result[0]).grid(row=i + 1, column=0)
tk.Label(table, text=result[1]).grid(row=i + 1, column=1)
tk.Label(table, text=result[2]).grid(row=i + 1, column=2)
tk.Label(table, text=result[3]).grid(row=i + 1, column=3)
tk.Label(table, text=names[int(result[4])]).grid(row=i + 1, column=4)
```
完整代码如下:
```python
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, result in enumerate(results):
tk.Label(table, text=result[0]).grid(row=i + 1, column=0)
tk.Label(table, text=result[1]).grid(row=i + 1, column=1)
tk.Label(table, text=result[2]).grid(row=i + 1, column=2)
tk.Label(table, text=result[3]).grid(row=i + 1, column=3)
tk.Label(table, text=names[int(result[4])]).grid(row=i + 1, column=4)
# 创建选择按钮
select_button = tk.Button(table, text="Select", command=lambda s=result: select_result(s))
select_button.grid(row=i + 1, column=5)
# 定义选择结果函数
def select_result(result):
print("Selected result:", result)
window.mainloop()
```
阅读全文