Update README.md
Browse files
README.md
CHANGED
|
@@ -20,19 +20,50 @@ pip install numpy opencv-python requests pillow transformers tensorflow
|
|
| 20 |
### Usage
|
| 21 |
|
| 22 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
import numpy as np
|
| 24 |
import cv2
|
| 25 |
import requests
|
| 26 |
from PIL import Image
|
| 27 |
from io import BytesIO
|
| 28 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
class MNISTPredictor:
|
| 31 |
def __init__(self, model_name):
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
def extract_features(self, image):
|
| 36 |
"""Extract features from the image for multiple digits."""
|
| 37 |
# Convert to grayscale
|
| 38 |
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
|
@@ -53,26 +84,22 @@ class MNISTPredictor:
|
|
| 53 |
x, y, w, h = cv2.boundingRect(contour)
|
| 54 |
roi = thresh[y:y+h, x:x+w]
|
| 55 |
resized = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
|
| 56 |
-
digit_images.append(
|
| 57 |
|
| 58 |
return digit_images
|
| 59 |
|
| 60 |
-
def predict(self, image):
|
| 61 |
"""Predict digits in the image."""
|
| 62 |
try:
|
| 63 |
digit_images = self.extract_features(image)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
outputs = self.model(**inputs)
|
| 68 |
-
predicted_class = int(np.argmax(outputs.logits))
|
| 69 |
-
predictions.append(predicted_class)
|
| 70 |
-
return predictions
|
| 71 |
except Exception as e:
|
| 72 |
print(f"Error during prediction: {e}")
|
| 73 |
return None
|
| 74 |
|
| 75 |
-
def download_image(url):
|
| 76 |
"""Download an image from a URL."""
|
| 77 |
try:
|
| 78 |
response = requests.get(url)
|
|
@@ -82,7 +109,7 @@ def download_image(url):
|
|
| 82 |
print(f"Error downloading image: {e}")
|
| 83 |
return None
|
| 84 |
|
| 85 |
-
def save_predictions_to_file(predictions, output_path):
|
| 86 |
"""Save predictions to a text file."""
|
| 87 |
try:
|
| 88 |
with open(output_path, 'w') as f:
|
|
@@ -90,7 +117,7 @@ def save_predictions_to_file(predictions, output_path):
|
|
| 90 |
except Exception as e:
|
| 91 |
print(f"Error saving predictions to file: {e}")
|
| 92 |
|
| 93 |
-
def main(image_url, model_name, output_path):
|
| 94 |
try:
|
| 95 |
predictor = MNISTPredictor(model_name)
|
| 96 |
|
|
@@ -103,11 +130,14 @@ def main(image_url, model_name, output_path):
|
|
| 103 |
|
| 104 |
# Predict digits
|
| 105 |
digits = predictor.predict(image)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
print(f"An error occurred: {e}")
|
| 113 |
|
|
|
|
| 20 |
### Usage
|
| 21 |
|
| 22 |
```python
|
| 23 |
+
import os
|
| 24 |
+
os.environ["KERAS_BACKEND"] = "tensorflow"
|
| 25 |
+
|
| 26 |
+
import keras
|
| 27 |
import numpy as np
|
| 28 |
import cv2
|
| 29 |
import requests
|
| 30 |
from PIL import Image
|
| 31 |
from io import BytesIO
|
| 32 |
+
from typing import List, Optional
|
| 33 |
+
from huggingface_hub import hf_hub_download
|
| 34 |
+
import tensorflow as tf
|
| 35 |
+
import pickle
|
| 36 |
+
|
| 37 |
+
class ImageTokenizer:
|
| 38 |
+
def __init__(self):
|
| 39 |
+
self.unique_pixels = set()
|
| 40 |
+
self.pixel_to_token = {}
|
| 41 |
+
self.token_to_pixel = {}
|
| 42 |
+
|
| 43 |
+
def fit(self, images):
|
| 44 |
+
for image in images:
|
| 45 |
+
self.unique_pixels.update(np.unique(image))
|
| 46 |
+
self.pixel_to_token = {pixel: i for i, pixel in enumerate(sorted(self.unique_pixels))}
|
| 47 |
+
self.token_to_pixel = {i: pixel for pixel, i in self.pixel_to_token.items()}
|
| 48 |
+
|
| 49 |
+
def tokenize(self, images):
|
| 50 |
+
return np.vectorize(self.pixel_to_token.get)(images)
|
| 51 |
+
|
| 52 |
+
def detokenize(self, tokens):
|
| 53 |
+
return np.vectorize(self.token_to_pixel.get)(tokens)
|
| 54 |
|
| 55 |
class MNISTPredictor:
|
| 56 |
def __init__(self, model_name):
|
| 57 |
+
# Download the model and tokenizer files
|
| 58 |
+
model_path = hf_hub_download(repo_id=model_name, filename="mnist_model.keras")
|
| 59 |
+
tokenizer_path = hf_hub_download(repo_id=model_name, filename="mnist_tokenizer.pkl")
|
| 60 |
+
|
| 61 |
+
# Load the model and tokenizer
|
| 62 |
+
self.model = keras.models.load_model(model_path)
|
| 63 |
+
with open(tokenizer_path, 'rb') as tokenizer_file:
|
| 64 |
+
self.tokenizer = pickle.load(tokenizer_file)
|
| 65 |
|
| 66 |
+
def extract_features(self, image: Image.Image) -> List[np.ndarray]:
|
| 67 |
"""Extract features from the image for multiple digits."""
|
| 68 |
# Convert to grayscale
|
| 69 |
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
|
|
|
| 84 |
x, y, w, h = cv2.boundingRect(contour)
|
| 85 |
roi = thresh[y:y+h, x:x+w]
|
| 86 |
resized = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
|
| 87 |
+
digit_images.append(resized.reshape((28, 28, 1)).astype('float32') / 255)
|
| 88 |
|
| 89 |
return digit_images
|
| 90 |
|
| 91 |
+
def predict(self, image: Image.Image) -> Optional[List[int]]:
|
| 92 |
"""Predict digits in the image."""
|
| 93 |
try:
|
| 94 |
digit_images = self.extract_features(image)
|
| 95 |
+
tokenized_images = [self.tokenizer.tokenize(img) for img in digit_images]
|
| 96 |
+
predictions = self.model.predict(np.array(tokenized_images), verbose=0)
|
| 97 |
+
return np.argmax(predictions, axis=1).tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
print(f"Error during prediction: {e}")
|
| 100 |
return None
|
| 101 |
|
| 102 |
+
def download_image(url: str) -> Optional[Image.Image]:
|
| 103 |
"""Download an image from a URL."""
|
| 104 |
try:
|
| 105 |
response = requests.get(url)
|
|
|
|
| 109 |
print(f"Error downloading image: {e}")
|
| 110 |
return None
|
| 111 |
|
| 112 |
+
def save_predictions_to_file(predictions: List[int], output_path: str) -> None:
|
| 113 |
"""Save predictions to a text file."""
|
| 114 |
try:
|
| 115 |
with open(output_path, 'w') as f:
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
print(f"Error saving predictions to file: {e}")
|
| 119 |
|
| 120 |
+
def main(image_url: str, model_name: str, output_path: str) -> None:
|
| 121 |
try:
|
| 122 |
predictor = MNISTPredictor(model_name)
|
| 123 |
|
|
|
|
| 130 |
|
| 131 |
# Predict digits
|
| 132 |
digits = predictor.predict(image)
|
| 133 |
+
if digits is not None:
|
| 134 |
+
print(f"Predicted digits are: {digits}")
|
| 135 |
+
|
| 136 |
+
# Save predictions to file
|
| 137 |
+
save_predictions_to_file(digits, output_path)
|
| 138 |
+
print(f"Predictions saved to {output_path}")
|
| 139 |
+
else:
|
| 140 |
+
print("Failed to predict digits.")
|
| 141 |
except Exception as e:
|
| 142 |
print(f"An error occurred: {e}")
|
| 143 |
|