Skip to content

Commit

Permalink
fix : change threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
sycuuui committed May 20, 2024
1 parent cc86448 commit 31d8e17
Show file tree
Hide file tree
Showing 21 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ wider face.v3i.yolov5pytorch
dist
build
myenv
__pycache__
Binary file modified __pycache__/app.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/createTarget.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/mosaic.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/yoona_target.cpython-311.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def handle_video():
image_file = request.files[f'image{i}']
if image_file:
filename, extension = os.path.splitext(image_file.filename)
image_filename = os.path.join('save/train/Gongyoo', f'image{i}{extension}')
image_filename = os.path.join('save/train', f'image{i}{extension}')
image_file.save(image_filename)
image_paths.append(image_filename)
print(f'Image {i} saved successfully.')
Expand Down
8 changes: 6 additions & 2 deletions createTarget.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def extract_and_identify_faces_from_video(video_path):
# 얼굴 이미지 추출
face_image = frame[top:bottom, left:right]
face_images.append(face_image)

face_encodings.append(encoding)

# 인식된 얼굴 분류
Expand All @@ -38,13 +39,16 @@ def extract_and_identify_faces_from_video(video_path):
group_encodings = [enc for _, enc in face_group]
avg_encoding = np.mean(group_encodings, axis=0)
dist = np.linalg.norm(avg_encoding - encoding)
if dist < 0.6: # 같은 사람으로 판단하는 임계값
if dist < 0.555: # 같은 사람으로 판단하는 임계값
face_group.append((face_images[idx], encoding))
matched = True
break
if not matched:
identified_faces.append([(face_images[idx], encoding)])

# 얼굴 수가 많은 순서로 그룹 정렬
identified_faces.sort(key=lambda x: len(x), reverse=True)

video_capture.release()
print('end1')

Expand Down Expand Up @@ -77,4 +81,4 @@ def save_faces(identified_faces):
# 모든 인물의 인코딩된 이미지를 배열에 추가
face_base64_arrays.append(encoded_faces)

return face_base64_arrays
return face_base64_arrays
21 changes: 16 additions & 5 deletions mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ def mosaic(video_path, image_paths):

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)
Expand Down Expand Up @@ -49,6 +46,13 @@ def mosaic(video_path, image_paths):

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

if len(detections.xyxy[0]) > 2:
threshold = 0.27
not_threshold = 0.2851
else:
threshold = 0.3
not_threshold = 0.47

for face_id in detections.xyxy[0]:
x1, y1, x2, y2 = face_id[:4].int().tolist()
if y2 - y1 > 50 and x2 - x1 > 50:
Expand All @@ -59,17 +63,24 @@ def mosaic(video_path, image_paths):
distance = result['distance']

if not_threshold >= distance >= threshold:
face_filename = f"face_{face_count}.jpg"
face_filename = f"D/face_{face_count}.jpg"
verified_str = 'Different'
distance_str = '(%.4f <= %.4f)' % (distance, threshold)
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)
break

if distance < threshold:
face_filename = f"S/face_{face_count}.jpg"
verified_str = 'Same'
distance_str = '(%.4f >= %.4f)' % (distance, threshold)
print(face_filename, verified_str, distance_str)
face_filepath = os.path.join(faces_dir, face_filename)
cv2.imwrite(face_filepath, face_image)
break

if distance > not_threshold:
Expand Down
Binary file removed save/train/Gongyoo/image1.jpeg
Binary file not shown.
Binary file removed save/train/Gongyoo/image1.png
Binary file not shown.
Binary file removed save/train/Gongyoo/image2.jpeg
Binary file not shown.
Binary file removed save/train/Gongyoo/image2.png
Binary file not shown.
Binary file removed save/train/Gongyoo/image3.jpeg
Binary file not shown.
Binary file removed save/train/Gongyoo/image3.png
Binary file not shown.
Binary file removed save/train/Gongyoo/image4.jpeg
Binary file not shown.
Binary file removed save/train/Gongyoo/image5.jpeg
Binary file not shown.
Binary file removed save/train/Gongyoo/image6.jpeg
Binary file not shown.
Binary file removed save/train/Gongyoo/image7.jpeg
Binary file not shown.
Binary file removed save/train/Gongyoo/image8.jpeg
Binary file not shown.
Binary file removed save/train/Gongyoo/image9.jpeg
Binary file not shown.
Binary file removed tmp/output2.mp4
Binary file not shown.

0 comments on commit 31d8e17

Please sign in to comment.