-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
78 lines (63 loc) · 2.46 KB
/
Copy pathscript.py
File metadata and controls
78 lines (63 loc) · 2.46 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
import os
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import model_from_json
# Set the environment variable for encoding
os.environ['PYTHONIOENCODING'] = 'utf-8'
# Load the model architecture
try:
with open('./final_model/garbage_classification_inception.json', 'r', encoding='utf-8') as json_file:
model_json = json_file.read()
print("Model architecture loaded successfully.")
except UnicodeEncodeError as e:
print(f"Error reading model architecture: {e}")
exit(1)
model = model_from_json(model_json)
# Load the model weights
try:
model.load_weights('./final_model/garbage_classification_inception_weights.h5')
print("Model weights loaded successfully.")
except Exception as e:
print(f"Error loading model weights: {e}")
exit(1)
# Define the class names (replace these with your actual class names)
class_names = ["battery", "biological", "clothes", "glass", "metal", "paper", "plastic", "shoes", "trash"]
# Function to preprocess the frame
def preprocess_frame(frame):
img = cv2.resize(frame, (400, 400)) # Resize the frame to the input size of the model
img = img.astype('float32') / 255.0 # Normalize the image
img = np.expand_dims(img, axis=0) # Expand dimensions to fit the model input
return img
# Function to get the class name from prediction
def get_class_name(prediction):
class_idx = np.argmax(prediction)
return class_names[class_idx]
# Initialize the webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read() # Capture frame-by-frame
if not ret:
break
# Preprocess the frame
preprocessed_frame = preprocess_frame(frame)
# Make prediction
try:
prediction = model.predict(preprocessed_frame)
class_name = get_class_name(prediction)
confidence = np.max(prediction)
except Exception as e:
print(f"Error during prediction: {e}")
continue
# Draw bounding box and label
label = f"{class_name}: {confidence:.2f}"
cv2.putText(frame, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.rectangle(frame, (10, 10), (frame.shape[1]-10, frame.shape[0]-10), (0, 255, 0), 2) # Draw a rectangle around the object
# Display the resulting frame
cv2.imshow('Object Detection', frame)
# Break the loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close windows
cap.release()
cv2.destroyAllWindows()