I have 2 python file which are ‘anna_phog.py’ and ‘anna_phog_demo.py’. There’s no error when I run ‘anna_phog.py’
However when I tried to run the ‘anna_phog_demo.py’ on Jupyter Notebook, I got TypeError: unsupported operand type(s) for /: ‘int’ and ‘builtin_function_or_method’
This is the error that I got :
TypeError Traceback (most recent call last) <ipython-input-28-47b447c36f03> in <module> 1 Image = imageio.imread(image_path) ----> 2 p = anna_phog(Image, bin, angle, Level, roi) 3 print("P: n{}".format(p)) 4 print(len(p), type(p)) ~DesktopFYPanna_phog.ipynb in anna_phog(Img, bin, angle, L, roi) 36 " E = cv2.Canny(G,lower,upper) #high and low tresholdn", 37 " GradientX, GradientY = np.gradient(G)n", ---> 38 " GradientYY = np.gradient(GradientY, axis=1)n", 39 " Gr = np.sqrt(np.square(GradientX)+np.square(GradientY))n", 40 " index = GradientX == 0n", ~DesktopFYPanna_phog.ipynb in anna_BinMatrix(A, E, G, angle, bin) 57 " return p" 58 ] ---> 59 }, 60 { 61 "cell_type": "code", TypeError: unsupported operand type(s) for /: 'int' and 'builtin_function_or_method'
I tried to change few parts but still got the same error. I include both of the code below if anyone can help me to remove this error.
This one is anna_phog_demo.py code
from ipynb.fs.full.anna_phog import anna_phog import imageio import matplotlib.pyplot as plt image_path = r"C:UsersuserDocumentsFYPCODEPHOGPHOG-mainPHOG-mainimage_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))
This is the anna_phog.py code
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
You have to assign some value to
bin = ...
before
p = anna_phog(Image, bin, angle, Level, roi)
If you don’t do this then it uses built-in function `bin
()` and later in `anna_BinMatrix` you have
nAngle = angle/bin
which makes problem because bin
is built-in function.
But I don’t know what value you should assign to this variable
Recent Comments