| | import streamlit as st |
| | import os |
| | import tensorflow as tf |
| | from tensorflow.keras.models import load_model |
| | import cv2 |
| | import numpy as np |
| |
|
| | |
| | model = load_model('Model/keras_model.h5', compile=False) |
| |
|
| | |
| | labels = [ |
| | "Nothing", |
| | "Zip-top cans", |
| | "Newspaper", |
| | "Old shoes", |
| | "Watercolor pen", |
| | "Disinfectant", |
| | "Battery", |
| | "Vegetable leaf", |
| | "Apple" |
| | ] |
| |
|
| | |
| | classDic = {0: None, |
| | 1: 0, |
| | 2: 0, |
| | 3: 3, |
| | 4: 3, |
| | 5: 1, |
| | 6: 1, |
| | 7: 2, |
| | 8: 2} |
| |
|
| | |
| | st.title('Object Detection with Webcam') |
| | st.write("This application uses your webcam to detect objects in real-time.") |
| |
|
| | |
| | cap = cv2.VideoCapture(0) |
| | if not cap.isOpened(): |
| | st.error("Error: No camera found!") |
| | exit() |
| |
|
| | stframe = st.empty() |
| |
|
| | |
| | exit_button = st.button('Exit', key="exit_button") |
| |
|
| | |
| | while True: |
| | ret, img = cap.read() |
| | if not ret or img is None: |
| | st.error("Error: Failed to capture image!") |
| | continue |
| |
|
| | imgResize = cv2.resize(img, (454, 340)) |
| |
|
| | |
| | img_input = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| | img_input = cv2.resize(img_input, (224, 224)) |
| | img_input = img_input / 255.0 |
| | prediction = model.predict(tf.convert_to_tensor(img_input[None, ...])) |
| | classID = prediction.argmax() |
| | print(f"Predicted class ID: {classID}") |
| |
|
| | |
| | if 0 < classID <= len(labels): |
| | st.write(f"Detected class: {labels[classID - 1]}") |
| | else: |
| | st.write(f"Invalid classID: {classID}, setting to default.") |
| |
|
| | |
| | img_resized = cv2.cvtColor(imgResize, cv2.COLOR_BGR2RGB) |
| | stframe.image(img_resized, channels="RGB", caption="Real-time Detection", use_container_width=True) |
| |
|
| | |
| | if exit_button: |
| | break |
| |
|
| | |
| | cap.release() |
| |
|