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 16:03:15 浏览: 115
好的,首先需要将代码中的图像读取部分修改为从摄像头中读取图像。这可以使用OpenCV库实现。修改后的代码如下:
```python
import cv2
import tkinter as tk
from PIL import Image
def site(pred, names):
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
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 = 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)
cap.release()
return results
# 创建GUI界面
window = tk.Tk()
window.geometry("800x600")
# 创建按钮
def show_results(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)
# 创建表格内容
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)
def update_gui(pred, names):
results = site(pred, names)
# 创建按钮
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()
# 调用update_gui函数更新GUI界面
update_gui(pred, names)
window.mainloop()
```
这里我们将摄像头的读取放在了 `site` 函数中,并且在 `update_gui` 函数中调用了 `site` 函数来获取预测结果。在获取预测结果后,我们再创建按钮来显示每个类别的检测结果,并且添加了选择结果的功能。
同时,我们将 `source` 参数从函数中删除,因为我们不再需要从文件中读取图像。另外,我们将 `site` 函数的第一个参数改为 `pred`,以便更好地表达这个参数的作用。
阅读全文