Skip to content

Commit

Permalink
feat : deepFace custom
Browse files Browse the repository at this point in the history
  • Loading branch information
sycuuui committed May 10, 2024
1 parent cada2e9 commit 5e45833
Show file tree
Hide file tree
Showing 36 changed files with 113 additions and 39 deletions.
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.11"
Binary file modified __pycache__/app.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/mosaic.cpython-311.pyc
Binary file not shown.
33 changes: 9 additions & 24 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import io
import os
from pytorch import mosaic_jiyeon
from createTarget import extract_and_identify_faces_from_video
from yoona_target import yoona_test
import mosaic


from flask import (Flask, request, send_file, jsonify)
Expand Down Expand Up @@ -42,40 +41,26 @@ def yoona():

@app.route('/video', methods=['POST'])
def handle_video():
print('start')
video_file = request.files['video']
image_count = int(request.form['imageSize'])
print(image_count)

# 파일 저장 경로 지정, 여기서는 임시로 파일 이름으로 저장
#filename = os.path.join('tmp', file.filename)
video_file.save(video_file.filename)
print('success file')

image_paths = []
for i in range(1, image_count+1):
image_file = request.files['image{}'.format(i)]

for i in range(1, image_count + 1):
image_file = request.files[f'image{i}']
if image_file:
# 이미지를 저장할 경로 지정 (여기서는 루트 디렉토리에 저장)
filename, extension = os.path.splitext(image_file.filename)
image_filename = os.path.join('./pytorch/train/Gongyoo', f'image{i}{extension}')
image_filename = os.path.join('save/train/Gongyoo', f'image{i}{extension}')
image_file.save(image_filename)
image_paths.append(image_filename)
print('Image {} saved successfully.'.format(i))
else:
print('No image received for image{}.'.format(i))

print('success img')
print(image_paths)

# 여기서 파일을 처리하는 로직을 추가할 수 있습니다.
# 예를 들어, 처리된 파일을 다시 클라이언트에게 보낼 수 있습니다.
# 이 예시에서는 단순히 저장된 파일을 그대로 반환합니다.
print('start mosaic')
output_video_path = mosaic_jiyeon.mosaic(video_file.filename, image_paths)
print(f'Image {i} saved successfully.')

output_video_path = mosaic.mosaic(video_file.filename, image_paths)
# output_video_path = mosaic_jiyeon.mosaic(video_file.filename, image_paths)
print(output_video_path)
return send_file(output_video_path, mimetype='video/mp4', as_attachment=True)
return send_file(output_video_path, mimetype='video/mp4', as_attachment=True, download_name='output_video.mp4')

@app.route('/image', methods=['POST'])
def image_test():
Expand Down
Binary file added macWideface.pt
Binary file not shown.
6 changes: 3 additions & 3 deletions pytorch/mosaic_jiyeon.py → model_mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

def mosaic(video_path, image_paths):
# YOLOv5 모델 로드
# model = torch.hub.load('./pytorch/yolov5', 'custom', path='./pytorch/best.pt', source='local')
model = torch.hub.load('ultralytics/yolov5', 'custom', path='./pytorch/best.pt')
output_video_path = os.path.join('tmp', video_path)
# model = torch.hub.load('./save/yolov5', 'custom', path='./save/best.pt', source='local')
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)
output_video_path = os.path.join('save/tmp', video_path)
# 특정 사람의 얼굴 이미지 로드
# person_image = face_recognition.load_image_file("goognyoo.png")
# person_encoding = face_recognition.face_encodings(person_image)[0]
Expand Down
90 changes: 90 additions & 0 deletions mosaic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import cv2
import os
from deepface import DeepFace
import torch


def mosaic(video_path, image_paths):
model_name = "Facenet"

output_video_path = os.path.join('tmp', 'output2.mp4')
cap = cv2.VideoCapture(video_path)
fps = int(cap.get(cv2.CAP_PROP_FPS))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter(output_video_path, cv2.VideoWriter.fourcc(*'mp4v'), fps, (frame_width, frame_height))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 동영상의 전체 프레임 수를 얻음

print(f"Total number of frames in the video: {total_frames}")

threshold = 0.465
not_threshold = 0.47

faces_dir = os.path.join('tmp', 'faces')
if not os.path.exists(faces_dir):
os.makedirs(faces_dir)

face_count = 0
current_frame_count = 0

model = torch.hub.load('ultralytics/yolov5', 'custom', path='macWideface.pt', force_reload=True)

embedding_list = []
for image_path in image_paths:
embedding_result = DeepFace.create_verification_result(
img1_path=image_path,
detector_backend='retinaface',
model_name=model_name,
enforce_detection=False
)
embedding_list.append(embedding_result["embeddings"][0])

while cap.isOpened():
ret, frame = cap.read()
if not ret:
break

# detections = RetinaFace.detect_faces(img_path=frame)
detections = model(frame)

print(f"{current_frame_count}감지 시작")

for face_id in detections.xyxy[0]:
x1, y1, x2, y2 = face_id[:4].int().tolist()
if y2 - y1 > 50 and x2 - x1 > 50:
face_image = frame[y1:y2, x1:x2]
for ref_face in embedding_list:
result = DeepFace.verify(img1_path=face_image, img2_path=ref_face, model_name=model_name,
detector_backend='retinaface', enforce_detection=False)
distance = result['distance']

if not_threshold >= distance >= threshold:
face_filename = f"face_{face_count}.jpg"
verified_str = 'Different'
distance_str = '(%.4f <= %.4f)' % (distance, threshold)
print(face_filename,verified_str, distance_str)
face = cv2.resize(face_image, (10, 10))
face = cv2.resize(face, (x2 - x1, y2 - y1), interpolation=cv2.INTER_AREA)
frame[y1:y2, x1:x2] = face
face_filepath = os.path.join(faces_dir, face_filename)
cv2.imwrite(face_filepath, face_image)

if distance < threshold:
break

if distance > not_threshold:
face = cv2.resize(face_image, (10, 10))
face = cv2.resize(face, (x2 - x1, y2 - y1), interpolation=cv2.INTER_AREA)
frame[y1:y2, x1:x2] = face
break

face_count += 1
current_frame_count += 1
out.write(frame)

cap.release()
out.release()
cv2.destroyAllWindows()

return output_video_path

Binary file removed pytorch/Video.mp4
Binary file not shown.
Binary file removed pytorch/__pycache__/mosaic_jiyeon.cpython-311.pyc
Binary file not shown.
Binary file removed pytorch/best.pt
Binary file not shown.
1 change: 0 additions & 1 deletion pytorch/deep_sort
Submodule deep_sort deleted from 280b8b
Binary file removed pytorch/tmp/Video.mp4
Binary file not shown.
Binary file removed pytorch/wonbin1.jpg
Binary file not shown.
Binary file removed pytorch/wonbin2.jpg
Binary file not shown.
3 changes: 0 additions & 3 deletions pytorch/your_project_name.egg-info/PKG-INFO

This file was deleted.

6 changes: 0 additions & 6 deletions pytorch/your_project_name.egg-info/SOURCES.txt

This file was deleted.

1 change: 0 additions & 1 deletion pytorch/your_project_name.egg-info/dependency_links.txt

This file was deleted.

1 change: 0 additions & 1 deletion pytorch/your_project_name.egg-info/top_level.txt

This file was deleted.

Binary file added save/train/Gongyoo/image1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added save/train/Gongyoo/image2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added save/train/Gongyoo/image3.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added save/train/Gongyoo/image4.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added save/train/Gongyoo/image5.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added save/train/Gongyoo/image6.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added save/train/Gongyoo/image7.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added save/train/Gongyoo/image8.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added save/train/Gongyoo/image9.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added tmp/output2.mp4
Binary file not shown.

0 comments on commit 5e45833

Please sign in to comment.