跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 新人请教一下怎么添加判断
未分類
24 2 月 2022

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

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

資深大佬 : toyst 41

原代码:

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) 

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

大佬有話說 (6)

  • 資深大佬 : corningsun

    filetype ?

  • 資深大佬 : Latin

    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

    你是刚学吗?
    str.endswith(‘.jpg’)
    还有就是
    os.path.isdir
    这个函数最准,但是有坑,千万多测试

  • 資深大佬 : deplivesb

    看了下主的发帖记录。。。。

  • 資深大佬 : ChrisFreeMan

    @deplivesb 面向 V2EX 编程

  • 資深大佬 : efaun

    @deplivesb #4
    @ChrisFreeMan #5
    哈哈哈, 要不你们建个知识星球把主拉进去吧 新人请教一下怎么添加判断

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具