Siyun He commited on
Commit
aea6c92
·
1 Parent(s): a3446c5
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import cvzone
3
+ import numpy as np
4
+ import os
5
+ import gradio as gr
6
+
7
+ # Load the YuNet model
8
+ model_path = 'face_detection_yunet_2023mar.onnx'
9
+ face_detector = cv2.FaceDetectorYN.create(model_path, "", (320, 320))
10
+
11
+ # Initialize the glass number
12
+ num = 1
13
+ overlay = cv2.imread(f'glasses/glass{num}.png', cv2.IMREAD_UNCHANGED)
14
+
15
+ # Count glasses files
16
+ def count_files_in_directory(directory):
17
+ file_count = 0
18
+ for root, dirs, files in os.walk(directory):
19
+ file_count += len(files)
20
+ return file_count
21
+
22
+ directory_path = 'glasses'
23
+ total_glass_num = count_files_in_directory(directory_path)
24
+
25
+ # Change glasses
26
+ def change_glasses(action):
27
+ global num, overlay
28
+ if action == 'next':
29
+ num += 1
30
+ if num > total_glass_num:
31
+ num = 1
32
+ overlay = cv2.imread(f'glasses/glass{num}.png', cv2.IMREAD_UNCHANGED)
33
+ return overlay
34
+
35
+ # Process frame for overlay
36
+ def process_frame(frame):
37
+ global overlay
38
+ height, width = frame.shape[:2]
39
+ face_detector.setInputSize((width, height))
40
+ _, faces = face_detector.detect(frame)
41
+
42
+ if faces is not None:
43
+ for face in faces:
44
+ x, y, w, h = face[:4].astype(int)
45
+ face_landmarks = face[4:14].reshape(5, 2).astype(int) # Facial landmarks
46
+
47
+ # Get the nose position (assuming landmark index 2 is the nose)
48
+ nose_x, nose_y = face_landmarks[2].astype(int)
49
+
50
+ # Resize the overlay
51
+ overlay_resize = cv2.resize(overlay, (int(w * 1.15), int(h * 0.8)))
52
+
53
+ # Ensure the frame is a NumPy array and writable
54
+ frame = np.array(frame)
55
+
56
+ # Calculate the position to center the glasses on the nose
57
+ overlay_x = nose_x - overlay_resize.shape[1] // 2
58
+ overlay_y = y # Adjust this if needed for better vertical alignment
59
+
60
+ # Overlay the glasses
61
+ try:
62
+ frame = cvzone.overlayPNG(frame, overlay_resize, [overlay_x, overlay_y])
63
+ except Exception as e:
64
+ print(f"Error overlaying glasses: {e}")
65
+
66
+ return frame
67
+
68
+ # # Display the frame
69
+ # def display_frame():
70
+ # cap = cv2.VideoCapture(0)
71
+ # while True:
72
+ # ret, frame = cap.read()
73
+ # if not ret:
74
+ # break
75
+
76
+ # frame = process_frame(frame)
77
+ # cv2.imshow('SnapLens', frame)
78
+
79
+ # k = cv2.waitKey(10)
80
+ # if k == ord('q'):
81
+ # break
82
+
83
+ # cap.release()
84
+ # cv2.destroyAllWindows()
85
+
86
+ # display_frame()
87
+
88
+ # Gradio webcam input
89
+ def webcam_input(frame):
90
+ frame = process_frame(frame)
91
+ return frame
92
+
93
+ # Gradio Interface
94
+ with gr.Blocks() as demo:
95
+ with gr.Row():
96
+ with gr.Column():
97
+ input_img = gr.Image(label="Input", sources="webcam", streaming=True)
98
+ input_img.stream(webcam_input, [input_img], [input_img], time_limit=15, stream_every=0.1, concurrency_limit=30)
99
+ if __name__ == "__main__":
100
+ demo.launch(share=True)
face_detection_yunet_2023mar.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8f2383e4dd3cfbb4553ea8718107fc0423210dc964f9f4280604804ed2552fa4
3
+ size 232589
glasses/glass1.png ADDED
glasses/glass2.png ADDED
glasses/glass3.png ADDED
glasses/glass4.png ADDED
glasses/glass5.png ADDED
glasses/glass6.png ADDED
glasses/glass7.png ADDED
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ cvzone
3
+ opencv-python
4
+ numpy