Spaces:
Runtime error
Runtime error
skatanic9421rn
commited on
Commit
•
e07ca76
1
Parent(s):
64c4aee
komit
Browse files- app.py +47 -5
- datasets.py +28 -0
- download_image.py +42 -0
- models.py +22 -0
app.py
CHANGED
@@ -1,7 +1,49 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
import torchvision.transforms as transforms
|
4 |
+
from torch.utils.data import DataLoader
|
5 |
+
from PIL import Image
|
6 |
+
from models import ResNet18
|
7 |
+
from datasets import HandGestureDataset
|
8 |
|
9 |
+
# Set the path to the dataset directory
|
10 |
+
data_dir = 'dataset'
|
11 |
|
12 |
+
# Define the image transforms
|
13 |
+
transform = transforms.Compose([
|
14 |
+
transforms.Resize((224, 224)),
|
15 |
+
transforms.ToTensor(),
|
16 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
17 |
+
])
|
18 |
+
|
19 |
+
# Define the dataset
|
20 |
+
dataset = HandGestureDataset(data_dir, transform=transform)
|
21 |
+
|
22 |
+
# Define the data loader
|
23 |
+
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
|
24 |
+
|
25 |
+
# Load the pre-trained neural network
|
26 |
+
model = ResNet18(pretrained=True)
|
27 |
+
|
28 |
+
# Replace the final fully connected layer with a new one
|
29 |
+
num_classes = 7
|
30 |
+
model.fc = torch.nn.Linear(model.fc.in_features, num_classes)
|
31 |
+
|
32 |
+
# Set the model to evaluation mode
|
33 |
+
model.eval()
|
34 |
+
|
35 |
+
# Load an image from the dataset
|
36 |
+
for i, (image, label) in enumerate(dataloader):
|
37 |
+
# Apply the image transforms
|
38 |
+
image = transform(image)
|
39 |
+
|
40 |
+
# Add a batch dimension
|
41 |
+
image = image.unsqueeze(0)
|
42 |
+
|
43 |
+
# Make a prediction on the image
|
44 |
+
with torch.no_grad():
|
45 |
+
output = model(image)
|
46 |
+
prediction = torch.argmax(output)
|
47 |
+
|
48 |
+
# Print the prediction
|
49 |
+
print(f'Image {i+1}, Predicted note: {prediction.item()}')
|
datasets.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from torchvision.transforms import transforms
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
class HandGestureDataset(torch.utils.data.Dataset):
|
7 |
+
def __init__(self, data_dir, transform=None):
|
8 |
+
self.data_dir = data_dir
|
9 |
+
self.transform = transform
|
10 |
+
self.image_files = [os.path.join(self.data_dir, f) for f in os.listdir(self.data_dir) if f.endswith('.jpg')]
|
11 |
+
|
12 |
+
def __len__(self):
|
13 |
+
return len(self.image_files)
|
14 |
+
|
15 |
+
def __getitem__(self, idx):
|
16 |
+
image_path = self.image_files[idx]
|
17 |
+
image = Image.open(image_path)
|
18 |
+
|
19 |
+
if self.transform:
|
20 |
+
image = self.transform(image)
|
21 |
+
|
22 |
+
label = self.get_label(image_path)
|
23 |
+
|
24 |
+
return image, label
|
25 |
+
|
26 |
+
def get_label(self, image_path):
|
27 |
+
label = os.path.basename(os.path.dirname(image_path))
|
28 |
+
return label
|
download_image.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
|
4 |
+
def download_images(url_list, output_folder):
|
5 |
+
# Kreiranje izlaznog foldera ako ne postoji
|
6 |
+
os.makedirs(output_folder, exist_ok=True)
|
7 |
+
|
8 |
+
# Iteriranje kroz listu URL-ova
|
9 |
+
for i, url in enumerate(url_list):
|
10 |
+
try:
|
11 |
+
# Preuzimanje slike sa URL-a
|
12 |
+
response = requests.get(url)
|
13 |
+
if response.status_code == 200:
|
14 |
+
# Extrakcija naziva datoteke iz URL-a
|
15 |
+
filename = url.split("/")[-1]
|
16 |
+
# Razdvajanje naziva datoteke na osnovu nota
|
17 |
+
note = filename.split("_")[0]
|
18 |
+
# Kreiranje podfoldera za notu ako ne postoji
|
19 |
+
note_folder = os.path.join(output_folder, note)
|
20 |
+
os.makedirs(note_folder, exist_ok=True)
|
21 |
+
# Čuvanje slike u odgovarajućem podfolderu
|
22 |
+
with open(os.path.join(note_folder, filename), "wb") as f:
|
23 |
+
f.write(response.content)
|
24 |
+
print(f"Slika {i+1} uspešno preuzeta i sačuvana u {note_folder}.")
|
25 |
+
else:
|
26 |
+
print(f"Greska prilikom preuzimanja slike {i+1}. Status kod: {response.status_code}")
|
27 |
+
except Exception as e:
|
28 |
+
print(f"Greska prilikom preuzimanja slike {i+1}: {str(e)}")
|
29 |
+
|
30 |
+
# Lista URL-ova sa kojih ćemo preuzimati slike (primer)
|
31 |
+
url_list = [
|
32 |
+
"https://example.com/do_image1.jpg",
|
33 |
+
"https://example.com/re_image1.jpg",
|
34 |
+
"https://example.com/mi_image1.jpg",
|
35 |
+
# Dodajte ostale URL-ove prema potrebi
|
36 |
+
]
|
37 |
+
|
38 |
+
# Folder u koji ćemo sačuvati preuzete slike
|
39 |
+
output_folder = "dataset"
|
40 |
+
|
41 |
+
# Pozivanje funkcije za preuzimanje slika
|
42 |
+
download_images(url_list, output_folder)
|
models.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
class Net(nn.Module):
|
5 |
+
def __init__(self):
|
6 |
+
super(Net, self).__init__()
|
7 |
+
self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
|
8 |
+
self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
|
9 |
+
self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
|
10 |
+
self.pool = nn.MaxPool2d(2, 2)
|
11 |
+
self.fc1 = nn.Linear(64 * 28 * 28, 512)
|
12 |
+
self.fc2 = nn.Linear(512, 7)
|
13 |
+
self.dropout = nn.Dropout(0.2)
|
14 |
+
|
15 |
+
def forward(self, x):
|
16 |
+
x = self.pool(F.relu(self.conv1(x)))
|
17 |
+
x = self.pool(F.relu(self.conv2(x)))
|
18 |
+
x = self.pool(F.relu(self.conv3(x)))
|
19 |
+
x = x.view(-1, 64 * 28 * 28)
|
20 |
+
x = self.dropout(F.relu(self.fc1(x)))
|
21 |
+
x = self.fc2(x)
|
22 |
+
return x
|