gopichandra commited on
Commit
0716eb3
·
verified ·
1 Parent(s): 32393e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -24
app.py CHANGED
@@ -21,17 +21,20 @@ def extract_text(image):
21
  # Gradio App
22
  def create_app():
23
  with gr.Blocks() as app:
24
- gr.Markdown("## Webcam Image Capture with Rear Camera Selection and OCR")
25
 
26
- # Video feed for capturing
27
  with gr.Row():
28
  gr.HTML("""
29
  <div style="text-align: center;">
30
  <video id="video" autoplay playsinline style="width: 100%; max-width: 400px; border: 2px solid black; border-radius: 8px;"></video>
31
  <br />
 
 
 
 
32
  <button id="start-camera" style="margin: 10px; padding: 8px 16px;">Start Camera</button>
33
  <button id="capture-image" style="margin: 10px; padding: 8px 16px;">Capture Image</button>
34
- <select id="camera-select" style="margin: 10px; padding: 8px 16px;"></select>
35
  <br />
36
  <canvas id="canvas" style="display: none;"></canvas>
37
  <img id="output" alt="Captured Image" style="margin-top: 20px; max-width: 100%; border: 2px solid black; border-radius: 8px;">
@@ -43,48 +46,34 @@ def create_app():
43
  const startCamera = document.getElementById("start-camera");
44
  const captureImage = document.getElementById("capture-image");
45
  const cameraSelect = document.getElementById("camera-select");
46
-
47
- let stream;
48
 
49
- // Populate camera options
50
- async function getCameras() {
51
- const devices = await navigator.mediaDevices.enumerateDevices();
52
- const videoDevices = devices.filter(device => device.kind === "videoinput");
53
- cameraSelect.innerHTML = "";
54
- videoDevices.forEach((device, index) => {
55
- const option = document.createElement("option");
56
- option.value = device.deviceId;
57
- option.text = device.label || `Camera ${index + 1}`;
58
- cameraSelect.appendChild(option);
59
- });
60
- }
61
 
62
- // Start camera
63
  startCamera.addEventListener("click", async () => {
64
  if (stream) {
65
  stream.getTracks().forEach(track => track.stop());
66
  }
67
  stream = await navigator.mediaDevices.getUserMedia({
68
- video: { deviceId: { exact: cameraSelect.value } }
69
  });
70
  video.srcObject = stream;
71
  });
72
 
73
- // Capture image
74
  captureImage.addEventListener("click", () => {
75
  canvas.width = video.videoWidth;
76
  canvas.height = video.videoHeight;
77
  const ctx = canvas.getContext("2d");
78
  ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
79
  output.src = canvas.toDataURL("image/png");
 
 
80
  });
81
-
82
- // Populate cameras on load
83
- window.addEventListener("load", getCameras);
84
  </script>
85
  """)
86
 
87
- # Gradio components for OCR
88
  with gr.Row():
89
  gr.Markdown("### Extract Text from Captured Image")
90
  image_input = gr.Image(label="Captured Image", type="pil", interactive=False)
 
21
  # Gradio App
22
  def create_app():
23
  with gr.Blocks() as app:
24
+ gr.Markdown("## Webcam Capture with Front/Rear Camera Toggle and OCR")
25
 
26
+ # Camera functionality with front/rear toggle
27
  with gr.Row():
28
  gr.HTML("""
29
  <div style="text-align: center;">
30
  <video id="video" autoplay playsinline style="width: 100%; max-width: 400px; border: 2px solid black; border-radius: 8px;"></video>
31
  <br />
32
+ <select id="camera-select" style="margin: 10px; padding: 8px 16px;">
33
+ <option value="user">Front Camera</option>
34
+ <option value="environment">Rear Camera</option>
35
+ </select>
36
  <button id="start-camera" style="margin: 10px; padding: 8px 16px;">Start Camera</button>
37
  <button id="capture-image" style="margin: 10px; padding: 8px 16px;">Capture Image</button>
 
38
  <br />
39
  <canvas id="canvas" style="display: none;"></canvas>
40
  <img id="output" alt="Captured Image" style="margin-top: 20px; max-width: 100%; border: 2px solid black; border-radius: 8px;">
 
46
  const startCamera = document.getElementById("start-camera");
47
  const captureImage = document.getElementById("capture-image");
48
  const cameraSelect = document.getElementById("camera-select");
 
 
49
 
50
+ let stream;
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ // Start the camera with the selected mode
53
  startCamera.addEventListener("click", async () => {
54
  if (stream) {
55
  stream.getTracks().forEach(track => track.stop());
56
  }
57
  stream = await navigator.mediaDevices.getUserMedia({
58
+ video: { facingMode: cameraSelect.value }
59
  });
60
  video.srcObject = stream;
61
  });
62
 
63
+ // Capture image from video
64
  captureImage.addEventListener("click", () => {
65
  canvas.width = video.videoWidth;
66
  canvas.height = video.videoHeight;
67
  const ctx = canvas.getContext("2d");
68
  ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
69
  output.src = canvas.toDataURL("image/png");
70
+ const capturedImageEvent = new Event("imageCaptured");
71
+ output.dispatchEvent(capturedImageEvent);
72
  });
 
 
 
73
  </script>
74
  """)
75
 
76
+ # OCR Processing
77
  with gr.Row():
78
  gr.Markdown("### Extract Text from Captured Image")
79
  image_input = gr.Image(label="Captured Image", type="pil", interactive=False)