Spaces:
Sleeping
Sleeping
File size: 5,779 Bytes
aea6c92 215339c aea6c92 8d61727 aea6c92 8d61727 aea6c92 29cb19e aea6c92 29cb19e aea6c92 29cb19e aea6c92 29cb19e cf85ac5 29cb19e cf85ac5 29cb19e 74def1e 35c6cce 29cb19e aea6c92 29cb19e 74def1e 22360ca 29cb19e 22360ca aea6c92 29cb19e aea6c92 29cb19e aea6c92 74def1e aea6c92 29cb19e aea6c92 d04b2ef 215339c d04b2ef aea6c92 d04b2ef aea6c92 d04b2ef aea6c92 d04b2ef 668c3c8 215339c d04b2ef 215339c aea6c92 d04b2ef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
import cv2
import cvzone
import numpy as np
import os
import gradio as gr
from datetime import datetime
# Load the YuNet model
model_path = 'face_detection_yunet_2023mar.onnx'
face_detector = cv2.FaceDetectorYN.create(model_path, "", (320, 320))
# Initialize the glass number
num = 1
overlay = cv2.imread(f'glasses/glass{num}.png', cv2.IMREAD_UNCHANGED)
# Count glasses files
def count_files_in_directory(directory):
file_count = 0
for root, dirs, files in os.walk(directory):
file_count += len(files)
return file_count
directory_path = 'glasses'
total_glass_num = count_files_in_directory(directory_path)
# Change glasses
def change_glasses():
global num, overlay
num += 1
if num > total_glass_num:
num = 1
overlay = cv2.imread(f'glasses/glass{num}.png', cv2.IMREAD_UNCHANGED)
return overlay
# Process frame for overlay
def process_frame(frame):
global overlay
# Ensure the frame is writable
frame = np.array(frame, copy=True)
height, width = frame.shape[:2]
face_detector.setInputSize((width, height))
_, faces = face_detector.detect(frame)
if faces is not None:
for face in faces:
x, y, w, h = face[:4].astype(int)
face_landmarks = face[4:14].reshape(5, 2).astype(int) # Facial landmarks
# Get the nose position
nose_x, nose_y = face_landmarks[2].astype(int)
# Left and right eye positions
left_eye_x, left_eye_y = face_landmarks[0].astype(int)
right_eye_x, right_eye_y = face_landmarks[1].astype(int)
# Calculate the midpoint between the eyes
eye_center_x = (left_eye_x + right_eye_x) // 2
eye_center_y = (left_eye_y + right_eye_y) // 2
# Calculate the angle of rotation
delta_x = right_eye_x - left_eye_x
delta_y = right_eye_y - left_eye_y
angle = np.degrees(np.arctan2(delta_y, delta_x))
# Negate the angle to rotate in the opposite direction
angle = -angle
# Resize the overlay
overlay_resize = cv2.resize(overlay, (int(w * 1.15), int(h * 0.8)))
# Rotate the overlay
overlay_center = (overlay_resize.shape[1] // 2, overlay_resize.shape[0] // 2)
rotation_matrix = cv2.getRotationMatrix2D(overlay_center, angle, 1.0)
overlay_rotated = cv2.warpAffine(
overlay_resize, rotation_matrix,
(overlay_resize.shape[1], overlay_resize.shape[0]),
flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)
)
# Calculate the position to center the glasses on the eyes
overlay_x = eye_center_x - overlay_rotated.shape[1] // 2
overlay_y = eye_center_y - overlay_rotated.shape[0] // 2
# Overlay the glasses
try:
frame = cvzone.overlayPNG(frame, overlay_rotated, [overlay_x, overlay_y])
except Exception as e:
print(f"Error overlaying glasses: {e}")
return frame
# Transform function
def transform_cv2(frame, transform):
if transform == "cartoon":
# prepare color
img_color = cv2.pyrDown(cv2.pyrDown(frame))
for _ in range(6):
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
img_color = cv2.pyrUp(cv2.pyrUp(img_color))
# prepare edges
img_edges = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
img_edges = cv2.adaptiveThreshold(
cv2.medianBlur(img_edges, 7),
255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
9,
2,
)
img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
# combine color and edges
img = cv2.bitwise_and(img_color, img_edges)
return img
elif transform == "edges":
# perform edge detection
img = cv2.cvtColor(cv2.Canny(frame, 100, 200), cv2.COLOR_GRAY2BGR)
return img
else:
return frame
def refresh_interface():
# # Reset the transformation dropdown to its default value
# transform.update(value="none")
# Reset the image to an empty state or a default image
input_img.update(value=None)
# Return a message indicating the interface has been refreshed
return "Interface refreshed!"
def save_frame(frame):
# Convert frame to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Create a unique filename using the current timestamp
filename = f"saved_frame_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
# Save the frame
cv2.imwrite(filename, frame)
# Refresh the interfaceq
refresh_interface()
return f"Frame saved as '{filename}'"
# Gradio webcam input
def webcam_input(frame, transform):
frame = process_frame(frame)
frame = transform_cv2(frame, transform)
return frame
# Gradio Interface
with gr.Blocks() as demo:
with gr.Column(elem_classes=["my-column"]):
with gr.Group(elem_classes=["my-group"]):
transform = gr.Dropdown(choices=["cartoon", "edges", "none"],
value="none", label="Transformation")
input_img = gr.Image(sources=["webcam"], type="numpy", streaming=True)
next_button = gr.Button("Next Glasses")
save_button = gr.Button("Save as a Picture")
input_img.stream(webcam_input, [input_img, transform], [input_img], time_limit=30, stream_every=0.1)
with gr.Row():
next_button.click(change_glasses, [], [])
with gr.Row():
save_button.click(save_frame, [input_img], [])
if __name__ == "__main__":
demo.launch(share=True)
|