[
  {
    "path": "README.md",
    "content": "# Fast Real-time Object Detection with High-Res Output\n\n![Python](https://img.shields.io/badge/Python-3.8%2B-3776AB?style=for-the-badge&logo=python&logoColor=white)\n![YOLOv5](https://img.shields.io/badge/YOLOv5n6-6.1-orange?style=for-the-badge&logo=YOLO&logoColor=white)\n![CUDA](https://img.shields.io/badge/CUDA-Enabled-76B900?style=for-the-badge&logo=nvidia&logoColor=white)\n![OpenCV](https://img.shields.io/badge/OpenCV-3.4.2-5C3EE8?style=for-the-badge&logo=opencv&logoColor=white)\n![Gradio](https://img.shields.io/badge/Gradio-3.1.4-blueviolet?style=for-the-badge&logo=gradio&logoColor=white)\n[![Deployed on Hugging Face](https://img.shields.io/badge/Deployed%20on-Hugging%20Face-yellow?style=for-the-badge&logo=huggingface&logoColor=white)](https://huggingface.co/spaces/Sanshruth/CCTV_YOLO?embed=true)\n![image](https://github.com/user-attachments/assets/2cbae46f-e9de-48db-96f8-d439536ad703)\n\n\n## Overview\n\nLive stream of Las Vegas sidewalk traffic cam, processed with YOLOv5n6 on low-resolution frames, with results drawn on high-resolution frames.\n\nThis project demonstrates **real-time object detection** using the YOLOv5n6 model with **low-resolution inference** for high-speed processing, while drawing the results on **high-resolution frames**. The object detection pipeline is deployed as a Gradio app and streams live data from an external camera feed.\n\n### Features\n\n- **YOLOv5n6 Model**: Pre-trained object detection model optimized for speed and accuracy.\n- **Low-resolution Processing**: Efficiently processes frames in low resolution (320x180) while mapping results to high-res images.\n- **Gradio Interface**: Interactive Gradio interface with real-time video stream processing.\n- **CUDA Support**: Optimized for CUDA-enabled GPUs, ensuring fast inference times.\n\n### Model Details\n\n- **Model**: YOLOv5n6 (`yolov5n6.pt`)\n- **Confidence Threshold**: 0.25\n- **IOU Threshold**: 0.45\n- **Max Detections**: 100 objects per frame\n\n### How It Works\n\nThe pipeline processes a live video stream, detecting objects in each frame using YOLOv5n6. Bounding boxes are drawn on the high-resolution frames based on detections from the low-resolution inference.\n\n### Usage\n\n1. Clone the repository and install the dependencies:\n    ```bash\n    git clone https://github.com/SanshruthR/CCTV_YOLO.git\n    cd cctv-yolo\n    pip install -r requirements.txt\n    ```\n\n2. Run the script:\n    ```bash\n    python app.py\n    ```\n\n3. Access the Gradio interface and view the live stream processed with YOLOv5n6.\n\n### Deployment\n\nThis project is deployed on **Hugging Face Spaces**. You can interact with the app via the following link:\n\n[Live Demo on Hugging Face](https://huggingface.co/spaces/Sanshruth/CCTV_YOLO?embed=true)\n\n### License\n\nThis project is licensed under the MIT License.\n"
  },
  {
    "path": "app.py",
    "content": "from ultralytics import YOLO\nimport cv2\nimport numpy as np\nimport gradio as gr\nimport torch\n\n# Load YOLOv5n6 model\nmodel = YOLO('yolov5n6.pt')\n\n# Set the confidence threshold and IOU\nmodel.conf = 0.25  # confidence threshold\nmodel.iou = 0.45  # IOU threshold\nmodel.agnostic = False\nmodel.multi_label = False\nmodel.max_det = 100  # max number of detections\n\n# Low-resolution for inference \n#use frame_count += 3\n#LOW_RES = (500, 600) \n\n#Faster inference but less accurate \n#LOW_RES = (320, 180) \n\n#Fastest inference but low accuracy, change frame_count to += 2\nLOW_RES = (300, 300)\n\ndef detect_and_draw(frame):\n    # Create low-res copy\n    low_res_frame = cv2.resize(frame, LOW_RES)\n    \n    # Perform detection\n    results = model(low_res_frame, verbose=False)\n\n    # Scale bounding boxes\n    scale_x = frame.shape[1] / LOW_RES[0]\n    scale_y = frame.shape[0] / LOW_RES[1]\n\n    # Draw bounding boxes on high-res frame\n    for detection in results[0].boxes.data:\n        x1, y1, x2, y2, conf, cls = detection\n        x1, y1, x2, y2 = int(x1*scale_x), int(y1*scale_y), int(x2*scale_x), int(y2*scale_y)\n        label = f\"{results[0].names[int(cls)]} {conf:.2f}\"\n        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)\n        cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)\n\n    return frame\n\n# Define your stream URL\nstream_url = \"https://edge01.london.nginx.hdontap.com/hosb5/ng_showcase-coke_bottle-street_fixed.stream/chunklist_w464099566.m3u8\"\n\ndef process_stream():\n    cap = cv2.VideoCapture(stream_url)\n    frame_count = 0\n    while cap.isOpened():\n        ret, frame = cap.read()\n        if not ret:\n            break\n\n        frame_count += 2  #change to 3 for higher res\n        if frame_count % 30 == 0:\n            result = detect_and_draw(frame)\n            result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)\n            yield result_rgb\n\n    cap.release()\n\niface = gr.Interface(\n    fn=process_stream,\n    inputs=None,\n    outputs=\"image\",\n    live=True,\n    title=\"Fast Real-time Object Detection with High-Res Output\",\n    description=\"Live stream processed with YOLOv5n6 on low-res frames, results drawn on high-res frames.\"\n)\n\nif __name__ == \"__main__\":\n    if torch.cuda.is_available():\n        model.to('cuda')\n    #iface.queue()\n    iface.launch()\n"
  },
  {
    "path": "requirements.txt",
    "content": "ultralytics\nopencv-python\ngradio\ntorch\n"
  }
]