Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
4 |
-
import
|
5 |
|
6 |
pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
7 |
|
@@ -16,7 +16,13 @@ if st.button("Take an image from camera"):
|
|
16 |
cap = cv2.VideoCapture(0)
|
17 |
ret, frame = cap.read()
|
18 |
if ret:
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
file_name = 'webcam_image.jpg'
|
21 |
|
22 |
# Add a text bar to add a title
|
@@ -28,11 +34,17 @@ image_description = st.text_input("Image Description", value="(Optional)")
|
|
28 |
if file_name is not None:
|
29 |
col1, col2 = st.columns(2)
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
col1.image(image, use_column_width=True)
|
33 |
predictions = pipeline(image)
|
34 |
|
35 |
col2.header("Probabilities")
|
36 |
for p in predictions:
|
37 |
col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
|
38 |
-
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
4 |
+
import base64
|
5 |
|
6 |
pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
7 |
|
|
|
16 |
cap = cv2.VideoCapture(0)
|
17 |
ret, frame = cap.read()
|
18 |
if ret:
|
19 |
+
# Encode the webcam image as a Base64 string
|
20 |
+
img_encoded = base64.b64encode(cv2.imencode('.jpg', frame)[1]).decode('utf-8')
|
21 |
+
|
22 |
+
# Pass the Base64 encoded image to the pipeline function
|
23 |
+
predictions = pipeline(Image.open('data:image/jpeg;base64,' + img_encoded))
|
24 |
+
|
25 |
+
# Replace file_name with the encoded image
|
26 |
file_name = 'webcam_image.jpg'
|
27 |
|
28 |
# Add a text bar to add a title
|
|
|
34 |
if file_name is not None:
|
35 |
col1, col2 = st.columns(2)
|
36 |
|
37 |
+
# Check if the file is a webcam image
|
38 |
+
if file_name == 'webcam_image.jpg':
|
39 |
+
# Use the Base64 encoded image
|
40 |
+
image = Image.open('data:image/jpeg;base64,' + img_encoded)
|
41 |
+
else:
|
42 |
+
# Open the uploaded image
|
43 |
+
image = Image.open(file_name)
|
44 |
+
|
45 |
col1.image(image, use_column_width=True)
|
46 |
predictions = pipeline(image)
|
47 |
|
48 |
col2.header("Probabilities")
|
49 |
for p in predictions:
|
50 |
col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
|
|