"numpy.float 64" tkinter 中的对象不可调用错误。我该如何解决这个错误?
"numpy.float 64" object is not callable error in tkinter. How can I solve this error?
我正在尝试提取在 tkinter GUI 中放置在标签中的图像的蒙版区域像素坐标。然而,我第一次 运行 代码时,它工作正常,但是当我第二次 运行 时,它抛出了一个错误。这是我的代码,我还在下面添加了我的例外。感谢您阅读。祝你有美好的一天!
#Mask image and retrieve coordinates of the mask
from tkinter import *
import tkinter
import numpy as np
import cv2
import sys
import PIL.Image as imge
import PIL.ImageTk as imtk
curPth = sys.path[0]
imgPth = curPth+'/Img_GUI.png'
tmpPth = curPth+'/temp.png'
ev = None
thikness = 25
def click(event):
global ev, back, cropped, y,Y, x,X, mask, out
if ev == None:
ev = event
return None
im = cv2.imread(imgPth)
mask = cv2.cvtColor(im.copy()*0, cv2.COLOR_BGR2GRAY)
cv2.line(mask, pt1=(ev.x, ev.y), pt2=(event.x, event.y),
color=(255, 0, 0), thickness=thikness)
mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
out = im.copy()
out[np.where(mask == 0)] = 255
out = cv2.cvtColor(out, cv2.COLOR_BGR2RGB)
back = imtk.PhotoImage(image=imge.fromarray(out))
lbl.config(image=back)
x, X = min(ev.x, event.x)-thikness//2, max(ev.x, event.x)+thikness//2
y, Y = min(ev.y, event.y)-thikness//2, max(ev.y, event.y)+thikness//2
cropped = mask[y:Y, x:X]
print(cropped, cropped.shape)
ev = event
print(x, y, X, Y)
root = Tk()
back = PhotoImage(file=imgPth)
lbl = Label(root, image=back)
lbl.place(x=0, y=0)
root.bind('<Button-1>', lambda event: click(event))
root.mainloop()
我的错误是这样的
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\aravi\anaconda3\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "<ipython-input-7-e0e6e9918250>", line 53, in <lambda>
root.bind('<Button-1>', lambda event: click(event))
File "<ipython-input-7-e0e6e9918250>", line 39, in click
x, X = min(ev.x, event.x)-thikness//2, max(ev.x, event.x)+thikness//2
TypeError: 'numpy.float64' object is not callable
我的具体错误是由于我在 jupyter notebook 的另一个单元格中使用了最小值、最大值,导致这段代码混淆了从哪个最小值中选择,正如@acw1668 在评论中所说。但是,如本 article 中所述,如果未正确使用算术运算符,也会出现此错误。
我正在尝试提取在 tkinter GUI 中放置在标签中的图像的蒙版区域像素坐标。然而,我第一次 运行 代码时,它工作正常,但是当我第二次 运行 时,它抛出了一个错误。这是我的代码,我还在下面添加了我的例外。感谢您阅读。祝你有美好的一天!
#Mask image and retrieve coordinates of the mask
from tkinter import *
import tkinter
import numpy as np
import cv2
import sys
import PIL.Image as imge
import PIL.ImageTk as imtk
curPth = sys.path[0]
imgPth = curPth+'/Img_GUI.png'
tmpPth = curPth+'/temp.png'
ev = None
thikness = 25
def click(event):
global ev, back, cropped, y,Y, x,X, mask, out
if ev == None:
ev = event
return None
im = cv2.imread(imgPth)
mask = cv2.cvtColor(im.copy()*0, cv2.COLOR_BGR2GRAY)
cv2.line(mask, pt1=(ev.x, ev.y), pt2=(event.x, event.y),
color=(255, 0, 0), thickness=thikness)
mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
out = im.copy()
out[np.where(mask == 0)] = 255
out = cv2.cvtColor(out, cv2.COLOR_BGR2RGB)
back = imtk.PhotoImage(image=imge.fromarray(out))
lbl.config(image=back)
x, X = min(ev.x, event.x)-thikness//2, max(ev.x, event.x)+thikness//2
y, Y = min(ev.y, event.y)-thikness//2, max(ev.y, event.y)+thikness//2
cropped = mask[y:Y, x:X]
print(cropped, cropped.shape)
ev = event
print(x, y, X, Y)
root = Tk()
back = PhotoImage(file=imgPth)
lbl = Label(root, image=back)
lbl.place(x=0, y=0)
root.bind('<Button-1>', lambda event: click(event))
root.mainloop()
我的错误是这样的
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\aravi\anaconda3\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "<ipython-input-7-e0e6e9918250>", line 53, in <lambda>
root.bind('<Button-1>', lambda event: click(event))
File "<ipython-input-7-e0e6e9918250>", line 39, in click
x, X = min(ev.x, event.x)-thikness//2, max(ev.x, event.x)+thikness//2
TypeError: 'numpy.float64' object is not callable
我的具体错误是由于我在 jupyter notebook 的另一个单元格中使用了最小值、最大值,导致这段代码混淆了从哪个最小值中选择,正如@acw1668 在评论中所说。但是,如本 article 中所述,如果未正确使用算术运算符,也会出现此错误。