Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,28 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
title="摄像头拍照",
|
14 |
-
description="使用摄像头进行拍照"
|
15 |
-
)
|
16 |
|
17 |
-
# 启动
|
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()
|