Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
return False
|
| 9 |
-
cap.release()
|
| 10 |
-
return True
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
def capture_image():
|
| 14 |
-
cap = cv2.VideoCapture(0)
|
| 15 |
-
|
| 16 |
-
# 检查摄像头是否可以打开
|
| 17 |
-
if not cap.isOpened():
|
| 18 |
-
return "无法打开摄像头,检查摄像头权限或设备状态。"
|
| 19 |
-
|
| 20 |
-
ret, frame = cap.read()
|
| 21 |
-
cap.release()
|
| 22 |
-
|
| 23 |
-
if not ret:
|
| 24 |
-
return "无法读取摄像头内容,可能是权限不足或设备问题。"
|
| 25 |
-
|
| 26 |
-
# 将图像从BGR转换为RGB
|
| 27 |
-
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 28 |
-
|
| 29 |
-
return frame_rgb
|
| 30 |
-
|
| 31 |
-
# Gradio界面:先检测摄像头是否可用
|
| 32 |
-
def run_camera():
|
| 33 |
-
if check_camera():
|
| 34 |
-
return capture_image()
|
| 35 |
-
else:
|
| 36 |
-
return "摄像头不可用,可能是权限问题或设备连接问题。"
|
| 37 |
-
|
| 38 |
-
# 使用 Gradio 创建界面
|
| 39 |
demo = gr.Interface(
|
| 40 |
-
fn=
|
| 41 |
-
inputs=
|
| 42 |
-
outputs="image",
|
| 43 |
-
live=True,
|
| 44 |
title="摄像头拍照",
|
| 45 |
-
description="
|
| 46 |
)
|
| 47 |
|
| 48 |
# 启动应用
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
# 定义一个处理拍摄图像的函数
|
| 4 |
+
def process_image(image):
|
| 5 |
+
# 这里的 image 已经是拍到的照片
|
| 6 |
+
return image # 你可以在这里进行进一步处理
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# 使用 Gradio 的 Webcam 组件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
demo = gr.Interface(
|
| 10 |
+
fn=process_image, # 处理图像的函数
|
| 11 |
+
inputs=gr.Image(source="webcam", tool="editor"), # 调用摄像头
|
| 12 |
+
outputs="image", # 输出处理后的图像
|
|
|
|
| 13 |
title="摄像头拍照",
|
| 14 |
+
description="使用浏览器摄像头拍照"
|
| 15 |
)
|
| 16 |
|
| 17 |
# 启动应用
|