diff --git a/SoccerAnalysis/basicSoccerNet/shortTestVid.mov b/SoccerAnalysis/basicSoccerNet/shortTestVid.mov new file mode 100644 index 00000000..7214eefb Binary files /dev/null and b/SoccerAnalysis/basicSoccerNet/shortTestVid.mov differ diff --git a/SoccerAnalysis/basicSoccerNet/short_soccer_vid.mp4 b/SoccerAnalysis/basicSoccerNet/short_soccer_vid.mp4 new file mode 100644 index 00000000..86981c7a Binary files /dev/null and b/SoccerAnalysis/basicSoccerNet/short_soccer_vid.mp4 differ diff --git a/SoccerAnalysis/basicSoccerNet/soccerNet.py b/SoccerAnalysis/basicSoccerNet/soccerNet.py index 34df5a90..e955406b 100644 --- a/SoccerAnalysis/basicSoccerNet/soccerNet.py +++ b/SoccerAnalysis/basicSoccerNet/soccerNet.py @@ -6,30 +6,32 @@ # Paths to files PICKLE_FILE_PATH = "video_detections.pkl" # Path to the saved detections -SOURCE_VIDEO_PATH = "source_video.mov" # Path to the source video file -TARGET_VIDEO_PATH = "video_out.mov" # Path to the output video file +SOURCE_VIDEO_PATH = "short_soccer_vid.mp4" # Path to the source video file +TARGET_VIDEO_PATH = "video_out.mp4" # Path to the output video file # Initialize Roboflow and model -rf = Roboflow(api_key="") +rf = Roboflow(api_key="sQSgPz1GyivBdnnWUKtR") project = rf.workspace().project("football-players-detection-3zvbc") model = project.version(9).model # Create BYTETracker instance -byte_tracker = sv.ByteTrack(track_thresh=0.25, track_buffer=30, match_thresh=0.8, frame_rate=30) +#byte_tracker = sv.ByteTrack(track_thresh=0.25, track_buffer=30, match_thresh=0.8, frame_rate=30) +#byte_tracker = sv.ByteTrack(track_buffer=30, match_thresh=0.8, frame_rate=30) # Create instance of BoxAnnotator and TraceAnnotator -box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2) +box_annotator = sv.BoxAnnotator(thickness=4) + trace_annotator = sv.TraceAnnotator(thickness=4, trace_length=50) # Function to load detections from a pickle file -def load_detections_from_pickle(pickle_file_path): - with open(pickle_file_path, 'rb') as f: - detections = pickle.load(f) - print(f"Loaded detections from {pickle_file_path}") - return detections +# def load_detections_from_pickle(pickle_file_path): +# with open(pickle_file_path, 'rb') as f: +# detections = pickle.load(f) +# print(f"Loaded detections from {pickle_file_path}") +# return detections # Load detections from pickle file -all_detections = load_detections_from_pickle(PICKLE_FILE_PATH) +# all_detections = load_detections_from_pickle(PICKLE_FILE_PATH) # Open the source video cap = cv2.VideoCapture(SOURCE_VIDEO_PATH) @@ -72,3 +74,7 @@ def load_detections_from_pickle(pickle_file_path): cap.release() out.release() print(f"Processed video saved to {TARGET_VIDEO_PATH}") + +""" +Aarnav, and my favorite animal are dogs +""" \ No newline at end of file diff --git a/SoccerAnalysis/basicSoccerNet/updatedSoccerNet.py b/SoccerAnalysis/basicSoccerNet/updatedSoccerNet.py new file mode 100644 index 00000000..86e78b39 --- /dev/null +++ b/SoccerAnalysis/basicSoccerNet/updatedSoccerNet.py @@ -0,0 +1,71 @@ +import numpy as np +import supervision as sv +from roboflow import Roboflow +from inference import get_model + + +SOURCE_VIDEO_PATH = "shortTestVid.mov" +TARGET_VIDEO_PATH = "video_out.mp4" + +rf = Roboflow(api_key="") +#project = rf.workspace().project("football-players-detection-3zvbc") +model = get_model(model_id="football-players-detection-3zvbc/9") +#model = project.version(9).model +#print("model is 1111", model) + +# Create BYTETracker instance +byte_tracker = sv.ByteTrack(track_activation_threshold=0.25, lost_track_buffer=30, minimum_matching_threshold=0.8, frame_rate=1) + +# Create VideoInfo instance +video_info = sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH) + +# Create frame generator +generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH) + +# Create instance of BoxAnnotator +box_annotator = sv.BoxAnnotator(thickness=4) + +# Create instance of TraceAnnotator +trace_annotator = sv.TraceAnnotator(thickness=4, trace_length=50) + +# Define callback function to be used in video processing +def callback(frame: np.ndarray, index: int) -> np.ndarray: + # Model prediction on single frame and conversion to supervision Detections + print('callback') + print("frame is ", frame) + print("model is ", model) + results = model.infer(frame) + detections = sv.Detections.from_inference(results[0].dict(by_alias=True, exclude_none=True)) + + # Show detections in real time + print(detections) + + # Tracking detections + detections = byte_tracker.update_with_detections(detections) + print("Updated Detections:", detections) + + # Prepare labels for box annotations + labels = [ + f"{detections.data['class_name'][i]} {detections.confidence[i]:0.2f}" + for i in range(len(detections.confidence)) + ] + + # Annotate frame with traces and boxes + annotated_frame = trace_annotator.annotate( + scene=frame.copy(), + detections=detections + ) + annotated_frame = box_annotator.annotate( + scene=annotated_frame, + detections=detections, + ) + + # Return annotated frame + return annotated_frame + +# Process the whole video +sv.process_video( + source_path=SOURCE_VIDEO_PATH, + target_path=TARGET_VIDEO_PATH, + callback=callback +) \ No newline at end of file diff --git a/SoccerAnalysis/basicSoccerNet/video_out.mp4 b/SoccerAnalysis/basicSoccerNet/video_out.mp4 new file mode 100644 index 00000000..07ea5798 Binary files /dev/null and b/SoccerAnalysis/basicSoccerNet/video_out.mp4 differ