最近我在做一个课程设计,遇到要遍历中文文件夹的问题,刚开始不能,就翻译为英文,最后老师要求中文也可以用,所以我来告诉大家一个冷门又古老的函数,就是为解决中文编码问题,这个锅不要怪python,只能说OpenCV比较坑,下面上代码
def reading_images(fruit_img, fruit_labels, img_folder): # reading_images: a function that does the following steps: # 1. store the image pixels to fruit_img, and the label names to fruit_labels # 2. Read every image # 3. resize the images # 4. standardize the images # 5. return a list of image pixels ,hog freature and a list of labels for fruit_dir_path in glob.glob(img_folder): fruit_name = fruit_dir_path.split("\\")[-1] print(fruit_dir_path) for image_path in glob.glob(os.path.join(fruit_dir_path,'*.jpg')): print(image_path) image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8),1)
主要是
image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8),1)
的功劳,大家可以百度一下这个OpenCV的函数