'TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' when using cv2
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
# Get the training data we previously made
data_path = 'C:\\Users\\hp\\Unlock-Application\\frames'
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]
# Create arrays for training data and labels
Training_Data, Labels = [], []
# Open training images in our datapath
# Create a numpy array for training data
for i, files in enumerate(onlyfiles):
image_path = data_path + onlyfiles[i]
images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
Training_Data.append(np.asarray(images, dtype=np.uint8))
Labels.append(i)
# Create a numpy array for both training data and labels
Labels = np.asarray(Labels, dtype=np.int32)
# Initialize facial recognizer
model = cv2.face.LBPHFaceRecognizer_create()
# NOTE: For OpenCV 3.0 use cv2.face.createLBPHFaceRecognizer()
# Let's train our model
model.train(np.asarray(Training_Data), np.asarray(Labels))
print(cv2.__version__)
print("Model trained sucessefully")
When I run this code I get this error
File "c:\Users\hp\Unlock-Application\model.py", line 18, in <module>
Training_Data.append(np.asarray(images, dtype=np.uint8))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' any guess how to solve this?
Solution 1:[1]
I suspect the error originates from this line:
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]
You are checking if isfile but instead you should be checking if it's an image i.e if its .png, .jpg, because anything can be a file. The string method endswith() can be used for this.
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]
for f in listdir(data_path):
image_path = os.path.join(data_path, f)
if image_path.endswith(('.png', '.jpg', '.jpeg')): # you can add other formats
images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
