File size: 1,428 Bytes
a66c37e
 
dff77d8
a28fd10
 
 
 
 
 
a66c37e
 
a28fd10
a66c37e
 
 
 
 
 
 
 
 
 
a28fd10
a66c37e
 
 
 
 
 
a28fd10
 
 
 
 
 
 
 
 
a66c37e
a28fd10
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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')