当我们使用opencv读取图片或者使用Deep learning读取数据集时,如果图片有问题则会造成错误!
如何检测一个文件夹内所有图片是否都没有错误呢?这里总结以下代码。
参考链接:https://www.computationalimaging.cn/2020/01/python-detect-whether-there-is-problem.html
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
import cv2
import os
path = 'F://chaomo/'
filelist = os.listdir(path)
count = 0
sum_len = len(filelist)
for item in filelist:
image = cv2.imread(path+item)
# print(item)
try:
cv2.imshow('img',image) # 若能show, 则表示无误;若图片有问题这里会有error
except:
print(item+''+'have problem!')
pass
count = count + 1
if count % 1000 == 0: # 显示进度
print(str(count) + '' + 'images has been detected.')
cv2.destroyAllWindows()