-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (52 loc) · 1.96 KB
/
main.py
File metadata and controls
78 lines (52 loc) · 1.96 KB
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
import firefox_downloader as fox_downloader
import twitter_scraping
import cv2
import argparse
import os
from unidecode import unidecode
args = argparse.ArgumentParser()
args.add_argument('-p', '--path', nargs='?', const='', default='query.txt')
FACE_CASCADE = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_default.xml')
def save_frame(frame, path, nameImage):
if not os.path.exists(path):
os.mkdir(path)
saved = cv2.imwrite(path + nameImage, frame)
if saved:
print("File Saved!", path + nameImage)
else:
print("File Save Status", saved, path + nameImage)
def video_read(cap, key):
count = 0
key = unidecode(key.replace(' ', '_'))
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = FACE_CASCADE.detectMultiScale(gray)
contains_face_in_image = False
for i, (x, y, w, h) in enumerate(faces):
tl = (x, y) # TOP LEFT
br = (x + w, y + h) # BOTTOM RIGHT
cv2.rectangle(frame, tl, br, (80, 47, 245), 2)
cv2.imshow("Video_face_cropped", frame[y:y + h, x:x + w])
save_frame(frame[y:y + h, x:x + w], "data/" + key + "_face_crop", "/frame_("+str(count)+")_face_" + str(i) + ".png")
contains_face_in_image = True
count += 1
if contains_face_in_image:
save_frame(frame, "data/" + key, "/frame_" + str(count) + ".png")
cv2.imshow("Video", frame)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
def main():
fox_downloader.install()
list_videos = twitter_scraping.start(args.parse_args().path)
for key in list_videos:
print(key)
cap = cv2.VideoCapture(list_videos[key])
video_read(cap, key)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()