Update README.md
Browse files
README.md
CHANGED
|
@@ -1,23 +1,33 @@
|
|
| 1 |
```python
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
| 4 |
from transformers import VisionEncoderDecoderModel, AutoTokenizer
|
| 5 |
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained('tirthadagr8/CustomOCR')
|
| 7 |
model=VisionEncoderDecoderModel.from_pretrained('tirthadagr8/CustomOCR')
|
| 8 |
import torch
|
| 9 |
from torchvision import transforms as T
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
path="
|
| 17 |
-
img=
|
| 18 |
model.eval()
|
| 19 |
with torch.no_grad():
|
| 20 |
print(tokenizer.batch_decode(model.cuda().generate(img.unsqueeze(0).cuda()),skip_special_tokens=True))
|
| 21 |
|
| 22 |
-
plt.imshow(
|
| 23 |
```
|
|
|
|
| 1 |
```python
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image, ImageOps
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
+
from torchvision import transforms as T
|
| 7 |
from transformers import VisionEncoderDecoderModel, AutoTokenizer
|
| 8 |
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained('tirthadagr8/CustomOCR')
|
| 10 |
model=VisionEncoderDecoderModel.from_pretrained('tirthadagr8/CustomOCR')
|
| 11 |
import torch
|
| 12 |
from torchvision import transforms as T
|
| 13 |
+
def resize_with_padding(image, target_size=(224, 224)):
|
| 14 |
+
# Resize to fit within target_size while preserving aspect ratio
|
| 15 |
+
image.thumbnail((target_size[0], target_size[1]))
|
| 16 |
+
delta_w = target_size[0] - image.width
|
| 17 |
+
delta_h = target_size[1] - image.height
|
| 18 |
+
padding = (delta_w//2, delta_h//2, delta_w - (delta_w//2), delta_h - (delta_h//2))
|
| 19 |
+
padded_img = ImageOps.expand(image, padding, fill="white")
|
| 20 |
+
transform = T.Compose([
|
| 21 |
+
T.ToTensor(), # Convert to tensor and scale to [0, 1]
|
| 22 |
+
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Normalize
|
| 23 |
+
])
|
| 24 |
+
return transform((padded_img))
|
| 25 |
|
| 26 |
+
path="0106.jpg"
|
| 27 |
+
img=resize_with_padding(Image.open(path))
|
| 28 |
model.eval()
|
| 29 |
with torch.no_grad():
|
| 30 |
print(tokenizer.batch_decode(model.cuda().generate(img.unsqueeze(0).cuda()),skip_special_tokens=True))
|
| 31 |
|
| 32 |
+
plt.imshow(img.permute(1,2,0).detach().cpu().numpy())
|
| 33 |
```
|