Siyun He commited on
Commit
215339c
·
1 Parent(s): d04b2ef

add save button

Browse files
Files changed (1) hide show
  1. app.py +31 -12
app.py CHANGED
@@ -3,6 +3,7 @@ import cvzone
3
  import numpy as np
4
  import os
5
  import gradio as gr
 
6
 
7
  # Load the YuNet model
8
  model_path = 'face_detection_yunet_2023mar.onnx'
@@ -118,6 +119,31 @@ def transform_cv2(frame, transform):
118
  else:
119
  return frame
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
 
123
  # Gradio webcam input
@@ -134,21 +160,14 @@ with gr.Blocks() as demo:
134
  value="none", label="Transformation")
135
  input_img = gr.Image(sources=["webcam"], type="numpy", streaming=True)
136
  next_button = gr.Button("Next Glasses")
 
137
  input_img.stream(webcam_input, [input_img, transform], [input_img], time_limit=30, stream_every=0.1)
138
-
139
- next_button.click(change_glasses, [], [])
 
 
140
  if __name__ == "__main__":
141
  demo.launch(share=True)
142
 
143
 
144
 
145
- # # Gradio Interface
146
- # with gr.Blocks() as demo:
147
- # with gr.Row():
148
- # with gr.Column():
149
- # input_img = gr.Image(label="Input", sources="webcam", streaming=True)
150
- # next_button = gr.Button("Next Glasses")
151
- # input_img.stream(webcam_input, [input_img], [input_img], stream_every=0.1, concurrency_limit=30)
152
- # next_button.click(change_glasses, [], [])
153
- # if __name__ == "__main__":
154
- # demo.launch(share=True)
 
3
  import numpy as np
4
  import os
5
  import gradio as gr
6
+ from datetime import datetime
7
 
8
  # Load the YuNet model
9
  model_path = 'face_detection_yunet_2023mar.onnx'
 
119
  else:
120
  return frame
121
 
122
+ def refresh_interface():
123
+ # # Reset the transformation dropdown to its default value
124
+ # transform.update(value="none")
125
+
126
+ # Reset the image to an empty state or a default image
127
+ input_img.update(value=None)
128
+
129
+ # Return a message indicating the interface has been refreshed
130
+ return "Interface refreshed!"
131
+
132
+ def save_frame(frame):
133
+ # Convert frame to RGB
134
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
135
+
136
+ # Create a unique filename using the current timestamp
137
+ filename = f"saved_frame_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
138
+
139
+ # Save the frame
140
+ cv2.imwrite(filename, frame)
141
+
142
+ # Refresh the interfaceq
143
+ refresh_interface()
144
+
145
+ return f"Frame saved as '{filename}'"
146
+
147
 
148
 
149
  # Gradio webcam input
 
160
  value="none", label="Transformation")
161
  input_img = gr.Image(sources=["webcam"], type="numpy", streaming=True)
162
  next_button = gr.Button("Next Glasses")
163
+ save_button = gr.Button("Save as a Picture")
164
  input_img.stream(webcam_input, [input_img, transform], [input_img], time_limit=30, stream_every=0.1)
165
+ with gr.Row():
166
+ next_button.click(change_glasses, [], [])
167
+ with gr.Row():
168
+ save_button.click(save_frame, [input_img], [])
169
  if __name__ == "__main__":
170
  demo.launch(share=True)
171
 
172
 
173