詳解Python如何批量檢查圖像是否可用
數(shù)據(jù)集中的圖像,一般不可用在以下3個(gè)方面:
1.圖像過(guò)小
2.無(wú)法打開
3.“Premature end of JPEG file”
這些圖像可能會(huì)導(dǎo)致模型的學(xué)習(xí)異常,因此,使用多進(jìn)程檢查數(shù)據(jù)集中的每張圖像,是很有必要的。
具體邏輯如下:
- 遍歷文件夾,多進(jìn)程處理每一張圖像
- 判斷圖像是否可讀,是否支持resize尺寸,邊長(zhǎng)是否滿足
- 判斷JPG圖像是否Premature end
- 刪除錯(cuò)誤圖像
腳本如下:
#!/usr/bin/env python # -- coding: utf-8 -- """ Copyright (c) 2020. All rights reserved. Created by C. L. Wang on 10.11.20 """ import argparse import os from multiprocessing import Pool import cv2 def traverse_dir_files(root_dir, ext=None): """ 列出文件夾中的文件, 深度遍歷 :param root_dir: 根目錄 :param ext: 后綴名 :return: [文件路徑列表, 文件名稱列表] """ names_list = [] paths_list = [] for parent, _, fileNames in os.walk(root_dir): for name in fileNames: if name.startswith('.'): # 去除隱藏文件 continue if ext: # 根據(jù)后綴名搜索 if name.endswith(tuple(ext)): names_list.append(name) paths_list.append(os.path.join(parent, name)) else: names_list.append(name) paths_list.append(os.path.join(parent, name)) return paths_list, names_list def check_img(path, size): """ 檢查圖像 """ is_good = True try: img_bgr = cv2.imread(path) h, w, _ = img_bgr.shape if h < size or w < size: is_good = False _ = cv2.resize(img_bgr, (size, size)) except Exception as e: is_good = False if path.endswith("jpg"): with open(path, 'rb') as f: check_chars = f.read()[-2:] if check_chars != b'\xff\xd9': print('[Info] Not complete jpg image') is_good = False if not is_good: print('[Info] error path: {}'.format(path)) os.remove(path) def check_error(img_dir, n_prc, size): """ 檢查錯(cuò)誤圖像的數(shù)量 """ print('[Info] 處理文件夾路徑: {}'.format(img_dir)) paths_list, names_list = traverse_dir_files(img_dir) print('[Info] 數(shù)據(jù)總量: {}'.format(len(paths_list))) pool = Pool(processes=n_prc) # 多線程下載 for idx, path in enumerate(paths_list): pool.apply_async(check_img, (path, size)) if (idx+1) % 1000 == 0: print('[Info] idx: {}'.format(idx+1)) pool.close() pool.join() print('[Info] 數(shù)據(jù)處理完成: {}'.format(img_dir)) def parse_args(): """ 處理腳本參數(shù),支持相對(duì)路徑 :return: in_folder 輸入文件夾, size 尺寸, n_prc 進(jìn)程數(shù) """ parser = argparse.ArgumentParser(description='檢查圖片腳本') parser.add_argument('-i', dest='in_folder', required=True, help='輸入文件夾', type=str) parser.add_argument('-p', dest='n_prc', required=False, default=100, help='進(jìn)程數(shù)', type=str) parser.add_argument('-s', dest='size', required=False, default=50, help='最小邊長(zhǎng)', type=str) args = parser.parse_args() in_folder = args.in_folder size = int(args.size) n_prc = int(args.n_prc) print("[Info] 文件路徑:{}".format(in_folder)) print("[Info] 進(jìn)程數(shù): {}".format(n_prc)) print("[Info] 邊長(zhǎng): {}".format(size)) return in_folder, n_prc, size def main(): arg_in, n_prc, size = parse_args() check_error(arg_in, n_prc, size) if __name__ == '__main__': main()
到此這篇關(guān)于詳解Python如何批量檢查圖像是否可用的文章就介紹到這了,更多相關(guān)Python檢查圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Python實(shí)現(xiàn)批量讀取大量nc格式文件并導(dǎo)出全部時(shí)間信息
這篇文章主要為大家詳細(xì)介紹了如何基于Python語(yǔ)言,逐一讀取大量.nc格式的多時(shí)相柵格文件并導(dǎo)出其中所具有的全部時(shí)間信息的方法,需要的可以參考下2024-01-01python人工智能tensorflow常見(jiàn)損失函數(shù)LOSS匯總
這篇文章主要為大家介紹了python人工智能tensorflowf常見(jiàn)損失函數(shù)LOSS匯總,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python操作Access數(shù)據(jù)庫(kù)基本步驟分析
這篇文章主要介紹了Python操作Access數(shù)據(jù)庫(kù)基本步驟,結(jié)合實(shí)例形式詳細(xì)分析了Python針對(duì)access操作的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-09-09Python Pandas 箱線圖的實(shí)現(xiàn)
這篇文章主要介紹了Python Pandas 箱線圖的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python使用tkinter實(shí)現(xiàn)小時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了Python使用tkinter實(shí)現(xiàn)小時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02Python小程序編程實(shí)現(xiàn)一鍵自動(dòng)整理文件解壓文件
這篇文章主要為大家介紹了Python小程序編程實(shí)現(xiàn)一鍵自動(dòng)整理文件解壓文件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Python的多種對(duì)象工廠模式方便代碼維護(hù)擴(kuò)展
這篇文章主要為大家介紹了Python的多種對(duì)象工廠模式更方便我們進(jìn)行代碼維護(hù)擴(kuò)展,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01python實(shí)現(xiàn)銀行管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)銀行管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10