Harsh-Jadhav
commited on
Commit
•
ed38da4
1
Parent(s):
e3facda
Update app.py
Browse filesUpdated webcam capture code
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from fastai.vision.all import *
|
|
|
|
|
3 |
|
4 |
# Load the pre-trained model
|
5 |
model_path = "assets/model-r34.pkl" # Replace with the path to your model file
|
@@ -33,24 +35,29 @@ if uploaded_image is not None:
|
|
33 |
# Capture an image from webcam
|
34 |
capture = st.checkbox("Capture an Image from Webcam")
|
35 |
|
36 |
-
|
37 |
st.write("Click the button to capture the image")
|
38 |
capture_button = st.button("Capture")
|
39 |
|
40 |
if capture_button:
|
41 |
# Capture the image from the webcam
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# cap.release()
|
50 |
|
51 |
# After capturing the image, predict and display it
|
52 |
-
captured_image_path = "captured_image.jpg"
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
# Make predictions
|
56 |
prediction, confidence = predict(captured_image_path)
|
@@ -60,6 +67,10 @@ if capture:
|
|
60 |
st.write(f"Gesture: {prediction}")
|
61 |
st.write(f"Confidence: {confidence:.2f}")
|
62 |
|
|
|
|
|
|
|
|
|
63 |
# Example images
|
64 |
st.sidebar.title("Example Images")
|
65 |
example_images = {
|
|
|
1 |
import streamlit as st
|
2 |
from fastai.vision.all import *
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
|
6 |
# Load the pre-trained model
|
7 |
model_path = "assets/model-r34.pkl" # Replace with the path to your model file
|
|
|
35 |
# Capture an image from webcam
|
36 |
capture = st.checkbox("Capture an Image from Webcam")
|
37 |
|
38 |
+
def capture_image():
|
39 |
st.write("Click the button to capture the image")
|
40 |
capture_button = st.button("Capture")
|
41 |
|
42 |
if capture_button:
|
43 |
# Capture the image from the webcam
|
44 |
+
cap = cv2.VideoCapture(0)
|
45 |
+
|
46 |
+
if not cap.isOpened():
|
47 |
+
st.error("Error: Unable to access the camera")
|
48 |
+
return
|
49 |
|
50 |
+
ret, frame = cap.read()
|
51 |
+
if not ret:
|
52 |
+
st.error("Error: Could not capture image")
|
53 |
+
return
|
|
|
54 |
|
55 |
# After capturing the image, predict and display it
|
56 |
+
captured_image_path = "captured_image.jpg"
|
57 |
+
cv2.imwrite(captured_image_path, frame)
|
58 |
+
|
59 |
+
# Display the captured image
|
60 |
+
st.image(captured_image_path, caption = "Captured Image", use_column_width = True)
|
61 |
|
62 |
# Make predictions
|
63 |
prediction, confidence = predict(captured_image_path)
|
|
|
67 |
st.write(f"Gesture: {prediction}")
|
68 |
st.write(f"Confidence: {confidence:.2f}")
|
69 |
|
70 |
+
# Capture calling
|
71 |
+
if capture:
|
72 |
+
capture_image()
|
73 |
+
|
74 |
# Example images
|
75 |
st.sidebar.title("Example Images")
|
76 |
example_images = {
|