Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,30 @@
|
|
| 1 |
-
from flask import Flask, request, render_template
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
-
from mtcnn import MTCNN
|
|
|
|
| 7 |
|
| 8 |
-
app = Flask(__name__)
|
| 9 |
detector = MTCNN()
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
nparr = np.frombuffer(image_bytes, np.uint8)
|
| 14 |
-
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 15 |
-
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 16 |
faces = detector.detect_faces(img_rgb)
|
| 17 |
-
|
| 18 |
if faces:
|
| 19 |
for face in faces:
|
| 20 |
bounding_box = face['box']
|
| 21 |
keypoints = face['keypoints']
|
| 22 |
-
|
| 23 |
-
cv2.rectangle(img,
|
| 24 |
(bounding_box[0], bounding_box[1]),
|
| 25 |
(bounding_box[0] + bounding_box[2], bounding_box[1] + bounding_box[3]),
|
| 26 |
(0, 255, 0),
|
| 27 |
2)
|
| 28 |
-
|
| 29 |
for key, value in keypoints.items():
|
| 30 |
-
cv2.circle(
|
| 31 |
-
|
| 32 |
-
# Convert the processed image back to bytes for display
|
| 33 |
-
img_pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 34 |
-
img_buffer = io.BytesIO()
|
| 35 |
-
img_pil.save(img_buffer, format="JPEG")
|
| 36 |
-
return img_buffer.getvalue()
|
| 37 |
-
else:
|
| 38 |
-
return None
|
| 39 |
-
|
| 40 |
-
@app.route('/', methods=['GET', 'POST'])
|
| 41 |
-
def upload_file():
|
| 42 |
-
if request.method == 'POST':
|
| 43 |
-
if 'file' not in request.files:
|
| 44 |
-
return render_template('index.html', error='No file part')
|
| 45 |
-
file = request.files['file']
|
| 46 |
-
if file.filename == '':
|
| 47 |
-
return render_template('index.html', error='No selected file')
|
| 48 |
-
if file:
|
| 49 |
-
image_bytes = file.read()
|
| 50 |
-
processed_image_bytes = detect_faces_and_draw(image_bytes)
|
| 51 |
-
if processed_image_bytes:
|
| 52 |
-
return render_template('index.html', result=True, image_data=f"data:image/jpeg;base64,{processed_image_bytes.hex()}")
|
| 53 |
-
else:
|
| 54 |
-
return render_template('index.html', error='No faces detected')
|
| 55 |
-
return render_template('index.html')
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
+
from mtcnn import MTCNN
|
| 6 |
+
import gradio as gr
|
| 7 |
|
|
|
|
| 8 |
detector = MTCNN()
|
| 9 |
|
| 10 |
+
def detect_faces_gradio(image):
|
| 11 |
+
img_rgb = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
|
|
|
|
| 12 |
faces = detector.detect_faces(img_rgb)
|
| 13 |
+
processed_img = np.array(image).copy()
|
| 14 |
if faces:
|
| 15 |
for face in faces:
|
| 16 |
bounding_box = face['box']
|
| 17 |
keypoints = face['keypoints']
|
| 18 |
+
cv2.rectangle(processed_img,
|
|
|
|
| 19 |
(bounding_box[0], bounding_box[1]),
|
| 20 |
(bounding_box[0] + bounding_box[2], bounding_box[1] + bounding_box[3]),
|
| 21 |
(0, 255, 0),
|
| 22 |
2)
|
|
|
|
| 23 |
for key, value in keypoints.items():
|
| 24 |
+
cv2.circle(processed_img, (int(value[0]), int(value[1])), 2, (0, 0, 255), 2)
|
| 25 |
+
return processed_img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
iface = gr.Interface(fn=detect_faces_gradio, inputs=gr.Image(type="pil"), outputs="image",
|
| 28 |
+
title="MTCNN Face Detection",
|
| 29 |
+
description="Upload an image and see the detected faces and landmarks.")
|
| 30 |
+
iface.launch()
|