YanmHa commited on
Commit
7382af5
·
verified ·
1 Parent(s): e279dc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -25
app.py CHANGED
@@ -1,38 +1,49 @@
1
  import gradio as gr
2
  import os
 
 
3
 
4
- # 根目录路径
5
- BASE_IMAGE_DIR = "/data/images/images"
6
- IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff'}
 
 
7
 
8
- def count_images_recursive(base_dir=BASE_IMAGE_DIR):
9
- total_count = 0
10
- folder_counts = {}
 
 
11
 
12
- for root, _, files in os.walk(base_dir):
13
- count = 0
 
 
14
  for file in files:
15
- ext = os.path.splitext(file)[1].lower()
16
- if ext in IMAGE_EXTENSIONS:
17
- count += 1
18
- if count > 0:
19
- folder_counts[root] = count
20
- total_count += count
21
 
22
- # 构建输出字符串
23
- output = [f"图像文件总数:{total_count} 张", "\n📂 每个子目录图像数:"]
24
- for folder, count in sorted(folder_counts.items()):
25
- relative_path = os.path.relpath(folder, base_dir)
26
- output.append(f" - {relative_path}: {count} 张")
27
 
28
- return "\n".join(output)
 
 
 
29
 
30
  # Gradio 接口
31
- iface = gr.Interface(fn=count_images_recursive,
32
- inputs=[],
33
- outputs="text",
34
- title="图像目录统计器",
35
- description="统计根目录及其所有子目录中的图像文件数")
 
 
36
 
37
  if __name__ == "__main__":
38
  iface.launch()
 
1
  import gradio as gr
2
  import os
3
+ import shutil
4
+ from zipfile import ZipFile
5
 
6
+ # 根目录和目标文件名
7
+ BASE_DIR = "/data/images/images/NeuroPictor"
8
+ TARGET_FILENAMES = {"10046_0.jpg", "59194_0.jpg"}
9
+ TEMP_DIR = "/tmp/npictor_download"
10
+ ZIP_OUTPUT = "/tmp/npictor_download.zip"
11
 
12
+ def collect_target_images():
13
+ # 清理临时目录
14
+ if os.path.exists(TEMP_DIR):
15
+ shutil.rmtree(TEMP_DIR)
16
+ os.makedirs(TEMP_DIR, exist_ok=True)
17
 
18
+ found = []
19
+
20
+ # 遍历子目录查找目标文件
21
+ for root, dirs, files in os.walk(BASE_DIR):
22
  for file in files:
23
+ if file in TARGET_FILENAMES:
24
+ src_path = os.path.join(root, file)
25
+ dst_path = os.path.join(TEMP_DIR, file)
26
+ shutil.copy(src_path, dst_path)
27
+ found.append(file)
 
28
 
29
+ # 压缩成 zip 文件
30
+ with ZipFile(ZIP_OUTPUT, 'w') as zipf:
31
+ for file in os.listdir(TEMP_DIR):
32
+ zipf.write(os.path.join(TEMP_DIR, file), arcname=file)
 
33
 
34
+ if found:
35
+ return ZIP_OUTPUT
36
+ else:
37
+ return "未找到目标图像文件。请确认文件是否存在于子目录中。"
38
 
39
  # Gradio 接口
40
+ iface = gr.Interface(
41
+ fn=collect_target_images,
42
+ inputs=[],
43
+ outputs=gr.File(label="下载压缩包"),
44
+ title="NeuroPictor 图像下载器",
45
+ description="下载四个子文件夹下的 10046_0.jpg 和 59194_0.jpg 文件"
46
+ )
47
 
48
  if __name__ == "__main__":
49
  iface.launch()