Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| # 定义一个打开摄像头并拍照的函数 | |
| def capture_image(): | |
| cap = cv2.VideoCapture(0) | |
| if not cap.isOpened(): | |
| return "无法打开摄像头" | |
| ret, frame = cap.read() | |
| cap.release() | |
| if not ret: | |
| return "无法读取摄像头内容" | |
| # 将图像从BGR转换为RGB | |
| frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| return frame_rgb | |
| # 使用 Gradio 创建界面 | |
| demo = gr.Interface( | |
| fn=capture_image, | |
| inputs=None, | |
| outputs="image", | |
| live=True, | |
| title="摄像头拍照", | |
| description="点击按钮打开摄像头并拍照" | |
| ) | |
| # 启动应用 | |
| demo.launch() | |