我使用 python 制作了一个检测图像中的对象的项目,但我需要为输入/输出做 GUI


`import cv2
import numpy as np
import time
from tkinter import * 
import os
from tkinter.ttk import *
from tkinter.filedialog import askopenfilename
from tkinter import filedialog
from tkinter import *
cv2.setUseOptimized(True)
cv2.useOptimized()
x1 = cv2.getTickCount()
model = cv2.dnn.readNetFromTensorflow("frozen_inference_graph.pb","ssd_mobilenet_v2_coco_2018_03_29.pbtxt")
classNames = {0:'background',1:'person',2:'bicycle',3:'car',7:'train'                               
,13:'stop sign',17:'cat',18:'dog',44:'bottle',48:'fork',49:'knife',                        
51:'bowl',53:'apple',55:'orange',73:'laptop', 
77: 'cell phone',78: 'microwave',79:'oven',80:'toaster'}
#image = cv2.imread("fork1.jpg")
def browse_image():
global folder_path
filename = filedialog.askopenfilename()
img = str(os.path.basename(filename))
return img
x=browse_image()
print(x)         
image = cv2.imread(x)   
root = Tk()
folder_path = StringVar()
lbl1 = Label(master=root,textvariable=folder_path)
lbl1.grid(row=0, column=1)
button2 = Button(text="Browse",command=browse_image)
button2.grid(row=0, column=3)

image = cv2.resize(image,(300,300))
image = cv2.medianBlur(image, 5)
w,h,_ = image.shape
model.setInput(cv2.dnn.blobFromImage(image, size = (w,h), swapRB=True))
output = model.forward()

def id_class_name(class_id, classes):
for key, value in classes.items():
if class_id == key:
return value
for detection in output[0, 0, :, :]:
confidence = detection[2]
if confidence > 0.5:
class_id = detection[1]
class_name = id_class_name(class_id,classNames)
#If the object doesnt match a one in the dataset or its skewed
if class_name == None or class_name == "background":
print("unable to define")
else:  
print(class_name)
x2 = cv2.getTickCount()
t = (x2-x1)/cv2.getTickFrequency()
print(t)

所以这是我的代码,我正在尝试检测我从路径中选择的图像中的对象,并使用另一种files.py来支持此代码运行,但我希望输出显示在 GUI 窗口中,其中包含文件名/图像.jpeg而不是打印在终端上。

我的问题是如何将此代码的输出放入 GUI 窗口而不是在终端上输出?

生成代码时的示例(例如 https://tkdocs.com/tutorial/firstexample.html 中的示例(是很好的起点。在这种情况下,我使用了这个例子,因为您显然想将小部件放在网格上。

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import os
def browse_image(*args):
global folder_path
filename = filedialog.askopenfilename()
img = str(os.path.basename(filename))
pic.set(img)
return img
root = Tk()
root.title("Tina Gh")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
pic = StringVar()
pic.set('')
ttk.Label(mainframe, textvariable=pic).grid(column=0, row=1, sticky=(W, E))
ttk.Button(mainframe, text="Do it", command=browse_image).grid(column=3, row=3, sticky=W)
ttk.Label(mainframe, text="Path").grid(column=0, row=0, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.bind('<Return>', browse_image)
root.mainloop()

我假设您只想在图像中显示检测到的对象的名称。您只需在 Tkinter 窗口中显示文本即可执行此操作。这可以使用 Tkinter 文本小部件来完成,可以使用以下代码创建:

import tkinter as tk
root = tk.Tk()
text = tk.Text(root, height=2, width=10) # height and width can be changed to suit your needs
text.pack()
text.insert(tk.END, "Your text here") # put text in the text widget
tk.mainloop()

如果您想格式化、着色或以其他方式更改文本的外观或将其放置在特定位置,请查看 Tkinter 文档,可在此处或此处获得

欢迎来到Stackoverflow!

试试这个代码:

import cv2
import numpy as np
import time
from tkinter import * 
import os
from tkinter.ttk import *
from tkinter.filedialog import askopenfilename
from tkinter import filedialog
from tkinter import *

cv2.setUseOptimized(True)
cv2.useOptimized()
x1 = cv2.getTickCount()
model = cv2.dnn.readNetFromTensorflow("frozen_inference_graph.pb","ssd_mobilenet_v2_coco_2018_03_29.pbtxt")
classNames = {0:'background',1:'person',2:'bicycle',3:'car',7:'train'                               
,13:'stop sign',17:'cat',18:'dog',44:'bottle',48:'fork',49:'knife',                        
51:'bowl',53:'apple',55:'orange',73:'laptop', 
77: 'cell phone',78: 'microwave',79:'oven',80:'toaster'}
#image = cv2.imread("fork1.jpg")

def browse_image():
filename = filedialog.askopenfilename()
img = str(os.path.basename(filename))
lbl1.config(text=img)
lbl1.grid(row=0, column=1)
return img

x=browse_image()
print(x)
image = cv2.imread(x)   
root = Tk()
lbl1 = Label(root)
button2 = Button(text="Browse", command=browse_image)
button2.grid(row=0, column=3)

image = cv2.resize(image,(300,300))
image = cv2.medianBlur(image, 5)
w,h,_ = image.shape
model.setInput(cv2.dnn.blobFromImage(image, size = (w,h), swapRB=True))
output = model.forward()
canvas = Label(gui, image=output)
canvas.grid(row=0, column=0)

def id_class_name(class_id, classes):
for key, value in classes.items():
if class_id == key:
return value
for detection in output[0, 0, :, :]:
confidence = detection[2]
if confidence > 0.5:
class_id = detection[1]
class_name = id_class_name(class_id,classNames)
#If the object doesnt match a one in the dataset or its skewed
if class_name == None or class_name == "background":
print("unable to define")
else:  
print(class_name)
x2 = cv2.getTickCount()
t = (x2-x1)/cv2.getTickFrequency()
print(t)

最新更新