yuragoithf commited on
Commit
e74b235
·
verified ·
1 Parent(s): 0fbcc15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -28
app.py CHANGED
@@ -11,35 +11,43 @@ processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-handwritten')
11
  trocr_model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten')
12
 
13
  def recognize_handwritten_text(image):
14
- # Convert Gradio image to format compatible with hezar
15
- image_np = np.array(image)
16
- processed_image = load_image(image_np)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Detect text regions with CRAFT
19
- outputs = craft_model.predict(processed_image)
20
- if not outputs or "boxes" not in outputs[0]:
21
- return Image.fromarray(processed_image), "No text detected"
22
-
23
- boxes = outputs[0]["boxes"]
24
- pil_image = Image.fromarray(processed_image)
25
- texts = []
26
-
27
- # Recognize text in each detected region
28
- for box in boxes:
29
- x_min, y_min, x_max, y_max = box[0][0], box[0][1], box[2][0], box[2][1]
30
- crop = pil_image.crop((x_min, y_min, x_max, y_max))
31
- pixel_values = processor(images=crop, return_tensors="pt").pixel_values
32
- generated_ids = trocr_model.generate(pixel_values)
33
- text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
34
- texts.append(text)
35
-
36
- # Draw boxes on the image
37
- result_image = draw_boxes(processed_image, boxes)
38
- result_pil = Image.fromarray(result_image)
39
-
40
- # Join recognized texts
41
- text_data = " ".join(texts) if texts else "No text recognized"
42
- return result_pil, f"Recognized text: {text_data}"
43
 
44
  # Create Gradio interface
45
  interface = gr.Interface(
 
11
  trocr_model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten')
12
 
13
  def recognize_handwritten_text(image):
14
+ try:
15
+ # Ensure image is a PIL image and convert to NumPy array
16
+ if not isinstance(image, Image.Image):
17
+ image = Image.fromarray(np.array(image)).convert("RGB")
18
+ image_np = np.array(image)
19
+
20
+ # Load image with hezar utils
21
+ processed_image = load_image(image_np)
22
+
23
+ # Detect text regions with CRAFT
24
+ outputs = craft_model.predict(processed_image)
25
+ if not outputs or "boxes" not in outputs[0]:
26
+ return Image.fromarray(processed_image), "No text detected"
27
+
28
+ boxes = outputs[0]["boxes"]
29
+ pil_image = Image.fromarray(processed_image)
30
+ texts = []
31
+
32
+ # Recognize text in each detected region
33
+ for box in boxes:
34
+ x_min, y_min, x_max, y_max = box[0][0], box[0][1], box[2][0], box[2][1]
35
+ crop = pil_image.crop((x_min, y_min, x_max, y_max))
36
+ pixel_values = processor(images=crop, return_tensors="pt").pixel_values
37
+ generated_ids = trocr_model.generate(pixel_values)
38
+ text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
39
+ texts.append(text)
40
+
41
+ # Draw boxes on the image
42
+ result_image = draw_boxes(processed_image, boxes)
43
+ result_pil = Image.fromarray(result_image)
44
+
45
+ # Join recognized texts
46
+ text_data = " ".join(texts) if texts else "No text recognized"
47
+ return result_pil, f"Recognized text: {text_data}"
48
 
49
+ except Exception as e:
50
+ return Image.fromarray(image_np), f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # Create Gradio interface
53
  interface = gr.Interface(