index.html
· 1.3 KiB · HTML
Raw
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" c initial-scale=1.0">
<title>Image Classifier</title>
<style>
#preview {
max-width: 500px;
max-height: 500px;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Wczytaj obraz do klasyfikacji</h1>
<form acti method="POST" enctype="multipart/form-data">
<input type="file" name="image" id="imageInput" accept="image/*" required>
<br>
<img id="preview" src="#" alt="Image Preview" style="display: none;">
<br>
<button type="submit">Klasyfikuj</button>
</form>
<script>
const imageInput = document.getElementById('imageInput');
const preview = document.getElementById('preview');
imageInput.addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
preview.src = e.target.result;
preview.style.display = 'block';
};
reader.readAsDataURL(file);
} else {
preview.style.display = 'none';
}
});
</script>
</body>
</html>
| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="UTF-8"> |
| 5 | <meta name="viewport" c initial-scale=1.0"> |
| 6 | <title>Image Classifier</title> |
| 7 | <style> |
| 8 | #preview { |
| 9 | max-width: 500px; |
| 10 | max-height: 500px; |
| 11 | margin-top: 20px; |
| 12 | } |
| 13 | </style> |
| 14 | </head> |
| 15 | <body> |
| 16 | <h1>Wczytaj obraz do klasyfikacji</h1> |
| 17 | <form acti method="POST" enctype="multipart/form-data"> |
| 18 | <input type="file" name="image" id="imageInput" accept="image/*" required> |
| 19 | <br> |
| 20 | <img id="preview" src="#" alt="Image Preview" style="display: none;"> |
| 21 | <br> |
| 22 | <button type="submit">Klasyfikuj</button> |
| 23 | </form> |
| 24 | |
| 25 | <script> |
| 26 | const imageInput = document.getElementById('imageInput'); |
| 27 | const preview = document.getElementById('preview'); |
| 28 | |
| 29 | imageInput.addEventListener('change', function(event) { |
| 30 | const file = event.target.files[0]; |
| 31 | if (file) { |
| 32 | const reader = new FileReader(); |
| 33 | reader.onload = function(e) { |
| 34 | preview.src = e.target.result; |
| 35 | preview.style.display = 'block'; |
| 36 | }; |
| 37 | reader.readAsDataURL(file); |
| 38 | } else { |
| 39 | preview.style.display = 'none'; |
| 40 | } |
| 41 | }); |
| 42 | </script> |
| 43 | </body> |
| 44 | </html> |
| 45 |
poziom1.py
· 1.9 KiB · Python
Raw
# Importowanie bibliotek
# Torch - głębokie sieci neuronowe (ang. Deep Neural Networks)
import torch
from torchvision import transforms
from PIL import Image
import timm
# Wczytaj model Swin Transformer z biblioteki timm
# ta wersja jest wyuczona na zbiorze ImageNet-21k
nazwa_modelu = "swin_large_patch4_window7_224"
klasyfikator = timm.create_model(nazwa_modelu, pretrained=True)
# klasyfikator.eval()
# Pobierz zbiór ImageNet (1000 klas)
import json
imagenet_labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
import requests
labels = json.loads(requests.get(imagenet_labels_url).text)
# Potok przetwarzania obrazu na potrzeby klasyfikatora
preprocess = transforms.Compose([
transforms.Resize((224, 224)), # model Swin Transformer bierze na wejściu obrazy 224x224
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), # Normalizacja
])
def recognize_image(img_path):
"""Wczytuje obraz z pliku, przetwarza zgodnie z potokiem preprocess a na koniec klasyfikuje"""
img = Image.open(img_path).convert("RGB")
# Dodatkowy wymiar na wejściu
img_tensor = preprocess(img).unsqueeze(0)
# Predykcja, czyli przypisanie prawdopodobieństw klas do obrazu wejściowego
with torch.no_grad():
logits = klasyfikator(img_tensor)
probabilities = torch.nn.functional.softmax(logits, dim=-1)
top5_prob, top5_catid = torch.topk(probabilities, 5)
# Przeliczenie prawdopodobieństw na etykiety klas
results = [(labels[catid], prob.item()) for catid, prob in zip(top5_catid[0], top5_prob[0])]
return results
if __name__ == "__main__":
img_path = "obrazek.jpg"
wyjscie_klasyfikatora = recognize_image(img_path)
for i, (label, confidence) in enumerate(wyjscie_klasyfikatora):
print(f"{i + 1}: {label} ({confidence:.2f})")
| 1 | # Importowanie bibliotek |
| 2 | # Torch - głębokie sieci neuronowe (ang. Deep Neural Networks) |
| 3 | import torch |
| 4 | from torchvision import transforms |
| 5 | from PIL import Image |
| 6 | import timm |
| 7 | |
| 8 | # Wczytaj model Swin Transformer z biblioteki timm |
| 9 | # ta wersja jest wyuczona na zbiorze ImageNet-21k |
| 10 | nazwa_modelu = "swin_large_patch4_window7_224" |
| 11 | klasyfikator = timm.create_model(nazwa_modelu, pretrained=True) |
| 12 | # klasyfikator.eval() |
| 13 | |
| 14 | # Pobierz zbiór ImageNet (1000 klas) |
| 15 | import json |
| 16 | imagenet_labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json" |
| 17 | import requests |
| 18 | labels = json.loads(requests.get(imagenet_labels_url).text) |
| 19 | |
| 20 | # Potok przetwarzania obrazu na potrzeby klasyfikatora |
| 21 | preprocess = transforms.Compose([ |
| 22 | transforms.Resize((224, 224)), # model Swin Transformer bierze na wejściu obrazy 224x224 |
| 23 | transforms.ToTensor(), |
| 24 | transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), # Normalizacja |
| 25 | ]) |
| 26 | |
| 27 | |
| 28 | def recognize_image(img_path): |
| 29 | """Wczytuje obraz z pliku, przetwarza zgodnie z potokiem preprocess a na koniec klasyfikuje""" |
| 30 | img = Image.open(img_path).convert("RGB") |
| 31 | # Dodatkowy wymiar na wejściu |
| 32 | img_tensor = preprocess(img).unsqueeze(0) |
| 33 | |
| 34 | # Predykcja, czyli przypisanie prawdopodobieństw klas do obrazu wejściowego |
| 35 | with torch.no_grad(): |
| 36 | logits = klasyfikator(img_tensor) |
| 37 | probabilities = torch.nn.functional.softmax(logits, dim=-1) |
| 38 | top5_prob, top5_catid = torch.topk(probabilities, 5) |
| 39 | |
| 40 | # Przeliczenie prawdopodobieństw na etykiety klas |
| 41 | results = [(labels[catid], prob.item()) for catid, prob in zip(top5_catid[0], top5_prob[0])] |
| 42 | return results |
| 43 | |
| 44 | |
| 45 | if __name__ == "__main__": |
| 46 | img_path = "obrazek.jpg" |
| 47 | wyjscie_klasyfikatora = recognize_image(img_path) |
| 48 | for i, (label, confidence) in enumerate(wyjscie_klasyfikatora): |
| 49 | print(f"{i + 1}: {label} ({confidence:.2f})") |
| 50 |
poziom2_flask.py
· 2.5 KiB · Python
Raw
from flask import Flask, request, render_template, redirect, url_for
import torch
from torchvision import transforms
from PIL import Image
import timm
import json
import requests
import os
app = Flask(__name__)
UPLOAD_FOLDER = 'static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Folder na załadowane obrazki
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Wczytaj model Swin Transformer z biblioteki timm
# ta wersja jest wyuczona na zbiorze ImageNet-21k
nazwa_modelu = "swin_large_patch4_window7_224"
klasyfikator = timm.create_model(nazwa_modelu, pretrained=True)
#klasyfikator.eval()
# Pobierz zbiór ImageNet (1000 klas)
imagenet_labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
labels = json.loads(requests.get(imagenet_labels_url).text)
# Potok przetwarzania obrazu na potrzeby klasyfikatora
preprocess = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])
def recognize_image(img_path):
"""
Wczytuje obraz z pliku, przetwarza zgodnie z potokiem preprocess a na koniec klasyfikuje
"""
img = Image.open(img_path).convert("RGB")
# Dodatkowy wymiar na wejściu
img_tensor = preprocess(img).unsqueeze(0)
# Predykcja, czyli przypisanie prawdopodobieństw klas do obrazu wejściowego
with torch.no_grad():
logits = klasyfikator(img_tensor)
probabilities = torch.nn.functional.softmax(logits, dim=-1)
top5_prob, top5_catid = torch.topk(probabilities, 5)
# Przeliczenie prawdopodobieństw na etykiety klas
results = [(labels[catid], prob.item()) for catid, prob in zip(top5_catid[0], top5_prob[0])]
return results
@app.route('/')
def index():
return render_template("index.html")
@app.route('/', methods=['POST'])
def classify_image():
if 'image' not in request.files:
return redirect(url_for('index'))
image = request.files['image']
if image.filename == '':
return redirect(url_for('index'))
try:
# Zapisz wczytany obraz do katalogu UPLOAD_FOLDER
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
image.save(image_path)
# Klasyfikacja
wyjscie_klasyfikatora = recognize_image(image_path)
return render_template("result.html", image_path=image_path, predictions=wyjscie_klasyfikatora)
except Exception as e:
return str(e), 500
if __name__ == "__main__":
app.run(debug=True)
| 1 | from flask import Flask, request, render_template, redirect, url_for |
| 2 | import torch |
| 3 | from torchvision import transforms |
| 4 | from PIL import Image |
| 5 | import timm |
| 6 | import json |
| 7 | import requests |
| 8 | import os |
| 9 | |
| 10 | app = Flask(__name__) |
| 11 | UPLOAD_FOLDER = 'static/uploads' |
| 12 | app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
| 13 | |
| 14 | # Folder na załadowane obrazki |
| 15 | os.makedirs(UPLOAD_FOLDER, exist_ok=True) |
| 16 | |
| 17 | # Wczytaj model Swin Transformer z biblioteki timm |
| 18 | # ta wersja jest wyuczona na zbiorze ImageNet-21k |
| 19 | nazwa_modelu = "swin_large_patch4_window7_224" |
| 20 | klasyfikator = timm.create_model(nazwa_modelu, pretrained=True) |
| 21 | #klasyfikator.eval() |
| 22 | |
| 23 | # Pobierz zbiór ImageNet (1000 klas) |
| 24 | imagenet_labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json" |
| 25 | labels = json.loads(requests.get(imagenet_labels_url).text) |
| 26 | |
| 27 | # Potok przetwarzania obrazu na potrzeby klasyfikatora |
| 28 | preprocess = transforms.Compose([ |
| 29 | transforms.Resize((224, 224)), |
| 30 | transforms.ToTensor(), |
| 31 | transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), |
| 32 | ]) |
| 33 | |
| 34 | def recognize_image(img_path): |
| 35 | """ |
| 36 | Wczytuje obraz z pliku, przetwarza zgodnie z potokiem preprocess a na koniec klasyfikuje |
| 37 | """ |
| 38 | img = Image.open(img_path).convert("RGB") |
| 39 | # Dodatkowy wymiar na wejściu |
| 40 | img_tensor = preprocess(img).unsqueeze(0) |
| 41 | |
| 42 | # Predykcja, czyli przypisanie prawdopodobieństw klas do obrazu wejściowego |
| 43 | with torch.no_grad(): |
| 44 | logits = klasyfikator(img_tensor) |
| 45 | probabilities = torch.nn.functional.softmax(logits, dim=-1) |
| 46 | top5_prob, top5_catid = torch.topk(probabilities, 5) |
| 47 | |
| 48 | # Przeliczenie prawdopodobieństw na etykiety klas |
| 49 | results = [(labels[catid], prob.item()) for catid, prob in zip(top5_catid[0], top5_prob[0])] |
| 50 | return results |
| 51 | |
| 52 | @app.route('/') |
| 53 | def index(): |
| 54 | return render_template("index.html") |
| 55 | |
| 56 | @app.route('/', methods=['POST']) |
| 57 | def classify_image(): |
| 58 | if 'image' not in request.files: |
| 59 | return redirect(url_for('index')) |
| 60 | |
| 61 | image = request.files['image'] |
| 62 | if image.filename == '': |
| 63 | return redirect(url_for('index')) |
| 64 | |
| 65 | try: |
| 66 | # Zapisz wczytany obraz do katalogu UPLOAD_FOLDER |
| 67 | image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename) |
| 68 | image.save(image_path) |
| 69 | |
| 70 | # Klasyfikacja |
| 71 | wyjscie_klasyfikatora = recognize_image(image_path) |
| 72 | return render_template("result.html", image_path=image_path, predictions=wyjscie_klasyfikatora) |
| 73 | except Exception as e: |
| 74 | return str(e), 500 |
| 75 | |
| 76 | if __name__ == "__main__": |
| 77 | app.run(debug=True) |
| 78 |
result.html
· 531 B · HTML
Raw
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" c initial-scale=1.0">
<title>Wyniki klasyfikacji</title>
</head>
<body>
<h1>Wyniki klasyfikacji</h1>
<img src="{{ image_path }}" alt="Uploaded Image" style="max-width: 500px; max-height: 500px;">
<h2>5 najlepszych trafień:</h2>
<ol>
{% for label, confidence in predictions %}
<li>{{ label }}: {{ confidence | round(2) }}</li>
{% endfor %}
</ol>
<a href="/">Powtórz</a>
</body>
</html>
| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="UTF-8"> |
| 5 | <meta name="viewport" c initial-scale=1.0"> |
| 6 | <title>Wyniki klasyfikacji</title> |
| 7 | </head> |
| 8 | <body> |
| 9 | <h1>Wyniki klasyfikacji</h1> |
| 10 | <img src="{{ image_path }}" alt="Uploaded Image" style="max-width: 500px; max-height: 500px;"> |
| 11 | <h2>5 najlepszych trafień:</h2> |
| 12 | <ol> |
| 13 | {% for label, confidence in predictions %} |
| 14 | <li>{{ label }}: {{ confidence | round(2) }}</li> |
| 15 | {% endfor %} |
| 16 | </ol> |
| 17 | <a href="/">Powtórz</a> |
| 18 | </body> |
| 19 | </html> |