Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,31 @@
|
|
| 1 |
-
import spaces
|
| 2 |
import base64
|
| 3 |
-
import cv2
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
from io import BytesIO
|
| 8 |
|
| 9 |
-
@spaces.GPU
|
| 10 |
def crop_face(base64_image):
|
| 11 |
try:
|
| 12 |
-
# Decode the base64 image
|
| 13 |
img_data = base64.b64decode(base64_image)
|
| 14 |
np_arr = np.frombuffer(img_data, np.uint8)
|
| 15 |
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 16 |
|
| 17 |
if image is None:
|
| 18 |
-
return "
|
| 19 |
-
|
| 20 |
# Load the pre-trained face detector
|
| 21 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 22 |
|
| 23 |
-
# Convert the image to grayscale
|
| 24 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 25 |
-
|
| 26 |
-
# Detect faces in the image
|
| 27 |
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 28 |
|
| 29 |
-
# If no faces are detected, return message
|
| 30 |
if len(faces) == 0:
|
| 31 |
-
return "No faces
|
| 32 |
|
| 33 |
-
# Crop the first face
|
| 34 |
x, y, w, h = faces[0]
|
| 35 |
face_crop = image[y:y+h, x:x+w]
|
| 36 |
|
|
@@ -44,21 +39,18 @@ def crop_face(base64_image):
|
|
| 44 |
return f"An error occurred: {str(e)}"
|
| 45 |
|
| 46 |
def image_to_base64(image):
|
| 47 |
-
|
| 48 |
-
buffered = io.BytesIO()
|
| 49 |
image.save(buffered, format="JPEG")
|
| 50 |
-
# Encode bytes to Base64 string
|
| 51 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 52 |
return img_str
|
| 53 |
|
| 54 |
-
|
| 55 |
-
# Define the Gradio interface using the updated syntax
|
| 56 |
base64_converter_interface = gr.Interface(
|
| 57 |
fn=image_to_base64,
|
| 58 |
inputs=gr.Image(type="pil"),
|
| 59 |
outputs=gr.Textbox(),
|
| 60 |
title="Image to Base64 Encoder",
|
| 61 |
-
description="Upload an image
|
| 62 |
)
|
| 63 |
|
| 64 |
face_crop_interface = gr.Interface(
|
|
@@ -70,4 +62,4 @@ face_crop_interface = gr.Interface(
|
|
| 70 |
)
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
gr.TabbedInterface([base64_converter_interface, face_crop_interface], ["Convert to Base64","Crop Face"]).launch()
|
|
|
|
|
|
|
| 1 |
import base64
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
|
|
|
| 8 |
def crop_face(base64_image):
|
| 9 |
try:
|
| 10 |
+
# Decode the base64 image to an OpenCV format
|
| 11 |
img_data = base64.b64decode(base64_image)
|
| 12 |
np_arr = np.frombuffer(img_data, np.uint8)
|
| 13 |
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 14 |
|
| 15 |
if image is None:
|
| 16 |
+
return "Image decoding failed. Check the input format."
|
| 17 |
+
|
| 18 |
# Load the pre-trained face detector
|
| 19 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 20 |
|
| 21 |
+
# Convert the image to grayscale for face detection
|
| 22 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
| 23 |
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 24 |
|
|
|
|
| 25 |
if len(faces) == 0:
|
| 26 |
+
return "No faces detected in the image."
|
| 27 |
|
| 28 |
+
# Crop the first detected face
|
| 29 |
x, y, w, h = faces[0]
|
| 30 |
face_crop = image[y:y+h, x:x+w]
|
| 31 |
|
|
|
|
| 39 |
return f"An error occurred: {str(e)}"
|
| 40 |
|
| 41 |
def image_to_base64(image):
|
| 42 |
+
buffered = BytesIO()
|
|
|
|
| 43 |
image.save(buffered, format="JPEG")
|
|
|
|
| 44 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 45 |
return img_str
|
| 46 |
|
| 47 |
+
# Define the Gradio interfaces
|
|
|
|
| 48 |
base64_converter_interface = gr.Interface(
|
| 49 |
fn=image_to_base64,
|
| 50 |
inputs=gr.Image(type="pil"),
|
| 51 |
outputs=gr.Textbox(),
|
| 52 |
title="Image to Base64 Encoder",
|
| 53 |
+
description="Upload an image to convert it to a base64 encoded string."
|
| 54 |
)
|
| 55 |
|
| 56 |
face_crop_interface = gr.Interface(
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
gr.TabbedInterface([base64_converter_interface, face_crop_interface], ["Convert to Base64", "Crop Face"]).launch(share=True)
|