Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,106 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import mediapipe as mp
|
| 3 |
-
from mediapipe.tasks import python
|
| 4 |
-
from mediapipe.tasks.python import vision
|
| 5 |
-
from mediapipe.framework.formats import landmark_pb2
|
| 6 |
-
import numpy as np
|
| 7 |
-
import gradio as gr
|
| 8 |
-
|
| 9 |
-
# Mediapipe FaceLandmarker seçeneklerini belirleyin
|
| 10 |
-
base_options = python.BaseOptions(model_asset_path='
|
| 11 |
-
options = vision.FaceLandmarkerOptions(
|
| 12 |
-
base_options=base_options,
|
| 13 |
-
output_face_blendshapes=True,
|
| 14 |
-
output_facial_transformation_matrixes=True,
|
| 15 |
-
num_faces=1
|
| 16 |
-
)
|
| 17 |
-
detector = vision.FaceLandmarker.create_from_options(options)
|
| 18 |
-
|
| 19 |
-
# Landmark noktalarını çizmek için fonksiyon
|
| 20 |
-
def draw_landmarks_on_image(rgb_image, detection_result):
|
| 21 |
-
face_landmarks_list = detection_result.face_landmarks
|
| 22 |
-
annotated_image = np.copy(rgb_image)
|
| 23 |
-
|
| 24 |
-
for idx in range(len(face_landmarks_list)):
|
| 25 |
-
face_landmarks = face_landmarks_list[idx]
|
| 26 |
-
face_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
|
| 27 |
-
face_landmarks_proto.landmark.extend([
|
| 28 |
-
landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in face_landmarks
|
| 29 |
-
])
|
| 30 |
-
|
| 31 |
-
mp.solutions.drawing_utils.draw_landmarks(
|
| 32 |
-
image=annotated_image,
|
| 33 |
-
landmark_list=face_landmarks_proto,
|
| 34 |
-
connections=mp.solutions.face_mesh.FACEMESH_TESSELATION,
|
| 35 |
-
landmark_drawing_spec=None,
|
| 36 |
-
connection_drawing_spec=mp.solutions.drawing_styles
|
| 37 |
-
.get_default_face_mesh_tesselation_style())
|
| 38 |
-
mp.solutions.drawing_utils.draw_landmarks(
|
| 39 |
-
image=annotated_image,
|
| 40 |
-
landmark_list=face_landmarks_proto,
|
| 41 |
-
connections=mp.solutions.face_mesh.FACEMESH_CONTOURS,
|
| 42 |
-
landmark_drawing_spec=None,
|
| 43 |
-
connection_drawing_spec=mp.solutions.drawing_styles
|
| 44 |
-
.get_default_face_mesh_contours_style())
|
| 45 |
-
mp.solutions.drawing_utils.draw_landmarks(
|
| 46 |
-
image=annotated_image,
|
| 47 |
-
landmark_list=face_landmarks_proto,
|
| 48 |
-
connections=mp.solutions.face_mesh.FACEMESH_IRISES,
|
| 49 |
-
landmark_drawing_spec=None,
|
| 50 |
-
connection_drawing_spec=mp.solutions.drawing_styles
|
| 51 |
-
.get_default_face_mesh_iris_connections_style())
|
| 52 |
-
return annotated_image
|
| 53 |
-
|
| 54 |
-
# Gradio için gerçek zamanlı video akışı işleme fonksiyonu
|
| 55 |
-
def process_frame(frame):
|
| 56 |
-
# OpenCV görüntüsünü Mediapipe formatına dönüştür
|
| 57 |
-
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 58 |
-
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb_frame)
|
| 59 |
-
|
| 60 |
-
# Yüz yer işaretlerini algıla
|
| 61 |
-
detection_result = detector.detect(mp_image)
|
| 62 |
-
|
| 63 |
-
# Çerçeveyi güncelle
|
| 64 |
-
if detection_result.face_blendshapes:
|
| 65 |
-
# İlk yüzün blendshape skorlarını al
|
| 66 |
-
face_blendshapes = detection_result.face_blendshapes[0]
|
| 67 |
-
|
| 68 |
-
# eyeBlinkLeft ve eyeBlinkRight blendshape skorlarını bul
|
| 69 |
-
blink_left = next((bs.score for bs in face_blendshapes if bs.category_name == "eyeBlinkLeft"), 0)
|
| 70 |
-
blink_right = next((bs.score for bs in face_blendshapes if bs.category_name == "eyeBlinkRight"), 0)
|
| 71 |
-
|
| 72 |
-
# Göz durumunu belirle
|
| 73 |
-
left_eye_status = "Kapalı" if blink_left > 0.5 else "Açık"
|
| 74 |
-
right_eye_status = "Kapalı" if blink_right > 0.5 else "Açık"
|
| 75 |
-
|
| 76 |
-
# Landmarkları çizin
|
| 77 |
-
annotated_image = draw_landmarks_on_image(rgb_frame, detection_result)
|
| 78 |
-
|
| 79 |
-
# # Çerçeveye göz durumunu yaz
|
| 80 |
-
# cv2.putText(annotated_image, f"Sol Goz: {left_eye_status}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 81 |
-
# cv2.putText(annotated_image, f"Sag Goz: {right_eye_status}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 82 |
-
|
| 83 |
-
return cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR), left_eye_status, right_eye_status
|
| 84 |
-
else:
|
| 85 |
-
return frame, "Göz Tespiti Yok", "Göz Tespiti Yok"
|
| 86 |
-
|
| 87 |
-
# Gradio arayüzü
|
| 88 |
-
def video_feed():
|
| 89 |
-
cap = cv2.VideoCapture(0)
|
| 90 |
-
while True:
|
| 91 |
-
success, frame = cap.read()
|
| 92 |
-
if not success:
|
| 93 |
-
break
|
| 94 |
-
|
| 95 |
-
frame, left_eye_status, right_eye_status = process_frame(frame)
|
| 96 |
-
yield frame, left_eye_status, right_eye_status
|
| 97 |
-
|
| 98 |
-
iface = gr.Interface(fn=video_feed,
|
| 99 |
-
inputs=None, # Giriş yok, sadece video akışı
|
| 100 |
-
outputs=[gr.Image(type="numpy", label="Yüz Tespiti Sonucu"),
|
| 101 |
-
gr.Textbox(label="Sol Göz Durumu"),
|
| 102 |
-
gr.Textbox(label="Sağ Göz Durumu")],
|
| 103 |
-
live=True)
|
| 104 |
-
|
| 105 |
-
# Gradio arayüzünü başlat
|
| 106 |
-
iface.launch(share=True)
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
from mediapipe.tasks import python
|
| 4 |
+
from mediapipe.tasks.python import vision
|
| 5 |
+
from mediapipe.framework.formats import landmark_pb2
|
| 6 |
+
import numpy as np
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# Mediapipe FaceLandmarker seçeneklerini belirleyin
|
| 10 |
+
base_options = python.BaseOptions(model_asset_path='face_landmarker.task')
|
| 11 |
+
options = vision.FaceLandmarkerOptions(
|
| 12 |
+
base_options=base_options,
|
| 13 |
+
output_face_blendshapes=True,
|
| 14 |
+
output_facial_transformation_matrixes=True,
|
| 15 |
+
num_faces=1
|
| 16 |
+
)
|
| 17 |
+
detector = vision.FaceLandmarker.create_from_options(options)
|
| 18 |
+
|
| 19 |
+
# Landmark noktalarını çizmek için fonksiyon
|
| 20 |
+
def draw_landmarks_on_image(rgb_image, detection_result):
|
| 21 |
+
face_landmarks_list = detection_result.face_landmarks
|
| 22 |
+
annotated_image = np.copy(rgb_image)
|
| 23 |
+
|
| 24 |
+
for idx in range(len(face_landmarks_list)):
|
| 25 |
+
face_landmarks = face_landmarks_list[idx]
|
| 26 |
+
face_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
|
| 27 |
+
face_landmarks_proto.landmark.extend([
|
| 28 |
+
landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in face_landmarks
|
| 29 |
+
])
|
| 30 |
+
|
| 31 |
+
mp.solutions.drawing_utils.draw_landmarks(
|
| 32 |
+
image=annotated_image,
|
| 33 |
+
landmark_list=face_landmarks_proto,
|
| 34 |
+
connections=mp.solutions.face_mesh.FACEMESH_TESSELATION,
|
| 35 |
+
landmark_drawing_spec=None,
|
| 36 |
+
connection_drawing_spec=mp.solutions.drawing_styles
|
| 37 |
+
.get_default_face_mesh_tesselation_style())
|
| 38 |
+
mp.solutions.drawing_utils.draw_landmarks(
|
| 39 |
+
image=annotated_image,
|
| 40 |
+
landmark_list=face_landmarks_proto,
|
| 41 |
+
connections=mp.solutions.face_mesh.FACEMESH_CONTOURS,
|
| 42 |
+
landmark_drawing_spec=None,
|
| 43 |
+
connection_drawing_spec=mp.solutions.drawing_styles
|
| 44 |
+
.get_default_face_mesh_contours_style())
|
| 45 |
+
mp.solutions.drawing_utils.draw_landmarks(
|
| 46 |
+
image=annotated_image,
|
| 47 |
+
landmark_list=face_landmarks_proto,
|
| 48 |
+
connections=mp.solutions.face_mesh.FACEMESH_IRISES,
|
| 49 |
+
landmark_drawing_spec=None,
|
| 50 |
+
connection_drawing_spec=mp.solutions.drawing_styles
|
| 51 |
+
.get_default_face_mesh_iris_connections_style())
|
| 52 |
+
return annotated_image
|
| 53 |
+
|
| 54 |
+
# Gradio için gerçek zamanlı video akışı işleme fonksiyonu
|
| 55 |
+
def process_frame(frame):
|
| 56 |
+
# OpenCV görüntüsünü Mediapipe formatına dönüştür
|
| 57 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 58 |
+
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb_frame)
|
| 59 |
+
|
| 60 |
+
# Yüz yer işaretlerini algıla
|
| 61 |
+
detection_result = detector.detect(mp_image)
|
| 62 |
+
|
| 63 |
+
# Çerçeveyi güncelle
|
| 64 |
+
if detection_result.face_blendshapes:
|
| 65 |
+
# İlk yüzün blendshape skorlarını al
|
| 66 |
+
face_blendshapes = detection_result.face_blendshapes[0]
|
| 67 |
+
|
| 68 |
+
# eyeBlinkLeft ve eyeBlinkRight blendshape skorlarını bul
|
| 69 |
+
blink_left = next((bs.score for bs in face_blendshapes if bs.category_name == "eyeBlinkLeft"), 0)
|
| 70 |
+
blink_right = next((bs.score for bs in face_blendshapes if bs.category_name == "eyeBlinkRight"), 0)
|
| 71 |
+
|
| 72 |
+
# Göz durumunu belirle
|
| 73 |
+
left_eye_status = "Kapalı" if blink_left > 0.5 else "Açık"
|
| 74 |
+
right_eye_status = "Kapalı" if blink_right > 0.5 else "Açık"
|
| 75 |
+
|
| 76 |
+
# Landmarkları çizin
|
| 77 |
+
annotated_image = draw_landmarks_on_image(rgb_frame, detection_result)
|
| 78 |
+
|
| 79 |
+
# # Çerçeveye göz durumunu yaz
|
| 80 |
+
# cv2.putText(annotated_image, f"Sol Goz: {left_eye_status}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 81 |
+
# cv2.putText(annotated_image, f"Sag Goz: {right_eye_status}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 82 |
+
|
| 83 |
+
return cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR), left_eye_status, right_eye_status
|
| 84 |
+
else:
|
| 85 |
+
return frame, "Göz Tespiti Yok", "Göz Tespiti Yok"
|
| 86 |
+
|
| 87 |
+
# Gradio arayüzü
|
| 88 |
+
def video_feed():
|
| 89 |
+
cap = cv2.VideoCapture(0)
|
| 90 |
+
while True:
|
| 91 |
+
success, frame = cap.read()
|
| 92 |
+
if not success:
|
| 93 |
+
break
|
| 94 |
+
|
| 95 |
+
frame, left_eye_status, right_eye_status = process_frame(frame)
|
| 96 |
+
yield frame, left_eye_status, right_eye_status
|
| 97 |
+
|
| 98 |
+
iface = gr.Interface(fn=video_feed,
|
| 99 |
+
inputs=None, # Giriş yok, sadece video akışı
|
| 100 |
+
outputs=[gr.Image(type="numpy", label="Yüz Tespiti Sonucu"),
|
| 101 |
+
gr.Textbox(label="Sol Göz Durumu"),
|
| 102 |
+
gr.Textbox(label="Sağ Göz Durumu")],
|
| 103 |
+
live=True)
|
| 104 |
+
|
| 105 |
+
# Gradio arayüzünü başlat
|
| 106 |
+
iface.launch(share=True)
|