Spaces:
Runtime error
Runtime error
Update app.py
Browse files20 unmodified lines2122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210320 unmodified lines# Gradio Appdef create_app():with gr.Blocks() as app:gr.Markdown("## Webcam Capture with Rear Camera Selection for Mobile Devices")# Camera functionality with dynamic camera listwith gr.Row():gr.HTML("""<div style="text-align: center;"><video id="video" autoplay playsinline style="width: 100%; max-width: 400px; border: 2px solid black; border-radius: 8px;"></video><br /><select id="camera-select" style="margin: 10px; padding: 8px 16px;"></select><button id="start-camera" style="margin: 10px; padding: 8px 16px;">Start Camera</button><button id="capture-image" style="margin: 10px; padding: 8px 16px;">Capture Image</button><br /><canvas id="canvas" style="display: none;"></canvas><img id="output" alt="Captured Image" style="margin-top: 20px; max-width: 100%; border: 2px solid black; border-radius: 8px;"></div><script>const video = document.getElementById("video");const canvas = document.getElementById("canvas");const output = document.getElementById("output");const startCamera = document.getElementById("start-camera");const captureImage = document.getElementById("capture-image");const cameraSelect = document.getElementById("camera-select");let stream;// Populate camera optionsasync function getCameras() {const devices = await navigator.mediaDevices.enumerateDevices();const videoDevices = devices.filter(device => device.kind === "videoinput");cameraSelect.innerHTML = "";videoDevices.forEach((device, index) => {const option = document.createElement("option");option.value = device.deviceId;option.text = device.label || `Camera ${index + 1}`;cameraSelect.appendChild(option);});}// Start the camera with the selected deviceIdstartCamera.addEventListener("click", async () => {if (stream) {stream.getTracks().forEach(track => track.stop());}const deviceId = cameraSelect.value;stream = await navigator.mediaDevices.getUserMedia({video: { deviceId: { exact: deviceId } }});video.srcObject = stream;});// Capture image from videocaptureImage.addEventListener("click", () => {canvas.width = video.videoWidth;canvas.height = video.videoHeight;const ctx = canvas.getContext("2d");ctx.drawImage(video, 0, 0, canvas.width, canvas.height);output.src = canvas.toDataURL("image/png");const capturedImageEvent = new Event("imageCaptured");output.dispatchEvent(capturedImageEvent);});// Populate cameras on loadwindow.addEventListener("load", getCameras);</script>""")# OCR Processingwith gr.Row():gr.Markdown("### Extract Text from Captured Image")image_input = gr.Image(label="Captured Image", type="pil", interactive=False)extracted_text = gr.Textbox(label="Extracted Text")# Button to trigger OCR processingwith gr.Row():process_button = gr.Button("Process Image for OCR")# OCR logicprocess_button.click(fn=extract_text,inputs=image_input,20 unmodified lines2122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910020 unmodified lines# Gradio Appdef create_app():with gr.Blocks() as app:gr.Markdown("## Webcam Capture with Front and Back Camera Support")# HTML and JavaScript for handling camera inputgr.HTML("""<div style="text-align: center;"><video id="video" autoplay playsinline style="width: 100%; max-width: 400px; border: 2px solid black; border-radius: 8px;"></video><br /><select id="camera-select" style="margin: 10px; padding: 8px 16px;"></select><button id="start-camera" style="margin: 10px; padding: 8px 16px;">Start Camera</button><button id="capture-image" style="margin: 10px; padding: 8px 16px;">Capture Image</button><br /><canvas id="canvas" style="display: none;"></canvas><img id="output" alt="Captured Image" style="margin-top: 20px; max-width: 100%; border: 2px solid black; border-radius: 8px;"></div><script>const video = document.getElementById("video");const canvas = document.getElementById("canvas");const output = document.getElementById("output");const startCamera = document.getElementById("start-camera");const captureImage = document.getElementById("capture-image");const cameraSelect = document.getElementById("camera-select");let stream;// Populate camera optionsasync function getCameras() {const devices = await navigator.mediaDevices.enumerateDevices();const videoDevices = devices.filter(device => device.kind === "videoinput");cameraSelect.innerHTML = "";videoDevices.forEach((device, index) => {const option = document.createElement("option");option.value = device.deviceId;option.text = device.label || `Camera ${index + 1}`;cameraSelect.appendChild(option);});}// Start the selected camerastartCamera.addEventListener("click", async () => {if (stream) {stream.getTracks().forEach(track => track.stop());}const deviceId = cameraSelect.value;stream = await navigator.mediaDevices.getUserMedia({video: { deviceId: { exact: deviceId } }});video.srcObject = stream;});// Capture image from videocaptureImage.addEventListener("click", () => {canvas.width = video.videoWidth;canvas.height = video.videoHeight;const ctx = canvas.getContext("2d");ctx.drawImage(video, 0, 0, canvas.width, canvas.height);output.src = canvas.toDataURL("image/png");const event = new Event("change");output.dispatchEvent(event);});// Populate cameras on loadwindow.addEventListener("load", getCameras);</script>""")# Gradio components for captured image and OCR processingwith gr.Row():image_input = gr.Image(label="Captured Image", type="pil", interactive=False)extracted_text = gr.Textbox(label="Extracted Text")# Button to process OCRprocess_button = gr.Button("Process Image for OCR")# OCR processing logicprocess_button.click(fn=extract_text,inputs=image_input,
app.py+61-64