themanas021 commited on
Commit
2c87ef6
1 Parent(s): f127d58

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ from PIL import Image as PilImage
4
+ from PIL import ImageDraw
5
+ import numpy as np
6
+ import io
7
+ import base64
8
+
9
+ # Load the face detection classifier
10
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
11
+
12
+ def detect_and_write_on_faces(image):
13
+ text_to_write = "Happy faces, this person has a retention rate of 69% in a balanced way."
14
+
15
+ # Convert the uploaded image to grayscale for face detection
16
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
17
+
18
+ # Detect faces in the image
19
+ faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
20
+
21
+ # Convert the OpenCV image to a Pillow image
22
+ pil_image = PilImage.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
23
+ draw = ImageDraw.Draw(pil_image)
24
+
25
+ for (x, y, w, h) in faces:
26
+ # Write text on the detected face
27
+ draw.text((x, y - 10), text_to_write, fill=(255, 0, 0, 0))
28
+
29
+ # Convert the Pillow image back to OpenCV format
30
+ image_with_text = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
31
+
32
+ return image_with_text
33
+
34
+ st.title("Face Detection and Text Writing")
35
+
36
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
37
+
38
+ if uploaded_image is not None:
39
+ if st.button("Process Image"):
40
+ input_image = cv2.imdecode(np.frombuffer(uploaded_image.read(), np.uint8), -1)
41
+ result_image = detect_and_write_on_faces(input_image)
42
+
43
+ st.image(result_image, caption="Processed Image", use_column_width=True)
44
+
45
+ # Allow the user to download the processed image as a JPEG file
46
+ output_buffer = io.BytesIO()
47
+ PilImage.fromarray(result_image).save(output_buffer, format="JPEG")
48
+ st.markdown("### Download Processed Image")
49
+ st.markdown(
50
+ f"Download your processed image [here](data:file/jpeg;base64,{base64.b64encode(output_buffer.getvalue()).decode()})"
51
+ )