from multiprocessing import Queue, cpu_count from fastapi import FastAPI, Request, Response, Form, File, UploadFile from fastapi.responses import HTMLResponse from jinja2 import Template import cv2, time, threading, signal, queue, os from loading_screen import display_loading_screen from player_ball_assigner import PlayerBallAssigner from camera_movement_estimator import CameraMovementEstimator from video_batch_processor import process_video_batch from view_transformer import ViewTransformer from speed_and_distance_estimator import SpeedAndDistance_Estimator from collections import deque from concurrent.futures import ThreadPoolExecutor from globals import zoom_options_instance from fastapi.responses import StreamingResponse app = FastAPI() global frame_queue, result_queue, buffer, isLoading, batch_duration, stop_event, video_source, processing_thread isLoading = True batch_duration = 1 FRAMES_PER_SECOND = 30 LOADING_NUMBER_FRAMES = batch_duration * FRAMES_PER_SECOND MAX_QUEUE = 1000 buffer = deque(maxlen=MAX_QUEUE) frame_queue = Queue(maxsize=MAX_QUEUE) result_queue = Queue() stop_event = threading.Event() video_source = None # Can be URL or local file path processing_thread = None html_page = """ Video Stream

Video Stream

Track Player Track Ball Enable Zoom Smooth Zoom Dynamic Zoom Level

Set IP Camera URL

Use Local Video File

""" @app.get('/', response_class=HTMLResponse) def index(): template = Template(html_page) rendered_html = template.render() return HTMLResponse(content=rendered_html) def validate_video_source(source): try: cap = cv2.VideoCapture(source) if not cap.isOpened(): return False ret, frame = cap.read() if not ret or frame is None: return False cap.release() return True except: return False @app.post('/set_video_source') def set_video_source(video_source: str = Form(...)): global processing_thread, stop_event if not validate_video_source(video_source): return {"message": "Invalid video source or unable to connect."} # Stop any ongoing processing if processing_thread and processing_thread.is_alive(): stop_event.set() processing_thread.join() # Wait for the thread to finish # Reset the event to allow new processing stop_event = threading.Event() # Set the new video source and restart processing globals()['video_source'] = video_source print(f"SETTING THE VIDEO SOURCE TO {video_source}") if video_source: processing_thread = threading.Thread(target=start_processing) processing_thread.start() return {"message": "Video source set and processing restarted."} @app.post("/upload_local_file") async def upload_local_file(file: UploadFile = File(...)): file_location = f"uploads/{file.filename}" os.makedirs("uploads", exist_ok=True) with open(file_location, "wb+") as file_object: file_object.write(file.file.read()) return await set_video_source(video_source=file_location) @app.post('/set_zoom_options') def set_zoom_options(request: Request): zoom_options_instance.update_options(request.json()) return '', 204 def start_processing(): global batch_duration, video_source batch_num = 0 batch_size = 3 #FRAMES_PER_SECOND * batch_duration # Start frame capture thread capture_thread = threading.Thread(target=capture_frames, args=(video_source, frame_queue, stop_event)) capture_thread.start() first_frame = frame_queue.get() if first_frame is None: print("Error: Could not read first frame from video source.") return display_loading_screen(result_queue, first_frame, LOADING_NUMBER_FRAMES, cv2) camera_movement_estimator = CameraMovementEstimator(first_frame) view_transformer = ViewTransformer() speed_and_distance_estimator = SpeedAndDistance_Estimator() player_assigner = PlayerBallAssigner() frame_list = [] with ThreadPoolExecutor(max_workers=cpu_count()-3 if cpu_count()>6 else cpu_count()) as executor: while not stop_event.is_set(): if not frame_queue.empty(): frame = frame_queue.get() if frame is None or frame.size == 0: continue frame_list.append(frame) if len(frame_list) == batch_size: future = executor.submit(process_video_batch, frame_list.copy(), result_queue, batch_num, camera_movement_estimator, view_transformer, speed_and_distance_estimator, player_assigner) frame_list = [] batch_num += 1 if stop_event.is_set() and frame_queue.empty(): break stop_event.set() capture_thread.join() def capture_frames(video_source, frame_queue, stop_event): reconnect_delay = 5 frame_read_timeout = 10 # seconds while not stop_event.is_set(): try: print(f"Attempting to open video source: {video_source}") cap = cv2.VideoCapture(video_source) if not cap.isOpened(): print(f"Error: Could not open video stream from source: {video_source}") time.sleep(reconnect_delay) continue print(f"Successfully opened video source: {video_source}") frame_read_start = time.time() while not stop_event.is_set(): ret, frame = cap.read() if not ret or frame is None or frame.size == 0: current_time = time.time() if current_time - frame_read_start > frame_read_timeout: print(f"Error: Failed to read valid frame for {frame_read_timeout} seconds. Reconnecting...") break if isinstance(video_source, str) and os.path.isfile(video_source): # If it's a local file print("Reached end of local video file. Restarting from beginning.") cap.set(cv2.CAP_PROP_POS_FRAMES, 0) # Reset to beginning of video continue frame_read_start = time.time() # Reset the timer on successful frame read if frame_queue.full(): try: frame_queue.get_nowait() except queue.Empty: pass frame_queue.put(frame) cap.release() except Exception as e: print(f"Error during frame capture: {e}") time.sleep(reconnect_delay) print("Capture frames thread stopped.") def process_frames(): global buffer, result_queue, isLoading while not stop_event.is_set(): try: frame = result_queue.get(timeout=1) except: continue if frame is None: break if isLoading: buffer.clear() isLoading = False buffer.append(frame) def generate(): while not stop_event.is_set(): if buffer: frame = buffer.popleft() ret, jpeg = cv2.imencode('.jpg', frame) if not ret: continue yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n') else: time.sleep(0.1) @app.get('/video_feed') def video_feed(): if not video_source: return HTMLResponse(content="

Please set the video source first.

") return StreamingResponse(generate(), media_type='multipart/x-mixed-replace; boundary=frame') if __name__ == '__main__': signal.signal(signal.SIGINT, lambda s, f: stop_event.set()) app.run(host='0.0.0.0', port=5000, debug=False, use_reloader=False)