我有一个python程序,我正试图在Jupyter notebook的shell上运行它。该代码在notebook中运行良好,但是当我导出到shell时,我一直收到以下错误
'function' object has no attribute 'display_formatter'
import cv2
import numpy as np
from scipy import spatial
from IPython import get_ipython
def make_mask(b,image):
mask=np.zeros((image.shape[0],image.shape[1],1),dtype=np.unint8)
for xx,yy in enumarate(b):
mask[yy:,xx]=255
return mask
def display_mask(b,image,color=[0,0,255]):
result=image.copy()
overlay=np.full(image.shape,color,image.dtype)
display(cv2.addWeighted(cv2.bitwise_and(overlay,overlay,mask=make_mask(b,image)),1,image,1,0,result))
def display_cv2_image(image):
return cv2.imencode('.png', image)[1].tostring()
ip=get_ipython()
png_f = ip.display_formatter.formatters['image/png']
png_f.for_type_by_name('numpy', 'ndarray', display_cv2_image);我已经显式导入了IPython,但仍然没有任何进展。
发布于 2020-08-23 06:19:56
我真的解决了。I get_ipython的工作是获取全局InteractiveShell实例,如果没有注册InteractiveShell实例,则返回None。
我找到了两种解决问题的方法。1.在普通shell中导入get_ipython将不起作用,直到您通过ipython运行它会将get_python创建为全局变量
$ ipython python/myscripy.py2.注释掉所有的get_ipython命令,因为您已经运行了一个就绪的python shell实例
我希望这也能帮助到一些人
https://stackoverflow.com/questions/63541463
复制相似问题