Spaces:
Runtime error
Runtime error
themanas021
commited on
Commit
•
2058162
1
Parent(s):
0c0e6f5
Update app.py
Browse files
app.py
CHANGED
@@ -9,9 +9,7 @@ import base64
|
|
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 face, this person has a retention rate of 69%."
|
14 |
-
|
15 |
# Convert the uploaded image to grayscale for face detection
|
16 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
17 |
|
@@ -22,23 +20,37 @@ def detect_and_write_on_faces(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 |
-
|
|
|
|
|
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
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
st.image(result_image, caption="Processed Image", use_column_width=True)
|
44 |
|
|
|
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, texts_to_write):
|
|
|
|
|
13 |
# Convert the uploaded image to grayscale for face detection
|
14 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
15 |
|
|
|
20 |
pil_image = PilImage.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
21 |
draw = ImageDraw.Draw(pil_image)
|
22 |
|
23 |
+
for i, (x, y, w, h) in enumerate(faces):
|
24 |
# Write text on the detected face
|
25 |
+
if i < len(texts_to_write):
|
26 |
+
text_to_write = texts_to_write[i]
|
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 |
+
|
42 |
+
# Define different texts for each face
|
43 |
+
texts_to_write = [
|
44 |
+
"Happy face, this person has a retention rate of 69.",
|
45 |
+
"Smiling face, spreading positivity!",
|
46 |
+
"Serious face, focused and determined.",
|
47 |
+
"Surprised face, something caught their attention!",
|
48 |
+
"Confused face, deep in thought.",
|
49 |
+
"Excited face, full of energy!",
|
50 |
+
"Calm face, a picture of tranquility."
|
51 |
+
]
|
52 |
+
|
53 |
+
result_image = detect_and_write_on_faces(input_image, texts_to_write)
|
54 |
|
55 |
st.image(result_image, caption="Processed Image", use_column_width=True)
|
56 |
|