V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
toyst
V2EX  ›  Python

新人请教一下怎么添加判断

  •  
  •   toyst · 2021-11-26 17:04:19 +08:00 · 2256 次点击
    这是一个创建于 854 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原代码:

    def extract_features(img_dir, model):
        try:
            cache = Cache('./tmp')
            feats = []
            names = []
            img_list = get_imgs(img_dir)
            total = len(img_list)
            cache['total'] = total
            for i, img_path in enumerate(img_list):
                try:
                    norm_feat = model.resnet50_extract_feat(img_path)
                    feats.append(norm_feat)
                    names.append(img_path.encode())
                    cache['current'] = i + 1
                    print(f"Extracting feature from image No. {i + 1} , {total} images in total")
                except Exception as e:
                    LOGGER.error(f"Error with extracting feature from image {e}")
                    continue
            return feats, names
        except Exception as e:
            LOGGER.error(f"Error with extracting feature from image {e}")
            sys.exit(1)
    

    大概位置是下面这里:

    def extract_features(img_dir, model):
        try:
            cache = Cache('./tmp')
            feats = []
            names = []
            img_list = get_imgs(img_dir)
            total = len(img_list)
            cache['total'] = total
            #check if img_dir is a image or image_path
            #if it is image, create embedding and return, else, traverse the directory
            for i, img_path in enumerate(img_list):
                #extract feature
            return feats, names
        except Exception as e:
            LOGGER.error(f"Error with extracting feature from image {e}")
            sys.exit(1)
    

    请问一下应该如何写啊? 判断传入的是图片文件名还是目录

    7 条回复    2021-12-05 13:23:45 +08:00
    corningsun
        1
    corningsun  
       2021-11-26 17:23:54 +08:00
    filetype ?
    Latin
        2
    Latin  
       2021-11-26 17:27:16 +08:00
    import os
    flag = os.path.exists(dir_name)
    from pathlib import Path
    filePath = path("/data/mydir")
    flag = True if filePath.is_dir() else False
    advancejar
        3
    advancejar  
       2021-11-26 17:29:54 +08:00
    你是刚学吗?
    str.endswith('.jpg')
    还有就是
    os.path.isdir
    这个函数最准,但是有坑,千万多测试
    deplivesb
        4
    deplivesb  
       2021-11-26 18:04:20 +08:00   ❤️ 1
    看了下楼主的发帖记录。。。。
    ChrisFreeMan
        5
    ChrisFreeMan  
       2021-11-26 18:08:24 +08:00   ❤️ 1
    @deplivesb 面向 V2EX 编程
    efaun
        6
    efaun  
       2021-11-26 19:05:09 +08:00
    @deplivesb #4
    @ChrisFreeMan #5
    哈哈哈, 要不你们建个知识星球把楼主拉进去吧
    imn1
        7
    imn1  
       2021-12-05 13:23:45 +08:00
    文件系统的路径问题,建议 os.scandir 和 pathlib 比较省事,基本上就是特殊的字符串处理
    如果要判断真实图片是需要导入内容的,例如文件头判断,就是 file signature
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2946 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:10 · PVG 22:10 · LAX 07:10 · JFK 10:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.