Spaces:
Sleeping
Sleeping
Update utils/image_processor.py
Browse files- utils/image_processor.py +11 -22
utils/image_processor.py
CHANGED
@@ -30,32 +30,21 @@ def preprocess_image(image):
|
|
30 |
|
31 |
def extract_text_from_image(image_path):
|
32 |
"""
|
33 |
-
Extract text
|
34 |
"""
|
35 |
try:
|
36 |
-
|
37 |
-
raise ValueError("TrOCR is not available.")
|
38 |
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
if image is None:
|
44 |
-
raise ValueError(f"Could not load image: {image_path}")
|
45 |
|
46 |
-
|
47 |
-
pil_image = Image.fromarray(processed_image).convert("RGB")
|
48 |
-
|
49 |
-
# TrOCR expects pixel values between 0-1
|
50 |
-
pixel_values = processor(images=pil_image, return_tensors="pt").pixel_values
|
51 |
-
|
52 |
-
# Disable gradient for inference
|
53 |
-
with torch.no_grad():
|
54 |
-
generated_ids = model.generate(pixel_values)
|
55 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
56 |
-
|
57 |
-
return generated_text.strip() or "Text extraction failed. Please enter text manually."
|
58 |
|
|
|
|
|
59 |
except Exception as e:
|
60 |
-
print(f"OCR failed: {e}")
|
61 |
-
return "Text extraction failed.
|
|
|
30 |
|
31 |
def extract_text_from_image(image_path):
|
32 |
"""
|
33 |
+
Extract handwritten text from an image using TrOCR
|
34 |
"""
|
35 |
try:
|
36 |
+
print(f"π Reading image from: {image_path}") # β
log file path
|
|
|
37 |
|
38 |
+
image = Image.open(image_path).convert("RGB")
|
39 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
40 |
|
41 |
+
generated_ids = trocr_model.generate(pixel_values)
|
42 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
|
|
|
|
43 |
|
44 |
+
print(f"π Extracted Text from {os.path.basename(image_path)}: {text}") # β
PRINT EXTRACTED TEXT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
return text or "Text extraction failed."
|
47 |
+
|
48 |
except Exception as e:
|
49 |
+
print(f"β OCR failed on {image_path}: {str(e)}")
|
50 |
+
return "Text extraction failed."
|