View difference between Paste ID: 3UG6mUHQ and p4dfFkPA
SHOW: | | - or go back to the newest paste.
1
import face_recognition
2
import cv2
3
import os
4
from PIL import Image
5
import numpy as np
6
7
8
# Tune this this to memory.
9
BATCH_FRAMES = 25
10
11
def process_video(vid):
12
    print(f"Processing: {vid}")
13
    basename, _ = os.path.splitext(os.path.basename(vid))
14
    video_capture = cv2.VideoCapture(vid)
15
    faces = 0
16
    frames = []
17
    while video_capture.isOpened():
18
        # Grab a single frame of video
19
        ret, frame_cv = video_capture.read()
20
21
        # Bail out when the video file ends
22
        if not ret:
23
            break
24
25
        frame = cv2.cvtColor(frame_cv, cv2.COLOR_BGR2RGB)
26
        frames.append(frame)
27
        print(".", end="", flush=""),
28
		# Tune this to GPU Memory Size.
29
        if len(frames) == BATCH_FRAMES:
30
            print("#", end="", flush="")
31
            batch_of_face_locations = face_recognition.batch_face_locations(
32
                frames, number_of_times_to_upsample=0
33
            )
34
35
            for idx,face_locations in enumerate(batch_of_face_locations):
36
                if len(face_locations)==0:
37
                    continue
38
                
39
                face_encodings = face_recognition.face_encodings(
40
                    face_image=frames[idx],
41
                    known_face_locations=face_locations
42
                )
43
                for face_encoding, face_location in zip(face_encodings, face_locations):
44
                    print("$", end="", flush=True)
45
                    top, right, bottom, left = face_location
46
47
                    buffer = 0.5
48
                    max_bottom, max_right, _ = frame.shape
49
                    width = right - left
50
                    height = bottom - top
51
                    top2 = int(max([0, top - height * buffer]))
52
                    left2 = int(max([0, left - width * buffer]))
53
54
                    bottom2 = int(min([max_bottom, bottom + height * buffer]))
55
                    right2 = int(min([max_right, right + width * buffer]))
56
57
                    # You can access the actual face itself like this:
58
                    face_image = frame[top2:bottom2, left2:right2]
59
                    pil_image = Image.fromarray(face_image)
60
61
                    pil_image.save(f"faces/{basename}-{faces:08d}.jpg")
62
63
                    with open(f"faces/{basename}-{faces:08d}.np", "wb") as b:
64
                        np.save(b, face_encoding, allow_pickle=False, fix_imports=False)
65
66
                    faces += 1
67
            print("", end="\n", flush=True)
68
            frames = []
69
            
70
    print("#", end="", flush="")
71
    batch_of_face_locations = face_recognition.batch_face_locations(
72
        frames, number_of_times_to_upsample=0
73
    )
74
75
    for idx,face_locations in enumerate(batch_of_face_locations):
76
        if len(face_locations)==0:
77
            continue
78
        
79
        face_encodings = face_recognition.face_encodings(
80
            face_image=frames[idx],
81
            known_face_locations=face_locations
82
        )
83
        for face_encoding, face_location in zip(face_encodings, face_locations):
84
            print("$", end="", flush=True)
85
            top, right, bottom, left = face_location
86
87
            buffer = 0.1
88
            max_bottom, max_right, _ = frame.shape
89
            width = right - left
90
            height = bottom - top
91
            top2 = int(max([0, top - height * buffer]))
92
            left2 = int(max([0, left - width * buffer]))
93
94
            bottom2 = int(min([max_bottom, bottom + height * buffer]))
95
            right2 = int(min([max_right, right + width * buffer]))
96
97
            # You can access the actual face itself like this:
98
            face_image = frame[top2:bottom2, left2:right2]
99
            pil_image = Image.fromarray(face_image)
100
101
            pil_image.save(f"faces/{basename}-{faces:08d}.jpg")
102
103
            with open(f"faces/{basename}-{faces:08d}.np", "wb") as b:
104
                np.save(b, face_encoding, allow_pickle=False, fix_imports=False)
105
            faces += 1
106
107
# Open video file
108
import glob
109
110
vids = glob.glob("*.mp4")
111
for vid in vids:
112
    process_video(vid)
113