I am creating a face detection algorithm which should take in images from a folder as input but I get this error:
import dlib
import argparse
import cv2
import sys
import time
import process_dlib_boxes
# construct the argument parser
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', default=r"C:\Users\awais\OneDrive\Documents\Greenwich Uni work\Face detec work\images folder",
help='path to the input image')
parser.add_argument('-u', '--upsample', type=float,
help='factor by which to upsample the image, default None, '
'pass 1, 2, 3, ...')
args = vars(parser.parse_args())
# read the image and convert to RGB color format
image = cv2.imread(args['input'])
image_cvt = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# path for saving the result image
save_name = f"outputs/{args['input'].split('/')[-1].split('.')[0]}_u{args['upsample']}.jpg"
# initilaize the Dlib face detector according to the upsampling value
detector = dlib.get_frontal_face_detector()
i get this error:
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('C:\Users\awais\OneDrive\Documents\Greenwich Uni work\Face detec work\images folder'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "C:\Users\awais\OneDrive\Documents\Greenwich Uni work\Face detec work\face_det_image.py", line 20, in <module>
image_cvt = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
CodePudding user response:
I assume the problem is that cv2.imread
is returning None
because it is unable to read the input image. This can happen if the file path provided to cv2.imread
is incorrect or if the file does not exist.
You can try printing the value of args['input']
to make sure it is correct and points to a valid image file. You can also try using the os.path.isfile
function to check if the file exists before trying to read it. Here is an example:
import dlib
import argparse
import cv2
import sys
import time
import os
import process_dlib_boxes
# construct the argument parser
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', default=r"C:\Users\awais\OneDrive\Documents\Greenwich Uni work\Face detec work\images folder",
help='path to the input image')
parser.add_argument('-u', '--upsample', type=float,
help='factor by which to upsample the image, default None, '
'pass 1, 2, 3, ...')
args = vars(parser.parse_args())
# check if the input file exists
if not os.path.isfile(args['input']):
print(f"Error: The file {args['input']} does not exist")
sys.exit(1)
# read the image and convert to RGB color format
image = cv2.imread(args['input'])
image_cvt = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# path for saving the result image
save_name = f"outputs/{args['input'].split('/')[-1].split('.')[0]}_u{args['upsample']}.jpg"
# initilaize the Dlib face detector according to the upsampling value
detector = dlib.get_frontal_face_detector()
CodePudding user response:
imread()
is meant to read single image files, not entire folders.
You must pass a path to a file, not a path to an entire directory.
You passed a path to a directory. That is why imread()
failed.