Skip to content
Snippets Groups Projects

made so code can be trained on GPU

Merged AC4908 requested to merge test_expanding_model into main
7 files
+ 106
23
Compare changes
  • Side-by-side
  • Inline
Files
7
+ 66
0
import keras
import numpy as np
import cv2
class Classifier:
def __init__(self, model_path, label_path):
self.model_path = model_path
np.set_printoptions(suppress=True)
self.model = keras.models.load_model(self.model_path)
self.data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
self.labels_path = label_path
if self.labels_path:
with open(self.labels_path, "r") as label_file:
self.list_labels = [line.strip() for line in label_file]
else:
print("No Labels Found")
def getPrediction(self, img, draw=True, pos=(50, 50), scale=2, color=(0, 255, 0)):
img_resized = cv2.resize(img, (224, 224))
image_array = np.asarray(img_resized)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
self.data[0] = normalized_image_array
prediction = self.model.predict(self.data)
index_val = np.argmax(prediction)
if draw and self.labels_path:
label_text = str(self.list_labels[index_val])
cv2.putText(img, label_text, pos, cv2.FONT_HERSHEY_COMPLEX, scale, color, 2)
return list(prediction[0]), index_val
def main():
cap = cv2.VideoCapture(0)
mask_classifier = Classifier('Model/keras_model.h5', 'Model/labels.txt')
while True:
ret, frame = cap.read()
if not ret:
print("Failed to capture frame")
break
prediction = mask_classifier.getPrediction(frame)
print("Prediction:", prediction)
cv2.imshow("Image", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
Loading