GC22's picture
Update app.py
a28fd10
import streamlit as st
import cv2
import numpy as np
import urllib.request
# Download the Haar cascade XML file
cascade_file_url = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml"
cascade_file_path = "haarcascade_frontalface_default.xml"
urllib.request.urlretrieve(cascade_file_url, cascade_file_path)
# Load the pre-trained object detection model
model = cv2.CascadeClassifier(cascade_file_path)
# Function to perform object recognition
def detect_objects(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
objects = model.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in objects:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
return image
# Streamlit app
st.title('Face Detection')
uploaded_file = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
if uploaded_file is not None:
# Read the uploaded image
image = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
if image is None:
st.write('Error: Unable to read the image. Please make sure the uploaded file is a valid image.')
else:
# Perform object recognition
result = detect_objects(image)
if result is None:
st.write('Error: Face detection failed.')
else:
# Display the result
st.image(result, channels='BGR')