Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto rotate frames containing faces that are not vertical to fix crappy insight face bug. #364

Merged
merged 18 commits into from
Dec 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add clockwise rotation algorithm
  • Loading branch information
Okusan Kenkyuusha committed Nov 2, 2023
commit b3c766c809c600d5c9d0300f01657db165abf1f5
41 changes: 37 additions & 4 deletions roop/ProcessMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,18 @@ def auto_rotate_frame(self, original_face, frame:Frame):

if horizontal_face:
print("face is horizontal, rotating frame anti-clockwise and getting face bounding box from rotated frame")
rotated_bbox = self.rotate_bbox_anticlockwise(original_face.bbox, frame)
frame = rotate_anticlockwise(frame)
target_face = self.get_rotated_target_face(rotated_bbox, frame)
# rotated_bbox = self.rotate_bbox_anticlockwise(original_face.bbox, frame)
print(f"original bbox: {original_face.bbox}")
rotated_bbox = self.rotate_bbox_clockwise(original_face.bbox, frame)
# rotated_frame = rotate_anticlockwise(frame)
rotated_frame = rotate_clockwise(frame)
target_face = self.get_rotated_target_face(rotated_bbox, rotated_frame)
if target_face is not None:
frame = rotated_frame
else:
#no face was detected in the rotated frame, so use the original frame and face
target_face = original_face

print()
else:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a # here too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was a little confused when I saw this comment because I thought I removed this. Turns out I forgot that one last commit. Fixed it now.

print("face is vertical, leaving frame untouched")
Expand All @@ -362,6 +371,10 @@ def auto_rotate_frame(self, original_face, frame:Frame):

def get_rotated_target_face(self, rotated_bbox, rotated_frame:Frame):
rotated_faces = get_all_faces(rotated_frame)

if rotated_faces is None:
return None

rotated_target_face = rotated_faces[0]
best_iou = 0

Expand All @@ -371,9 +384,29 @@ def get_rotated_target_face(self, rotated_bbox, rotated_frame:Frame):
rotated_target_face = rotated_face
best_iou = iou

print(f"closest matching face - iou: {best_iou}, bbox: {rotated_target_face.bbox}, rotated bbox: {rotated_bbox}")
return rotated_target_face


def rotate_bbox_clockwise(self, bbox, frame:Frame):
(height, width) = frame.shape[:2]

start_x = bbox[0]
start_y = bbox[1]
end_x = bbox[2]
end_y = bbox[3]

#bottom left corner becomes top left corner
#top right corner becomes bottom right corner

rotated_start_x = height - end_y
rotated_start_y = start_x
rotated_end_x = height - start_y
rotated_end_y = end_x

return [rotated_start_x, rotated_start_y, rotated_end_x, rotated_end_y]


def rotate_bbox_anticlockwise(self, bbox, frame:Frame):

(height, width) = frame.shape[:2]
Expand Down Expand Up @@ -423,7 +456,7 @@ def auto_unrotate_frame(self, original_face, frame:Frame):

if horizontal_face:
print("face was horizontal, unrotating processed frame")
return rotate_clockwise(frame)
return rotate_anticlockwise(frame)

print("face was vertical, leaving processed frame untouched")
return frame
Expand Down