Showing
7 changed files
with
0 additions
and
268 deletions
1 | -import dlib, cv2 | ||
2 | -import numpy as np | ||
3 | - | ||
4 | -detector = dlib.get_frontal_face_detector() | ||
5 | -sp = dlib.shape_predictor('models/shape_predictor_68_face_landmarks.dat') | ||
6 | -facerec = dlib.face_recognition_model_v1('models/dlib_face_recognition_resnet_model_v1.dat') | ||
7 | - | ||
8 | -def read_img(img_path): | ||
9 | - img = cv2.imread(img_path) | ||
10 | - img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | ||
11 | - return img | ||
12 | - | ||
13 | -def encode_face(img): | ||
14 | - dets = detector(img, 1) | ||
15 | - | ||
16 | - if len(dets) == 0: | ||
17 | - return np.empty(0) | ||
18 | - | ||
19 | - for k, d in enumerate(dets): | ||
20 | - # print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(k, d.left(), d.top(), d.right(), d.bottom())) | ||
21 | - shape = sp(img, d) | ||
22 | - face_descriptor = facerec.compute_face_descriptor(img, shape) | ||
23 | - | ||
24 | - return np.array(face_descriptor) | ||
25 | - | ||
26 | -# main | ||
27 | -img1_path = './img/matrix5.jpg' # iu | ||
28 | - | ||
29 | -# img2_path = '/Users/visualcamp/Pictures/00502318_20180518.JPG' # suz | ||
30 | -# img2_path = '/Users/visualcamp/Pictures/660190_v9_ba.jpg' # suz | ||
31 | - | ||
32 | -# img2_path = '/Users/visualcamp/Development/tf/GazeCapture/dataset/processed/00002/frames/00000.jpg' | ||
33 | -# img1_path = '/Users/visualcamp/Development/tf/GazeCapture/dataset/processed/03523/frames/02190.jpg' | ||
34 | -# img2_path = '/Users/visualcamp/Development/tf/GazeCapture/dataset/processed/03523/frames/00000.jpg' | ||
35 | -# img2_path = '/Users/visualcamp/Development/tf/GazeCapture/dataset/processed/02534/frames/00005.jpg' | ||
36 | - | ||
37 | -img1_path = './img/matrix5.jpg' # me | ||
38 | -img1 = read_img(img1_path) | ||
39 | -img1_encoded = encode_face(img1) | ||
40 | -# img2 = read_img(img2_path) | ||
41 | - | ||
42 | -cap = cv2.VideoCapture(0) | ||
43 | - | ||
44 | -if not cap.isOpened(): | ||
45 | - exit() | ||
46 | - | ||
47 | -while True: | ||
48 | - ret, img2 = cap.read() | ||
49 | - if not ret: | ||
50 | - break | ||
51 | - | ||
52 | - img2 = cv2.resize(img2, (640, img2.shape[0] * 640 // img2.shape[1])) | ||
53 | - img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) | ||
54 | - img2_encoded = encode_face(img2) | ||
55 | - | ||
56 | - if len(img2_encoded) == 0: | ||
57 | - continue | ||
58 | - | ||
59 | - dist = np.linalg.norm(img1_encoded - img2_encoded, axis=0) | ||
60 | - | ||
61 | - print('%s, Distance: %s' % (dist < 0.6, dist)) |
1 | -import dlib, cv2 | ||
2 | -import numpy as np | ||
3 | - | ||
4 | -detector = dlib.get_frontal_face_detector() | ||
5 | -sp = dlib.shape_predictor('models/shape_predictor_68_face_landmarks.dat') | ||
6 | -facerec = dlib.face_recognition_model_v1('models/dlib_face_recognition_resnet_model_v1.dat') | ||
7 | - | ||
8 | -descs = np.load('img/descs.npy', allow_pickle=True)[()] | ||
9 | - | ||
10 | -def encode_face(img): | ||
11 | - dets = detector(img, 1) | ||
12 | - | ||
13 | - if len(dets) == 0: | ||
14 | - return np.empty(0) | ||
15 | - | ||
16 | - for k, d in enumerate(dets): | ||
17 | - shape = sp(img, d) | ||
18 | - face_descriptor = facerec.compute_face_descriptor(img, shape) | ||
19 | - | ||
20 | - return np.array(face_descriptor) | ||
21 | - | ||
22 | -video_path = './data/record0.mp4' | ||
23 | -cap = cv2.VideoCapture(video_path) | ||
24 | - | ||
25 | -if not cap.isOpened(): | ||
26 | - exit() | ||
27 | - | ||
28 | -_, img_bgr = cap.read() # (800, 1920, 3) | ||
29 | -padding_size = 0 | ||
30 | -resized_width = 1920 | ||
31 | -video_size = (resized_width, int(img_bgr.shape[0] * resized_width // img_bgr.shape[1])) | ||
32 | -output_size = (resized_width, int(img_bgr.shape[0] * resized_width // img_bgr.shape[1] + padding_size * 2)) | ||
33 | - | ||
34 | -fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') | ||
35 | -writer = cv2.VideoWriter('%s_output.mp4' % (video_path.split('.')[0]), fourcc, cap.get(cv2.CAP_PROP_FPS), output_size) | ||
36 | - | ||
37 | -while True: | ||
38 | - ret, img_bgr = cap.read() | ||
39 | - if not ret: | ||
40 | - break | ||
41 | - | ||
42 | - img_bgr = cv2.resize(img_bgr, video_size) | ||
43 | - img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) | ||
44 | - | ||
45 | - # img_bgr = cv2.copyMakeBorder(img_bgr, top=padding_size, bottom=padding_size, left=0, right=0, borderType=cv2.BORDER_CONSTANT, value=(0,0,0)) | ||
46 | - | ||
47 | - dets = detector(img_bgr, 1) | ||
48 | - | ||
49 | - for k, d in enumerate(dets): | ||
50 | - shape = sp(img_rgb, d) | ||
51 | - face_descriptor = facerec.compute_face_descriptor(img_rgb, shape) | ||
52 | - | ||
53 | - last_found = {'name': 'unknown', 'dist': 0.6, 'color': (0,0,255)} | ||
54 | - | ||
55 | - for name, saved_desc in descs.items(): | ||
56 | - dist = np.linalg.norm([face_descriptor] - saved_desc, axis=1) | ||
57 | - | ||
58 | - if dist < last_found['dist']: | ||
59 | - last_found = {'name': name, 'dist': dist, 'color': (255,255,255)} | ||
60 | - | ||
61 | - cv2.rectangle(img_bgr, pt1=(d.left(), d.top()), pt2=(d.right(), d.bottom()), color=last_found['color'], thickness=2) | ||
62 | - cv2.putText(img_bgr, last_found['name'], org=(d.left(), d.top()), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=last_found['color'], thickness=2) | ||
63 | - | ||
64 | - writer.write(img_bgr) | ||
65 | - | ||
66 | - cv2.imshow('img', img_bgr) | ||
67 | - | ||
68 | - if cv2.waitKey(1) == ord('q'): | ||
69 | - break | ||
70 | - | ||
71 | -cap.release() | ||
72 | -writer.release() |
1 | -import dlib | ||
2 | -import cv2 | ||
3 | -import numpy as np | ||
4 | -import matplotlib.pyplot as plt | ||
5 | -import tensorflow as tf | ||
6 | -import math | ||
7 | -import os | ||
8 | -import pathlib | ||
9 | -import time | ||
10 | -import pandas as pd | ||
11 | -import tensorflow as tf | ||
12 | -from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img | ||
13 | -from tensorflow.keras.models import load_model | ||
14 | -from tensorflow.keras import regularizers | ||
15 | -from tensorflow import keras | ||
16 | -import time | ||
17 | - | ||
18 | - | ||
19 | -start = time.time() | ||
20 | -detector = dlib.get_frontal_face_detector() | ||
21 | -predictor = dlib.shape_predictor( | ||
22 | - "./models/shape_predictor_68_face_landmarks.dat") | ||
23 | -facerec = dlib.face_recognition_model_v1( | ||
24 | - 'models/dlib_face_recognition_resnet_model_v1.dat') | ||
25 | -model = load_model('../checkpoint/er-best-efficientNet1-bt32-model-SGD.h5') | ||
26 | - | ||
27 | - | ||
28 | -descs = np.load('img/descs2.npy', allow_pickle=True)[()] | ||
29 | - | ||
30 | -video_path = './data/zoom_1.mp4' | ||
31 | -cap = cv2.VideoCapture(video_path) | ||
32 | - | ||
33 | - | ||
34 | -# labels_dict_ = {0 : 'angry', 1 : 'fear' , 2: 'happy', 3: 'neutral', 4: 'sad', 5: 'surprise'} | ||
35 | -labels_dict_ = {'angry': 0, 'fear': 1, 'happy': 2, | ||
36 | - 'neutral': 3, 'sad': 4, 'surprise': 5} | ||
37 | -time_dict = {'angry': [], 'fear': [], 'happy': [], | ||
38 | - 'neutral': [], 'sad': [], 'surprise': []} | ||
39 | - | ||
40 | - | ||
41 | -def get_key(val): | ||
42 | - for key, value in labels_dict_.items(): | ||
43 | - if(value == val): | ||
44 | - return key | ||
45 | - | ||
46 | - | ||
47 | -def convertMillis(millis): | ||
48 | - seconds = (millis/1000) % 60 | ||
49 | - minutes = (millis/(1000*60)) % 60 | ||
50 | - hours = (millis/(1000*60*60)) % 24 | ||
51 | - return seconds, int(minutes), int(hours) | ||
52 | - | ||
53 | -# cap = cv2.VideoCapture(0) # 0번 카메라 | ||
54 | - | ||
55 | - | ||
56 | -# 동영상 크기(frame정보)를 읽어옴 | ||
57 | -frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | ||
58 | -frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | ||
59 | -frame_size = (frameWidth, frameHeight) | ||
60 | -fps = cap.get((cv2.CAP_PROP_FPS)) | ||
61 | - | ||
62 | - | ||
63 | -_, img_bgr = cap.read() # (800, 1920, 3) | ||
64 | -padding_size = 0 | ||
65 | -resized_width = 1920 | ||
66 | -video_size = (resized_width, int( | ||
67 | - img_bgr.shape[0] * resized_width // img_bgr.shape[1])) | ||
68 | -timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)] | ||
69 | -prev_time = 0 | ||
70 | - | ||
71 | -fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') | ||
72 | -# out1 = cv2.VideoWriter('./data/record0.mp4',fourcc, fps, frame_size) | ||
73 | - | ||
74 | -while True: | ||
75 | - retval, frameBGR = cap.read() # 영상을 한 frame씩 읽어오기 | ||
76 | - current_time = time.time() - prev_time | ||
77 | - | ||
78 | - if(type(frameBGR) == type(None)): | ||
79 | - pass | ||
80 | - else: | ||
81 | - frameBGR = cv2.resize(frameBGR, video_size) | ||
82 | - frame = cv2.cvtColor(frameBGR, cv2.COLOR_BGR2RGB) | ||
83 | - | ||
84 | - if (retval is True) and (current_time > 1.5): | ||
85 | - prev_time = time.time() | ||
86 | - faces = detector(frame, 1) | ||
87 | - | ||
88 | - for (i, face) in enumerate(faces): | ||
89 | - shape = predictor(frame, face) | ||
90 | - face_descriptor = facerec.compute_face_descriptor(frame, shape) | ||
91 | - | ||
92 | - img = cv2.resize(frame[face.top():face.bottom(), face.left( | ||
93 | - ):face.right()], dsize=(224, 224), interpolation=cv2.INTER_CUBIC) | ||
94 | - imgarr = np.array(img).reshape(1, 224, 224, 3) / 255 | ||
95 | - # emotion = labels_dict_[model.predict(imgarr).argmax(axis=-1)[0]] | ||
96 | - emotion = get_key(model.predict_classes(imgarr)) | ||
97 | - | ||
98 | - last_found = {'name': 'unknown', | ||
99 | - 'dist': 0.6, 'color': (0, 0, 255)} | ||
100 | - | ||
101 | - for name, saved_desc in descs.items(): | ||
102 | - dist = np.linalg.norm( | ||
103 | - [face_descriptor] - saved_desc, axis=1) | ||
104 | - if dist < last_found['dist']: | ||
105 | - last_found = {'name': name, 'dist': dist, | ||
106 | - 'color': (255, 255, 255)} | ||
107 | - | ||
108 | - cv2.rectangle(frameBGR, pt1=(face.left(), face.top()), pt2=( | ||
109 | - face.right(), face.bottom()), color=last_found['color'], thickness=2) | ||
110 | - cv2.putText(frameBGR, last_found['name'] + ',' + emotion, org=(face.left(), face.top( | ||
111 | - )), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=last_found['color'], thickness=2) | ||
112 | - # cv2.putText(frameBGR, last_found['name'] + ',' , org=(face.left(), face.top()), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=last_found['color'], thickness=2) | ||
113 | - | ||
114 | - con_sec, con_min, con_hour = convertMillis( | ||
115 | - cap.get(cv2.CAP_PROP_POS_MSEC)) | ||
116 | - time_dict[emotion].append("{0}:{1}:{2}".format( | ||
117 | - con_hour, con_min, round(con_sec, 3))) | ||
118 | - print("{0}:{1}:{2} {3}".format( | ||
119 | - con_hour, con_min, round(con_sec, 3), emotion)) | ||
120 | - # print("{0}:{1}:{2} {3}".format(con_hour, con_min, con_sec)) | ||
121 | - | ||
122 | - cv2.imshow('frame', frameBGR) | ||
123 | - | ||
124 | - key = cv2.waitKey(25) | ||
125 | - if key == 27: | ||
126 | - break | ||
127 | - | ||
128 | -print(time_dict) | ||
129 | -print("총 시간 : ", time.time() - start) | ||
130 | -if cap.isOpened(): | ||
131 | - cap.release() | ||
132 | - | ||
133 | -for i in range(1, 5): | ||
134 | - cv2.destroyAllWindows() | ||
135 | - cv2.waitKey(1) |
No preview for this file type
report/보고서/최종보고서.docx
0 → 100644
This file is too large to display.
report/최종발표영상 및 피피티/최종발표.pptx
0 → 100644
This file is too large to display.
report/최종발표영상 및 피피티/최종발표영상.mp4
0 → 100644
This file is too large to display.
-
Please register or login to post a comment