Spaces:
Sleeping
Sleeping
Download legacy/e4.py from ansarzeinulla/9OCR: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/ansarzeinulla/9OCR/resolve/e5800bc28e49e54d6a0a6c50c2fa7269d61c8c8c/legacy/e4.py
- Command line
-
hf download hf://spaces/ansarzeinulla/9OCR@e5800bc28e49e54d6a0a6c50c2fa7269d61c8c8c/legacy/e4.py
-
curl -L -o e4.py https://huggingface.co/spaces/ansarzeinulla/9OCR/resolve/e5800bc28e49e54d6a0a6c50c2fa7269d61c8c8c/legacy/e4.py
1.06 kB
| from fastai.vision.all import load_learner, PILImage | |
| from PIL import ImageOps | |
| # 1. Load the model | |
| print("Loading model...") | |
| learn = load_learner('handwriting_classifier_best.pkl') | |
| # 2. Open the image and FIX the iPhone rotation bug! | |
| raw_img = PILImage.create('IMG_1521 (4) (1).jpg') | |
| # FastAI's PILImage.create actually attempts to transpose EXIF under the hood, | |
| # but let's double check by physically saving what the model sees: | |
| raw_img.save("what_the_model_actually_sees.png") | |
| print("Saved 'what_the_model_actually_sees.png'. Please open this file and check if it is upright!") | |
| # 3. Predict using FastAI's official pipeline | |
| pred, pred_idx, probs = learn.predict(raw_img) | |
| print(f"\n--- Prediction ---") | |
| print(f"Predicted Class: {pred}") | |
| print(f"Confidence: {probs[pred_idx]*100:.2f}%") | |
| # Show top 3 | |
| class_probabilities = dict(zip(learn.dls.vocab, probs.tolist())) | |
| sorted_guesses = sorted(class_probabilities.items(), key=lambda x: x[1], reverse=True) | |
| print("\nTop 3 Guesses:") | |
| for cls, prob in sorted_guesses[:3]: | |
| print(f"Class {cls}: {prob * 100:.2f}%") |