当我尝试运行导入语句时,为什么会出现 ModuleNotFoundError 错误?

时间:2021-06-07 16:05:09

标签: python import python-import modulenotfounderror

我正在尝试在 Jupyter 上导入这些,但是当我运行这些代码时出现错误,提示 ModuleNotFoundError: No module named 'anna_phog'。我有另一个名为 'anna_phog' 的 python 文件。我该如何解决这个问题?

下面是'anna_phog_demo.py',我在那里得到了错误

from anna_phog import anna_phog
import imageio
import matplotlib.pyplot as plt

image_path = "image_0058.jpg"
S = 8
angle = 360
Level = 3
roi = [1,225,1,300]
save=True

Image = imageio.imread(image_path)
p = anna_phog(Image, bin, angle, Level, roi)
print("P: \n{}".format(p))
print(len(p), type(p))

下面是'anna_phog.py'代码

import numpy as np
import imageio
import cv2
import matplotlib.pyplot as plt
   
def anna_phog(Img, bin, angle, L, roi):
    
    if Img.shape[2] == 3:
        G = cv2.cvtColor(Img, cv2.COLOR_BGR2GRAY)
    else:
        G = Img

    if np.sum(G) > 100:
        # apply automatic Canny edge detection using the computed median
        sigma = 0.33
        v = np.median(G)
        lower = int(max(0, (1.0 - sigma) * v))
        upper = int(min(255, (1.0 + sigma) * v))
        E = cv2.Canny(G,lower,upper) #high and low treshold
        GradientX, GradientY = np.gradient(G)
        GradientYY = np.gradient(GradientY, axis=1)

        Gr = np.sqrt(np.square(GradientX)+np.square(GradientY))
        index = GradientX == 0
        GradientX[index] = 1e-5 #maybe another value

        YX = GradientY*GradientX
        if angle == 180: A = ((np.arctan(YX)+(np.pi/2))*180)/np.pi
        if angle == 360: A = ((np.arctan2(GradientY,GradientX)+np.pi)*180)/np.pi

        bh, bv = anna_BinMatrix(A,E,Gr,angle,bin)

    else:
        bh = np.zeros(Img.shape)
        bv = np.zeros(Img.shape)

    bh_roi = bh[roi[0]:roi[1], roi[2]:roi[3]]
    bv_roi = bv[roi[0]:roi[1], roi[2]:roi[3]]

    p = anna_PhogDescriptor(bh_roi,bv_roi,L,bin)
    return p


def anna_BinMatrix(A,E,G,angle,bin):
    
    n, contorns = cv2.connectedComponents(E, connectivity=8)

    X = E.shape[1]
    Y = E.shape[0]
    bm = np.zeros(shape=(Y,X))
    bv = np.zeros(shape=(Y,X))

    nAngle = angle/bin

    for i in range(n):
        posY, posX = np.where(contorns==i)
        for j in range(posY.shape[0]):
            pos_x = posX[j]
            pos_y = posY[j]

            b = np.ceil(A[pos_y,pos_x]/nAngle)
            if b==0: bin=1
            if G[pos_y,pos_x]>0:
                bm[pos_y,pos_x] = b
                bv[pos_y,pos_x] = G[pos_y,pos_x]

    return (bm, bv)



def anna_PhogDescriptor(bh,bv,L,bin):
    
    p = np.array([])
    #level 0
    for b in range(bin):
        ind = bh==b
        p = np.append(p, np.sum(bv[ind]))

    #higher levels
    for l in range(1, L+1):
        x = int(np.trunc(bh.shape[1]/(2**l)))
        y = int(np.trunc(bh.shape[0]/(2**l)))
        for xx in range(0, bh.shape[1]-x+1, x):
            for yy in range(0, bh.shape[0]-y+1, y):
                print(l)
                bh_cella = bh[yy:yy+y, xx:xx+x]
                bv_cella = bv[yy:yy+y, xx:xx+x]

                for b in range(bin):
                    ind = bh_cella==b
                    p = np.append(p, np.sum(bv_cella[ind], axis=0))

    if np.sum(p)!=0:
        p = p/np.sum(p)

    return p

下面是我放置这些文件的文件夹的截图 folder path

0 个答案:

没有答案