Hammad712 commited on
Commit
327e205
·
verified ·
1 Parent(s): 5e4f357

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -60,10 +60,14 @@ st.markdown("""
60
 
61
  # Title and description
62
  st.markdown('<div class="title">Emotion Detection</div>', unsafe_allow_html=True)
63
- st.markdown('<div class="description">Upload an image to detect emotions</div>', unsafe_allow_html=True)
 
 
 
 
64
 
65
  # File uploader for images
66
- st.markdown('<div class="header">Upload Image</div>', unsafe_allow_html=True)
67
  image_file = st.file_uploader("Choose an image", type=["png", 'jpg'])
68
 
69
  # Sidebar to show previous predictions
@@ -74,10 +78,25 @@ st.sidebar.header("Previous Predictions")
74
  for pred in st.session_state.predictions:
75
  st.sidebar.image(pred['image'], caption=pred['emotion'], use_column_width=True)
76
 
77
- # Display the uploaded image and predict emotions
78
- if image_file is not None:
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  image = Image.open(image_file)
80
- st.image(image, caption="Uploaded Image", use_column_width=True)
 
 
81
 
82
  with st.spinner('Processing...'):
83
  st.markdown('<div class="spinner-text">Processing...</div>', unsafe_allow_html=True)
 
60
 
61
  # Title and description
62
  st.markdown('<div class="title">Emotion Detection</div>', unsafe_allow_html=True)
63
+ st.markdown('<div class="description">Upload an image or provide an image URL to detect emotions</div>', unsafe_allow_html=True)
64
+
65
+ # Input field for image URL
66
+ st.markdown('<div class="header">Enter Image URL</div>', unsafe_allow_html=True)
67
+ image_url = st.text_input("Image URL")
68
 
69
  # File uploader for images
70
+ st.markdown('<div class="header">Or Upload Image</div>', unsafe_allow_html=True)
71
  image_file = st.file_uploader("Choose an image", type=["png", 'jpg'])
72
 
73
  # Sidebar to show previous predictions
 
78
  for pred in st.session_state.predictions:
79
  st.sidebar.image(pred['image'], caption=pred['emotion'], use_column_width=True)
80
 
81
+ # Function to load image from URL
82
+ def load_image_from_url(url):
83
+ try:
84
+ response = requests.get(url)
85
+ img = Image.open(BytesIO(response.content))
86
+ return img
87
+ except Exception as e:
88
+ st.error(f"Error loading image: {e}")
89
+ return None
90
+
91
+ # Display the image and predict emotions
92
+ image = None
93
+ if image_url:
94
+ image = load_image_from_url(image_url)
95
+ elif image_file is not None:
96
  image = Image.open(image_file)
97
+
98
+ if image:
99
+ st.image(image, caption="Selected Image", use_column_width=True)
100
 
101
  with st.spinner('Processing...'):
102
  st.markdown('<div class="spinner-text">Processing...</div>', unsafe_allow_html=True)