dal4933 commited on
Commit
38bfe3c
·
verified ·
1 Parent(s): ff3b583

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -19,11 +19,6 @@ def scan_barcode(image):
19
  image_bytes = base64.b64decode(image_data)
20
  pil_image = Image.open(io.BytesIO(image_bytes))
21
  image = np.array(pil_image)
22
- else:
23
- # Direct base64 without prefix
24
- image_bytes = base64.b64decode(image)
25
- pil_image = Image.open(io.BytesIO(image_bytes))
26
- image = np.array(pil_image)
27
 
28
  # Ensure image is numpy array
29
  if not isinstance(image, np.ndarray):
@@ -51,28 +46,30 @@ def scan_barcode(image):
51
  except Exception as e:
52
  return {"status": "error", "message": f"Processing error: {str(e)}"}
53
 
54
- # Create Gradio interface
55
- with gr.Blocks() as iface:
 
 
56
  gr.Markdown("# 📦 Barcode Scanner API")
57
- gr.Markdown("Upload an image to scan barcode")
58
 
59
- with gr.Row():
60
- image_input = gr.Image(label="Barcode Image", type="numpy")
61
- json_output = gr.JSON(label="Scan Result")
 
62
 
63
- # Create the API endpoint
64
- image_input.change(
65
  fn=scan_barcode,
66
  inputs=image_input,
67
- outputs=json_output,
68
  api_name="scan"
69
  )
70
 
71
  # Launch the application
72
  if __name__ == "__main__":
73
- iface.launch(
74
  server_name="0.0.0.0",
75
  server_port=7860,
76
  show_error=True,
77
- share=False # Set to True if you want public URL
78
  )
 
19
  image_bytes = base64.b64decode(image_data)
20
  pil_image = Image.open(io.BytesIO(image_bytes))
21
  image = np.array(pil_image)
 
 
 
 
 
22
 
23
  # Ensure image is numpy array
24
  if not isinstance(image, np.ndarray):
 
46
  except Exception as e:
47
  return {"status": "error", "message": f"Processing error: {str(e)}"}
48
 
49
+ # Create Gradio app using Blocks for better API control
50
+ app = gr.Blocks()
51
+
52
+ with app:
53
  gr.Markdown("# 📦 Barcode Scanner API")
 
54
 
55
+ # Define the interface components
56
+ image_input = gr.Image(label="Upload Image", type="numpy")
57
+ scan_button = gr.Button("Scan Barcode", variant="primary")
58
+ result_output = gr.JSON(label="Scan Result")
59
 
60
+ # Connect the function
61
+ scan_button.click(
62
  fn=scan_barcode,
63
  inputs=image_input,
64
+ outputs=result_output,
65
  api_name="scan"
66
  )
67
 
68
  # Launch the application
69
  if __name__ == "__main__":
70
+ app.launch(
71
  server_name="0.0.0.0",
72
  server_port=7860,
73
  show_error=True,
74
+ share=False
75
  )