Update app.py
Browse files
app.py
CHANGED
@@ -16,8 +16,8 @@ model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-large-handwri
|
|
16 |
def display_sketch(sketch):
|
17 |
logging.debug(f"Received sketch data: {sketch}")
|
18 |
|
19 |
-
if isinstance(sketch,
|
20 |
-
image_data = sketch
|
21 |
logging.debug(f"Image data type: {type(image_data)}")
|
22 |
logging.debug(f"Image data shape: {np.array(image_data).shape}")
|
23 |
|
@@ -25,7 +25,7 @@ def display_sketch(sketch):
|
|
25 |
plt.axis('off')
|
26 |
|
27 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
28 |
-
plt.savefig(temp_file.name, bbox_inches='tight')
|
29 |
temp_file_path = temp_file.name
|
30 |
|
31 |
return temp_file_path
|
@@ -35,14 +35,18 @@ def display_sketch(sketch):
|
|
35 |
return error_message
|
36 |
|
37 |
def recognize_text(image_path):
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
|
47 |
with gr.Blocks() as demo:
|
48 |
sketchpad = gr.Sketchpad(label="Draw Something")
|
|
|
16 |
def display_sketch(sketch):
|
17 |
logging.debug(f"Received sketch data: {sketch}")
|
18 |
|
19 |
+
if isinstance(sketch, np.ndarray):
|
20 |
+
image_data = sketch
|
21 |
logging.debug(f"Image data type: {type(image_data)}")
|
22 |
logging.debug(f"Image data shape: {np.array(image_data).shape}")
|
23 |
|
|
|
25 |
plt.axis('off')
|
26 |
|
27 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
28 |
+
plt.savefig(temp_file.name, bbox_inches='tight', pad_inches=0)
|
29 |
temp_file_path = temp_file.name
|
30 |
|
31 |
return temp_file_path
|
|
|
35 |
return error_message
|
36 |
|
37 |
def recognize_text(image_path):
|
38 |
+
try:
|
39 |
+
# Load the image
|
40 |
+
image = Image.open(image_path).convert("RGB")
|
41 |
+
# Prepare the image for the model
|
42 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
43 |
+
# Generate the text
|
44 |
+
generated_ids = model.generate(pixel_values)
|
45 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
46 |
+
return generated_text
|
47 |
+
except Exception as e:
|
48 |
+
logging.error(f"Error in recognizing text: {e}")
|
49 |
+
return "Error in recognizing text"
|
50 |
|
51 |
with gr.Blocks() as demo:
|
52 |
sketchpad = gr.Sketchpad(label="Draw Something")
|