File size: 2,451 Bytes
2c87ef6
 
 
 
 
 
 
 
 
 
 
2058162
2c87ef6
 
 
 
 
 
 
 
 
 
2058162
2c87ef6
2058162
 
 
2c87ef6
 
 
 
 
 
2058162
2c87ef6
 
 
 
 
 
2058162
 
 
 
 
 
 
 
 
 
 
 
 
2c87ef6
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import streamlit as st
import cv2
from PIL import Image as PilImage
from PIL import ImageDraw
import numpy as np
import io
import base64

# Load the face detection classifier
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

def detect_and_write_on_faces(image, texts_to_write):
    # Convert the uploaded image to grayscale for face detection
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Detect faces in the image
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

    # Convert the OpenCV image to a Pillow image
    pil_image = PilImage.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(pil_image)

    for i, (x, y, w, h) in enumerate(faces):
        # Write text on the detected face
        if i < len(texts_to_write):
            text_to_write = texts_to_write[i]
            draw.text((x, y - 10), text_to_write, fill=(255, 0, 0, 0))

    # Convert the Pillow image back to OpenCV format
    image_with_text = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)

    return image_with_text

st.title("Face Detection and Text Writing")

uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])

if uploaded_image is not None:
    if st.button("Process Image"):
        input_image = cv2.imdecode(np.frombuffer(uploaded_image.read(), np.uint8), -1)
        
        # Define different texts for each face
        texts_to_write = [
            "Happy face, this person has a retention rate of 69.",
            "Smiling face, spreading positivity!",
            "Serious face, focused and determined.",
            "Surprised face, something caught their attention!",
            "Confused face, deep in thought.",
            "Excited face, full of energy!",
            "Calm face, a picture of tranquility."
        ]

        result_image = detect_and_write_on_faces(input_image, texts_to_write)

        st.image(result_image, caption="Processed Image", use_column_width=True)

        # Allow the user to download the processed image as a JPEG file
        output_buffer = io.BytesIO()
        PilImage.fromarray(result_image).save(output_buffer, format="JPEG")
        st.markdown("### Download Processed Image")
        st.markdown(
            f"Download your processed image [here](data:file/jpeg;base64,{base64.b64encode(output_buffer.getvalue()).decode()})"
        )