Upload 3 files
Browse files- Model/keras_model.h5 +3 -0
- Model/labels.txt +9 -0
- app.py +82 -0
Model/keras_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:761cb0a866753925c6eb03a41ce147a2e2a80aaa0f23ebf678c7ad20d1448046
|
| 3 |
+
size 2458208
|
Model/labels.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0 Nothing
|
| 2 |
+
1 Zip-top cans
|
| 3 |
+
2 Newspaper
|
| 4 |
+
3 Old shoes
|
| 5 |
+
4 Watercolor pen
|
| 6 |
+
5 Disinfectant
|
| 7 |
+
6 Battery
|
| 8 |
+
7 Vegetable leaf
|
| 9 |
+
8 Apple
|
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Load the model manually using Keras
|
| 9 |
+
model = load_model('Model/keras_model.h5', compile=False) # Load model without compilation
|
| 10 |
+
|
| 11 |
+
# Labels directly added to the code
|
| 12 |
+
labels = [
|
| 13 |
+
"Nothing",
|
| 14 |
+
"Zip-top cans",
|
| 15 |
+
"Newspaper",
|
| 16 |
+
"Old shoes",
|
| 17 |
+
"Watercolor pen",
|
| 18 |
+
"Disinfectant",
|
| 19 |
+
"Battery",
|
| 20 |
+
"Vegetable leaf",
|
| 21 |
+
"Apple"
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# Classification dictionary (modified to skip image-based overlays)
|
| 25 |
+
classDic = {0: None,
|
| 26 |
+
1: 0,
|
| 27 |
+
2: 0,
|
| 28 |
+
3: 3,
|
| 29 |
+
4: 3,
|
| 30 |
+
5: 1,
|
| 31 |
+
6: 1,
|
| 32 |
+
7: 2,
|
| 33 |
+
8: 2}
|
| 34 |
+
|
| 35 |
+
# Streamlit page configuration
|
| 36 |
+
st.title('Object Detection with Webcam')
|
| 37 |
+
st.write("This application uses your webcam to detect objects in real-time.")
|
| 38 |
+
|
| 39 |
+
# Start the webcam feed for Streamlit
|
| 40 |
+
cap = cv2.VideoCapture(0) # Manually set to camera index 0 (or change to 1, 2 if necessary)
|
| 41 |
+
if not cap.isOpened():
|
| 42 |
+
st.error("Error: No camera found!")
|
| 43 |
+
exit()
|
| 44 |
+
|
| 45 |
+
stframe = st.empty() # Placeholder for the webcam feed
|
| 46 |
+
|
| 47 |
+
# Add the 'Exit' button outside the loop so it's only displayed once
|
| 48 |
+
exit_button = st.button('Exit', key="exit_button")
|
| 49 |
+
|
| 50 |
+
# Loop to keep updating webcam feed
|
| 51 |
+
while True:
|
| 52 |
+
ret, img = cap.read()
|
| 53 |
+
if not ret or img is None:
|
| 54 |
+
st.error("Error: Failed to capture image!")
|
| 55 |
+
continue
|
| 56 |
+
|
| 57 |
+
imgResize = cv2.resize(img, (454, 340)) # Resize captured frame to a specific size
|
| 58 |
+
|
| 59 |
+
# Perform prediction using the loaded model
|
| 60 |
+
img_input = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
|
| 61 |
+
img_input = cv2.resize(img_input, (224, 224)) # Resize to expected input size for your model
|
| 62 |
+
img_input = img_input / 255.0 # Normalize the image
|
| 63 |
+
prediction = model.predict(tf.convert_to_tensor(img_input[None, ...])) # Make prediction
|
| 64 |
+
classID = prediction.argmax() # Get the predicted class ID
|
| 65 |
+
print(f"Predicted class ID: {classID}")
|
| 66 |
+
|
| 67 |
+
# Handle the class ID (can be used to trigger different actions based on class)
|
| 68 |
+
if 0 < classID <= len(labels):
|
| 69 |
+
st.write(f"Detected class: {labels[classID - 1]}")
|
| 70 |
+
else:
|
| 71 |
+
st.write(f"Invalid classID: {classID}, setting to default.")
|
| 72 |
+
|
| 73 |
+
# Display the resized image in Streamlit
|
| 74 |
+
img_resized = cv2.cvtColor(imgResize, cv2.COLOR_BGR2RGB) # Convert image to RGB
|
| 75 |
+
stframe.image(img_resized, channels="RGB", caption="Real-time Detection", use_container_width=True)
|
| 76 |
+
|
| 77 |
+
# Exit condition based on button press
|
| 78 |
+
if exit_button:
|
| 79 |
+
break
|
| 80 |
+
|
| 81 |
+
# Release resources and close the window
|
| 82 |
+
cap.release()
|