import cv2 import numpy as np import torch from transformers import pipeline class VideoPlayer: def __init__(self, video_path): self.cap = cv2.VideoCapture(video_path) self.is_paused = False self.is_stopped = False self.frame = None self.playback_speed = 1.0 self.detector = pipeline("object-detection") # Hugging Face Object Detection Pipeline def process_frame(self, frame): # Convert the frame to RGB (Hugging Face models expect RGB input) rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Perform object detection (can be changed to any other Hugging Face task) results = self.detector(rgb_frame) # Annotate the frame with detection results for result in results: box = result['box'] label = result['label'] score = result['score'] x1, y1, x2, y2 = box cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(frame, f"{label}: {score:.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) return frame def play(self): while True: if self.is_stopped: break if not self.is_paused: ret, self.frame = self.cap.read() if not ret: print("End of video") break # Process the frame using Hugging Face (can be changed to any other task) self.frame = self.process_frame(self.frame) # Show the current frame cv2.imshow('Video Player', self.frame) # Wait for key press for controls: Pause, Stop, Play, Quit key = cv2.waitKey(int(1000 / self.playback_speed)) & 0xFF if key == ord('p'): # Pause self.is_paused = not self.is_paused print("Paused" if self.is_paused else "Playing") elif key == ord('s'): # Stop self.is_stopped = True print("Stopped") break elif key == ord('r'): # Resume playing if self.is_paused: self.is_paused = False print("Playing") elif key == ord('q'): # Quit print("Quitting") break self.cap.release() cv2.destroyAllWindows() if __name__ == "__main__": video_path = "your_video.mp4" # Provide the path to your video file here player = VideoPlayer(video_path) player.play()