ake178178 commited on
Commit
9d95991
·
verified ·
1 Parent(s): e675cc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -1,18 +1,28 @@
1
  import gradio as gr
 
 
2
 
3
- # 定义一个简单的拍照函数
4
- def capture_image(image):
5
- return image
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # 使用 Gradio 创建一个摄像头界面
8
- demo = gr.Interface(
9
- fn=capture_image,
10
- inputs=gr.Image(source="webcam", tool="editor"),
11
- outputs="image",
12
- live=True,
13
- title="摄像头拍照",
14
- description="使用摄像头进行拍照"
15
- )
16
 
17
- # 启动 Gradio 应用
18
  demo.launch()
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ import cv2
4
 
5
+ def capture_image():
6
+ # 打开摄像头
7
+ cap = cv2.VideoCapture(0)
8
+ ret, frame = cap.read()
9
+ if ret:
10
+ # 将BGR图像转换为RGB图像
11
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
12
+ # 转换为Pillow图像
13
+ img = Image.fromarray(frame)
14
+ cap.release()
15
+ return img
16
+ else:
17
+ cap.release()
18
+ return "无法打开摄像头"
19
 
20
+ # 创建gradio接口
21
+ with gr.Blocks() as demo:
22
+ webcam_image = gr.Image(label="摄像头拍摄的图片")
23
+ capture_button = gr.Button("拍照")
24
+
25
+ capture_button.click(capture_image, outputs=webcam_image)
 
 
 
26
 
27
+ # 启动gradio界面
28
  demo.launch()