2020年1月5日星期日

Python: detect whether there is a problem with the pictures in a specified directory

When we use opencv to read pictures or use deep learning to read data sets, if there are problems with pictures, it will cause errors!

How to detect if all pictures in a folder are correct? The following code is summarized here.




#! /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)    # If it can be shown, it means no error; if there is a problem with the picture, there will be an error
    except:
        print(item+''+'have problem!')
        pass
    count = count + 1
    if count % 1000 == 0:    # Show progress
        print(str(count) + '' + 'images has been detected.')
cv2.destroyAllWindows()

没有评论:

发表评论