-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.opencv_deepface.py
67 lines (51 loc) · 1.9 KB
/
2.opencv_deepface.py
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
import cv2
from deepface import DeepFace
import os
import pandas as pd
from cv2 import VideoCapture
from cv2 import waitKey
data = {
"Age":[],
"Gender":[]
}
'''
img = cv2.imread("/make/test/111.jpg")
result = DeepFace.analyze(img, actions=("gender","age"))
data["Gender"].append(result[0]["dominant_gender"])
#df = pd.DataFrame(data)
print(result[0]["dominant_gender"])
'''
# opencv python 코딩 기본 틀
# 카메라 영상을 받아올 객체 선언 및 설정(영상 소스, 해상도 설정)
videosource = "/make/test/final.mp4"
txt_path = videosource
face_cascade = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_default.xml')
capture = cv2.VideoCapture(videosource)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
if capture.isOpened():
while True:
ret, frame = capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor= 1.2, minNeighbors=20, minSize=(20,20))
if ret:
cnt = 0
age = 0
male = 0
female = 0
if len(faces) :
for x, y, w, h in faces :
cnt+=1
result = DeepFace.analyze(frame, actions=("gender","age"))
age+=result[0]["age"]
if(result[0]["dominant_gender"]=='Man'):
male+=1
else:
female+=1
with open(f'{txt_path}.txt', 'a') as f:
f.write(('%g %g %g %g') % (int(cnt) , int(male), int(female) ,int(age) )+ '\n')
cv2.waitKey(10)
else:
break
capture.release()
cv2.destroyAllWindows()