Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from PIL import Image | |
| import cv2 | |
| def capture_image(): | |
| # 打开摄像头 | |
| cap = cv2.VideoCapture(0) | |
| ret, frame = cap.read() | |
| if ret: | |
| # 将BGR图像转换为RGB图像 | |
| frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| # 转换为Pillow图像 | |
| img = Image.fromarray(frame) | |
| cap.release() | |
| return img | |
| else: | |
| cap.release() | |
| return "无法打开摄像头" | |
| # 创建gradio接口 | |
| with gr.Blocks() as demo: | |
| webcam_image = gr.Image(label="摄像头拍摄的图片") | |
| capture_button = gr.Button("拍照") | |
| capture_button.click(capture_image, outputs=webcam_image) | |
| # 启动gradio界面 | |
| demo.launch() | |