-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathver.04.py
219 lines (184 loc) · 8.43 KB
/
ver.04.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import threading
import datetime
import os
import signal
import cv2
import numpy as np
import pandas as pd
import asyncio
import io
import glob
import sys
import time
import uuid
import requests
from urllib.parse import urlparse
from io import BytesIO
from PIL import Image, ImageDraw
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person, SnapshotObjectType, \
OperationStatusType
def logging(persisted_face_id):
#df1은 설정값을 읽어와야함. 안찾는 친구는 굳이 반복문 돌릴 필요 없으니까
df1 = pd.read_csv('./data/person_list.csv')
df2 = df1.loc[df1['face_id'] == persisted_face_id]
if(df2.empty):
print("그런사람 없어요.")
elif(df2.empty == False ):
log_file = pd.read_csv('./data/log_file.csv')
temp = df2.copy(deep = True)
time = datetime.datetime.now()
now = time.strftime('%Y-%m-%d %H:%M:%S')
temp["Time"] = now
log_file = log_file.append(temp, ignore_index=True)
log_file.to_csv("C:/Users/CNH/PycharmProjects/untitled1/log_file.csv", index= False)
print("기록")
#함수정의 시작
def API_CALL():
path = "./outputs/"
multi_face_image_path = path + 'mnist_merged' + str(int(count_1)) + ".jpg"
multi_image_name = os.path.basename(multi_face_image_path)
image_name_2 = open(multi_face_image_path, 'rb')
detected_faces2 = face_client.face.detect_with_stream(image=image_name_2, return_face_id=True,
recognition_model='recognition_03')
if detected_faces2:
print('Detected face ID from', multi_image_name, ':')
for face in detected_faces2:
print("발견"+face.face_id)
similar_faces = face_client.face.find_similar(face_id=face.face_id,
face_list_id='api_list')
# 의미없는 반복문이긴 하나, SIMILAR_FACE가 없는 경우를 위해서 만듬.
for i in similar_faces:
#log 기록하는 csv 파일 열기 , list 포함하고있는 파일도 열기
if (similar_faces[0].confidence > 0.6):
logging(similar_faces[0].persisted_face_id)
img = cv2.imread(multi_face_image_path)
img = img[x - 10:x + w + 10, y - 10:y + h + 10]
# cv2.imshow("linear", img)
#함수정의 끝
#프로그램 시작
# 몇장을 한번에 합쳐서 검출한 것인지는, set_num 변수값을 지정해서 바꿔주면됩니다!
set_num = 4
# 1. azure api에 접근하기 위한 object들을 key값을 이용해 생성해줌
KEY = os.environ['FACE_SUBSCRIPTION_KEY']
ENDPOINT = os.environ['FACE_ENDPOINT']
# 2. Load Yolo( yolo 알고리즘 동작에 필요한 weight 파일들을 불러옴)
net = cv2.dnn.readNet("./data/weights/yolov3-wider_16000.weights", "./data/cfg/yolov3-face.cfg")
# GPU 가속을 원하면 여기 밑 두줄 주석 해제!
#net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
#net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
classes = []
with open("./data/names/coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))
# 이미지 이름을 변경해주기 위한 count 변수
count_1 = 0
count = 0
# 3. 특정인을 찾기위해 특정인 사진을 입력받음
face_client = FaceClient(ENDPOINT, CognitiveServicesCredentials(KEY))
# Display the detected face ID in the first single-face image.
# 3-1 IDs are used for comparison to faces (their IDs) detected in other images.
# 해당 이미지에서 검출된 얼굴들이 모두 저장됨. (face라는 객체에 face_id라는 string 변수가 잇는듯. detected_face = list )
# 3-2 Save this ID to use in Find Similar -> 이 코드는 id 한개만 저장
# 4. load video
#cap = cv2.VideoCapture("rtsp://admin:@[email protected]:554/profile2/media.smp")
# cctv ip 주소의 경우, 사용자에게서 입력받아 서버의 데이터에서 할당받을수 있게함.
# ip cctv를 쓰고 싶을경우, rtsp protocol을 이용해서 사용할 것!
# 터미널 이용해서 작동할시, videoCapture 뒤에 cv2.CAP_V4L )
cap = cv2.VideoCapture(0)
#cap = cv2.VideoCapture('./videos/test.mp4')
cv2.dnn.DNN_TARGET_OPENCL
#flag의 용도: flag는 처음 영상 시작시 합쳐지는 파일이 만들어지는 규칙의 예외이기 때문에 설정해줌.
# 이건 반복문. 영상이 살아있거나, 특정 키를 누르기 전까지 계속 찾는걸 반복함.
flag = False
#시간을 저장하는 변수.
time = datetime.datetime.now()
now_time = time.strftime('%Y-%m-%d %H:%M:%S')
while True:
# capture video
print("한프레임")
ret, frame = cap.read()
# 현재 프레임 == 총 프레임 : 비디오의 끝 종료
if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
print("video end")
break
# 프레임 수 조절 (Test 중에는 안씀)
# if cap.get(cv2.CAP_PROP_POS_FRAMES) % 15 != 0:
# continue
frame = cv2.resize(frame, None, fx=1, fy=1)
height, width, channels = frame.shape
# 5. Detecting objects
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
# Showing information on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
path = "./outputs/"
# print(indexes) 이런거 저장됏다고 보여주는 구문
font = cv2.FONT_HERSHEY_PLAIN
if not boxes:
if flag == True:
print("저장,없어서")
merged.save(path + 'mnist_merged' + str(int(count_1)) + ".jpg")
API_CALL()
count = 0
flag = False
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
name = str(count_1) + ".jpg"
faceimg = frame[y - 10:y + h + 10, x - 10:x + w + 10]
if not faceimg.any():
continue
faceimg = cv2.resize(faceimg, dsize=(200, 200), interpolation=cv2.INTER_CUBIC)
cv2.destroyAllWindows()
cv2.imwrite(path + name, faceimg)
print(count_1)
im0 = Image.open(path + name)
if count % set_num == 0:
if flag == False:
merged = Image.new('L', (200 * set_num, 200 * 1))
flag = True
print("생성,flag = false")
elif flag == True:
print("저장")
merged.save(path + 'mnist_merged' + str(int(count_1)) + ".jpg")
merged = Image.new('L', (200 * set_num, 200 * 1))
print("생성, flag = true")
API_CALL()
merged.paste(im0, (200 * (count % set_num), 0))
count += 1
count_1 += 1
#시간 계속 계산해서 1초 넘어가면 자르고 그냥 API 넘겨버리게 하기.
key = cv2.waitKey(0) & 0xFF
if key == 27:
break
# 프레임이 끝나게 되면 끝난 부분에서도 다시 저장해 similar 한 face가 있는지 검사해야하므로, 똑같은 부분이 나오게됨.
path = "./outputs/"
merged.save(path + 'mnist_merged' + str(count / (set_num)) + ".jpg")
cap.release()
print("program end")
os.kill(os.getpid(), signal.SIGTERM)