Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- from PIL import Image, ImageOps
- #Pętla przechodzi przez wszystkie pliki z rozszerzeniem .png w pliku 'testowe'
- #Obrazy są wczytywane w skali szarości .convert('L')
- #A następnie tworzony jest negatyw - białe pismo na czarnym tle, tak jak w zbiorze uczącym, wbrew znaczeniom robi to różnicę i upewniamy się że dane maja odpowiedni wymiar i są znormalizowane
- for path in Path("testowe").rglob("*.png"):
- image = Image.open(path).convert('L')
- image = ImageOps.invert(image)
- image = image.resize((28, 28))
- image = np.array(image).reshape(1, 28, 28, 1) / 255.0
- #Wyświetlamy obraz
- plt.imshow(image.reshape(28, 28), cmap='gray')
- plt.title('Przetworzony obraz')
- plt.show()
- prediction = model.predict(image)[0]
- top3_indices = np.argsort(prediction)[-3:][::-1]
- top3_probs = prediction[top3_indices]
- print("Top 3 rozpoznane cyfry:")
- for i, (label, prob) in enumerate(zip(top3_indices, top3_probs), 1):
- print(f"{i}. Cyfra: {label}, Prawdopodobieństwo: {prob:.4f}")
- #Oraz dokoujemy prognozy i ją wyświetlamy
- prediction = model.predict(image)
- digit = np.argmax(prediction)
- print(f"Rozpoznana cyfra: {digit}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement