I am tried so many time to figure out the problem in detecting the face and also it’s not so smooth enough to like other tools out there.
So basically I am using python and Yolo in this project but I want the person who is talking and who the ROI (region of interest) is.
Here is the code:
`from ultralytics import YOLO
from ultralytics.engine.results import Results
from moviepy.editor import VideoFileClip, concatenate_videoclips
from moviepy.video.fx.crop import crop
Load the YOLOv8 model
model = YOLO(“yolov8n.pt”)
Load the input video
clip = VideoFileClip(“short_test.mp4”)
tacked_clips = []
for frame_no, frame in enumerate(clip.iter_frames()):
# Process the frame
results: list[Results] = model(frame)
# Get the bounding box of the main object
if results[0].boxes:
objects = results[0].boxes
main_obj = max(
objects, key=lambda x: x.conf
) # Assuming the first detected object is the main one
x1, y1, x2, y2 = [int(val) for val in main_obj.xyxy[0].tolist()]
# Calculate the crop region based on the object's position and the target aspect ratio
w, h = clip.size
new_w = int(h * 9 / 16)
new_h = h
x_center = x2 - x1
y_center = y2 - y1
# Adjust x_center and y_center if they would cause the crop region to exceed the bounds
if x_center + (new_w / 2) > w:
x_center -= x_center + (new_w / 2) - w
elif x_center - (new_w / 2) < 0:
x_center += abs(x_center - (new_w / 2))
if y_center + (new_h / 2) > h:
y_center -= y_center + (new_h / 2) - h
elif y_center - (new_h / 2) < 0:
y_center += abs(y_center - (new_h / 2))
# Create a subclip for the current frame
start_time = frame_no / clip.fps
end_time = (frame_no + 1) / clip.fps
subclip = clip.subclip(start_time, end_time)
# Apply cropping using MoviePy
cropped_clip = crop(
subclip, x_center=x_center, y_center=y_center, width=new_w, height=new_h
)
tacked_clips.append(cropped_clip)
reframed_clip = concatenate_videoclips(tacked_clips, method=”compose”)
reframed_clip.write_videofile(“output_video.mp4”)`
So basically I want to fix the face detection with ROI detection where it can detect the face and make that face and the body on to the frame and making sure that the speaker who is speaking is brought to the frame
shreesha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.