GC22 commited on
Commit
a28fd10
1 Parent(s): dff77d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -1,9 +1,15 @@
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
 
 
 
 
 
 
4
 
5
  # Load the pre-trained object detection model
6
- model = cv2.CascadeClassifier('path/to/haarcascade.xml')
7
 
8
  # Function to perform object recognition
9
  def detect_objects(image):
@@ -14,15 +20,23 @@ def detect_objects(image):
14
  return image
15
 
16
  # Streamlit app
17
- st.title('Object Recognition')
18
  uploaded_file = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
19
 
20
  if uploaded_file is not None:
21
  # Read the uploaded image
22
  image = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
23
 
24
- # Perform object recognition
25
- result = detect_objects(image)
 
 
 
 
 
 
 
26
 
27
- # Display the result
28
- st.image(result, channels='BGR')
 
 
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
4
+ import urllib.request
5
+
6
+ # Download the Haar cascade XML file
7
+ cascade_file_url = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml"
8
+ cascade_file_path = "haarcascade_frontalface_default.xml"
9
+ urllib.request.urlretrieve(cascade_file_url, cascade_file_path)
10
 
11
  # Load the pre-trained object detection model
12
+ model = cv2.CascadeClassifier(cascade_file_path)
13
 
14
  # Function to perform object recognition
15
  def detect_objects(image):
 
20
  return image
21
 
22
  # Streamlit app
23
+ st.title('Face Detection')
24
  uploaded_file = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
25
 
26
  if uploaded_file is not None:
27
  # Read the uploaded image
28
  image = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
29
 
30
+ if image is None:
31
+ st.write('Error: Unable to read the image. Please make sure the uploaded file is a valid image.')
32
+
33
+ else:
34
+ # Perform object recognition
35
+ result = detect_objects(image)
36
+
37
+ if result is None:
38
+ st.write('Error: Face detection failed.')
39
 
40
+ else:
41
+ # Display the result
42
+ st.image(result, channels='BGR')