简体   繁体   中英

TypeError: can't convert type 'ndarray' to numerator/denominator

Error which I am getting Here is my code. I am not able to understand this error. I have tried a lot but not getting the correct solution. Please help me to know what is the error.

import numpy as np
import cv2
from skimage.feature import local_binary_pattern
import statistics 
from os import read
from google.colab.patches import cv2_imshow

cap = cv2.VideoCapture('/content/4.forged4.avi')

countF = cap.get(cv2.CAP_PROP_FPS) # calculate no of frames per sec

while True:
   ret, frame = cap.read()
   if ret:
     gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     gray = np.array(gray_img).astype(int)
     frames.append(gray)
     #cv2_imshow(gray_img) 
     radius = 3

     p = 8 * radius
     lbp =  local_binary_pattern(gray_img,p, radius, method = 'uniform') 
     print(lbp)
     val =  statistics.stdev(lbp) # caluculated standard deviation
     print(val)
     if val == 0:
       print("Authenticated")
     else:
       print("Forgery Detected!")

statistics.stdev expects as argument an iterable (eg a list) of real numbers with at least two values. You are passing to it the variable lbp which does not satisfy that requirement (it's a list of lists). You can run simple experiments to see how functions behave if the documentation is not quite clear. Here's eg what I did:

$ python3
>>> import statistics
>>> help(statistics.stdev)
Help on function stdev in module statistics:

stdev(data, xbar=None)
    Return the square root of the sample variance.

    See ``variance`` for arguments and other details.
    ...
>>> help(statistics.variance)
Help on function variance in module statistics:

variance(data, xbar=None)
    Return the sample variance of data.

    data should be an iterable of Real-valued numbers, with at least two
    values.
    ....
>>> l = [[1.0 ,2.0, 3.0], [4.0, 5.0, 6.0]]
>>> statistics.stdev(l)
Traceback (most recent call last):
...
TypeError: can't convert type 'list' to numerator/denominator
>>> l = [1.0 ,2.0, 3.0]
>>> statistics.stdev(l)
1.0
>>> 

As you can see, passing a list of lists of numbers to stdev produces the error you get, whereas passing a list of numbers works properly.

The moral of the story is that the local binary pattern lbp that you calculate two lines above is not of the proper type to pass to stdev .

Now, that's the reason that the python program fails, but it's likely that the underlying reason for this is confusion on your part about what should be done. If it makes sense to calculate the standard deviation of the entries in lbp (and it's a big if that I cannot answer for you), then you could try flattening lbp , which is a list of lists down to a single list, before passing it to stdev . But does it make sense to do that? Only you can answer that question and only if you understand the problem that you are trying to solve.

@rohit-sharma, you must flatten your data before use in statistics module.

Assume lbp variable is a ndarray:

change:

     val =  statistics.stdev(lbp) # caluculated standard deviation

to:

     val =  statistics.stdev(lbp.flatten()) # caluculated standard deviation

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM