TzuShianYang commited on
Commit
335b410
·
1 Parent(s): 9bab5aa

Add zip and download file

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +50 -3
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ *.zip
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
- import numpy as np
3
  import zipfile
 
4
 
5
 
6
- def add_image(camera: gr.Image, gallery: gr.Gallery):
7
  if gallery:
8
  images = gallery
9
  images.append(camera)
@@ -12,6 +12,26 @@ def add_image(camera: gr.Image, gallery: gr.Gallery):
12
  return [camera]
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  with gr.Blocks(fill_height=True) as demo:
16
  with gr.Row():
17
  with gr.Column():
@@ -20,14 +40,41 @@ with gr.Blocks(fill_height=True) as demo:
20
  catch_btn = gr.Button("Capture", scale=0)
21
  with gr.Column():
22
  preview_img = gr.Image(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  with gr.Row():
24
  with gr.Column():
25
  gallery = gr.Gallery(
26
  label="Collected images", show_label=False, elem_id="gallery", columns=[10], rows=[4], object_fit="contain", height="auto")
27
- download_btn = gr.Button("Download", scale=0)
 
28
 
29
  catch_btn.click(add_image, [preview_img, gallery], [gallery])
30
  camera.stream(lambda s: s, camera, preview_img)
 
 
31
 
32
  if __name__ == "__main__":
33
  demo.launch()
 
1
  import gradio as gr
 
2
  import zipfile
3
+ import os
4
 
5
 
6
+ def add_image(camera, gallery):
7
  if gallery:
8
  images = gallery
9
  images.append(camera)
 
12
  return [camera]
13
 
14
 
15
+ def zip_gallery(gallery):
16
+ count = 0
17
+ if not gallery:
18
+ return gr.DownloadButton("Download", visible=False)
19
+ if os.path.isfile("image_data.zip"):
20
+ os.remove("image_data.zip")
21
+ zf = zipfile.ZipFile("image_data.zip", mode="w")
22
+ try:
23
+ for path, _ in gallery:
24
+ zf.write(path, f'image_{count}.png',
25
+ compress_type=zipfile.ZIP_DEFLATED)
26
+ count = count + 1
27
+ except FileNotFoundError:
28
+ print("Error Occurred!")
29
+ finally:
30
+ zf.close()
31
+
32
+ return gr.DownloadButton("Download", value="image_data.zip", visible=True)
33
+
34
+
35
  with gr.Blocks(fill_height=True) as demo:
36
  with gr.Row():
37
  with gr.Column():
 
40
  catch_btn = gr.Button("Capture", scale=0)
41
  with gr.Column():
42
  preview_img = gr.Image(visible=False)
43
+ gr.Markdown(
44
+ """
45
+ # How to Use
46
+
47
+ 1. Open Camera on left side, then click "Record" button on the camera screen
48
+
49
+ 2. Click "Capture" button to capture image to gallery
50
+
51
+ 3. After capture all image, click "Zip file" button to zip images
52
+
53
+ 4. Download zip file by clicking "Download" button showed below
54
+
55
+ # 如何使用
56
+
57
+ 1. 點擊左側開啟攝影機,並按下"Record"按鈕讓攝影機能夠進行擷取
58
+
59
+ 2. 點擊"Capture"按鈕,影像就會被擷取到下方的欄位中
60
+
61
+ 3. 擷取影像完畢後,點擊下方的"Zip file"按鈕將影像打包並壓縮
62
+
63
+ 4. 按下隨後出現的"Download"按鈕就能下載打包好的壓縮檔
64
+
65
+ """
66
+ )
67
  with gr.Row():
68
  with gr.Column():
69
  gallery = gr.Gallery(
70
  label="Collected images", show_label=False, elem_id="gallery", columns=[10], rows=[4], object_fit="contain", height="auto")
71
+ zip_btn = gr.Button("Zip file")
72
+ download_btn = gr.DownloadButton("Download", visible=False)
73
 
74
  catch_btn.click(add_image, [preview_img, gallery], [gallery])
75
  camera.stream(lambda s: s, camera, preview_img)
76
+ zip_btn.click(zip_gallery, gallery, download_btn)
77
+
78
 
79
  if __name__ == "__main__":
80
  demo.launch()