此代码用于在OpenCV中使用imread
函数存储传递的图像,然后在输入水果名称并单击“标识”按钮时,应该显示存储在该名称中的所有图像。代码出现后,我得到一个错误。
from tkinter import *
import sqlite3
import cv2
import numpy as np
import matplotlib.image as mpimg
from tkinter import messagebox
from matplotlib import pyplot as plt
def add_command():
fname=Fruitname.get()
iname=Imagename.get()
img = cv2.imread(str(iname),0)
conn=sqlite3.connect('Fruits.db')
with conn:
cursor=conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS FRUITSINFO(NAME TEXT,IMAGENAME TEXT)')
cursor.execute('INSERT INTO FRUITSINFO(NAME,IMAGENAME) VALUES(?,?)',(fname,iname))
conn.commit()
def identify_command():
fname=Fruitname.get()
conn=sqlite3.connect('Fruits.db')
with conn:
cursor=conn.cursor()
cursor.execute("SELECT IMAGENAME FROM FRUITSINFO WHERE NAME = ?", (fname,))
v=cursor.fetchone()[0]
s=str(v)
imgplot=plt.imshow(s)
plt.show()
root=Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
width = 900
height = 500
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
root.resizable(0, 0)
Fruitname = StringVar()
Imagename = StringVar()
Top=Frame(root,width=900,height=50,bd=8,relief="raise")
Top.pack(side=TOP)
Left=Frame(root,width=300,height=500,relief="raise")
Left.pack(side=LEFT)
Right=Frame(root,width=600,height=500,bd=8,relief="raise")
Right.pack(side=RIGHT)
Forms = Frame(Left, width=300, height=450)
Forms.pack(side=TOP)
Buttons = Frame(Left, width=300, height=100, bd=8, relief="raise")
Buttons.pack(side=BOTTOM)
txt_title = Label(Top, width=900, font=('arial', 24), text = "Fruit Detection")
txt_title.pack()
txt_fruitname = Label(Forms, text="Fruit name:", font=('arial', 16), bd=15)
txt_fruitname.grid(row=0, stick="e")
txt_imagename = Label(Forms, text="Image name:", font=('arial', 16), bd=15)
txt_imagename.grid(row=1, stick="e")
txt_result = Label(Buttons)
txt_result.pack(side=TOP)
fruitname = Entry(Forms, textvariable=Fruitname, width=30)
fruitname.grid(row=0, column=1)
imagename = Entry(Forms, textvariable=Imagename, width=30)
imagename.grid(row=1, column=1)
btn_add = Button(Buttons, width=10, text="ADD",command=add_command)
btn_add.pack(side=LEFT)
btn_identify = Button(Buttons, width=10, text="IDENTIFY",command=identify_command)
btn_identify.pack(side=LEFT)
btn_exit = Button(Buttons, width=10, text="Exit")
btn_exit.pack(side=LEFT)
产生以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\prath\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\prath\Desktop\Mini\fruitdetect_gui.py", line 27, in identify_command
imgplot=plt.imshow(s)
File "C:\Users\prath\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\pyplot.py", line 2699, in imshow
None else {}), **kwargs)
File "C:\Users\prath\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\__init__.py", line 1810, in inner
return func(ax, *args, **kwargs)
File "C:\Users\prath\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\axes\_axes.py", line 5494, in imshow
im.set_data(X)
File "C:\Users\prath\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\image.py", line 634, in set_data
raise TypeError("Image data cannot be converted to float")
TypeError: Image data cannot be converted to float
答案 0 :(得分:0)
plt.imshow(...)
需要一张图像。也许您正在寻找的是:
def identify_command():
fname = Fruitname.get()
conn = sqlite3.connect('Fruits.db')
with conn:
cursor=conn.cursor()
cursor.execute("SELECT IMAGENAME FROM FRUITSINFO WHERE NAME = ?", (fname,))
v = cursor.fetchone()[0]
image = mpimg.imread(str(v)) # << idk why you deleted this line on the edit
imgplot = plt.imshow(image)
plt.show()
如果您使用fetchall(...)
,则可能是这样的:
vs = cursor.fetchall() # vs = [('apple.png',), ('apple1.png',), ('apple2.png',)]
for v in vs: # e.g., v = ('apple.png',)
image = mpimg.imread(str(v[0])) # e.g., v[0] = 'apple.png'
imgplot = plt.imshow(image)
plt.show()