dgith commited on
Commit
40bdeee
·
verified ·
1 Parent(s): 2b9d286

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -74,7 +74,7 @@ def preprocess_image(image):
74
  if hasattr(image, 'convert'):
75
  image = np.array(image.convert('L'))
76
 
77
- # Ensure it's a grayscale image of shape (28, 28)
78
  if len(image.shape) == 3:
79
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
80
  elif len(image.shape) == 2:
@@ -86,7 +86,8 @@ def preprocess_image(image):
86
  # Resize to 28x28
87
  resized = cv2.resize(gray, (28, 28), interpolation=cv2.INTER_AREA)
88
 
89
- # Invert if background is white (MNIST digits are white on black)
 
90
  if resized.mean() > 128:
91
  resized = 255 - resized
92
 
@@ -126,9 +127,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
126
  sketchpad = gr.ImageEditor(
127
  label="Draw Digit",
128
  image_mode="L",
129
- canvas_size=(200, 200),
130
- brush=gr.Brush(default_size=20),
131
- sources=["upload"]
 
 
 
 
 
 
 
 
 
132
  )
133
  upload_btn = gr.UploadButton("📁 Upload Image", file_types=["image"])
134
  clear_btn = gr.Button("🧹 Clear Canvas")
@@ -148,4 +158,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
148
  upload_btn.upload(predict_digit, inputs=upload_btn, outputs=[output_label, output_bars])
149
  clear_btn.click(lambda: None, inputs=None, outputs=sketchpad)
150
 
151
- demo.launch(debug=True)
 
74
  if hasattr(image, 'convert'):
75
  image = np.array(image.convert('L'))
76
 
77
+ # Ensure it's a grayscale image
78
  if len(image.shape) == 3:
79
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
80
  elif len(image.shape) == 2:
 
86
  # Resize to 28x28
87
  resized = cv2.resize(gray, (28, 28), interpolation=cv2.INTER_AREA)
88
 
89
+ # Invert: black strokes on white background -> white on black for MNIST
90
+ # Check if background is bright (white canvas with black pen)
91
  if resized.mean() > 128:
92
  resized = 255 - resized
93
 
 
127
  sketchpad = gr.ImageEditor(
128
  label="Draw Digit",
129
  image_mode="L",
130
+ canvas_size=(280, 280),
131
+ brush=gr.Brush(
132
+ default_size=15,
133
+ colors=["#000000"],
134
+ default_color="#000000",
135
+ color_mode="fixed"
136
+ ),
137
+ eraser=gr.Eraser(default_size=15),
138
+ sources=["upload"],
139
+ type="numpy",
140
+ layers=False,
141
+ transforms=[]
142
  )
143
  upload_btn = gr.UploadButton("📁 Upload Image", file_types=["image"])
144
  clear_btn = gr.Button("🧹 Clear Canvas")
 
158
  upload_btn.upload(predict_digit, inputs=upload_btn, outputs=[output_label, output_bars])
159
  clear_btn.click(lambda: None, inputs=None, outputs=sketchpad)
160
 
161
+ demo.launch(debug=True)