Advertisement
gandalfbialy

Untitled

Jun 28th, 2025
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from pathlib import Path
  2. from PIL import Image, ImageOps
  3.  
  4. #Pętla przechodzi przez wszystkie pliki z rozszerzeniem .png w pliku 'testowe'
  5. #Obrazy są wczytywane w skali szarości .convert('L')
  6. #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
  7. for path in Path("testowe").rglob("*.png"):
  8.   image = Image.open(path).convert('L')
  9.   image = ImageOps.invert(image)
  10.   image = image.resize((28, 28))
  11.   image = np.array(image).reshape(1, 28, 28, 1) / 255.0
  12.  
  13. #Wyświetlamy obraz
  14.   plt.imshow(image.reshape(28, 28), cmap='gray')
  15.   plt.title('Przetworzony obraz')
  16.   plt.show()
  17.  
  18.   prediction = model.predict(image)[0]
  19.   top3_indices = np.argsort(prediction)[-3:][::-1]
  20.   top3_probs = prediction[top3_indices]
  21.   print("Top 3 rozpoznane cyfry:")
  22.   for i, (label, prob) in enumerate(zip(top3_indices, top3_probs), 1):
  23.       print(f"{i}. Cyfra: {label}, Prawdopodobieństwo: {prob:.4f}")
  24.  
  25. #Oraz dokoujemy prognozy i ją wyświetlamy
  26.   prediction = model.predict(image)
  27.   digit = np.argmax(prediction)
  28.   print(f"Rozpoznana cyfra: {digit}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement