-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecycling_classifier.py
66 lines (50 loc) · 1.64 KB
/
recycling_classifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import cv2
import torch
from torchvision import transforms
from PIL import Image
class SimpleModel(torch.nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.conv1 = torch.nn.Conv2d(3, 16, 3, 1)
self.fc1 = torch.nn.Linear(16*222*222, 3)
def forward(self, x):
x = torch.relu(self.conv1(x))
x = x.view(x.size(0), -1)
x = self.fc1(x)
return x
model = SimpleModel()
state_dict = torch.load('best.pt', map_location=torch.device('cpu'))
model.load_state_dict(state_dict, strict=False)
model.eval()
categories = ['Plastic', 'Aluminum', 'Glass', 'Other']
transform = 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 preprocess_frame(frame):
img = Image.fromarray(frame)
img = transform(img)
img = img.unsqueeze(0)
return img
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("Error: Failed to capture image.")
break
preprocessed_frame = preprocess_frame(frame)
with torch.no_grad():
outputs = model(preprocessed_frame)
_, predicted = torch.max(outputs, 1)
class_idx = predicted.item()
class_name = categories[class_idx]
cv2.putText(frame, f'Material: {class_name}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
cv2.imshow('Recycling Classifier', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()