project dijango stream and storage camera ip

 Creating a Django project to stream and store footage from an IP camera involves several steps. Below is a comprehensive guide to help you set up the project. 


### Prerequisites

1. Python installed (preferably version 3.8 or higher).

2. Django installed (`pip install django`).

3. OpenCV installed (`pip install opencv-python`).

4. A compatible IP camera.


### Step-by-Step Guide


#### 1. Set Up Django Project

First, create a new Django project and an app within it.


```bash

django-admin startproject camera_stream

cd camera_stream

django-admin startapp streamapp

```


#### 2. Configure Django Settings

Edit `camera_stream/settings.py` to include your new app.


```python

INSTALLED_APPS = [

    ...

    'streamapp',

    ...

]

```


#### 3. Create Models

You might want to create a model to store video metadata. Edit `streamapp/models.py`:


```python

from django.db import models


class VideoStream(models.Model):

    title = models.CharField(max_length=100)

    description = models.TextField(blank=True, null=True)

    created_at = models.DateTimeField(auto_now_add=True)

    video_file = models.FileField(upload_to='videos/')

```


#### 4. Set Up Media Files

In `camera_stream/settings.py`, add configurations for media files:


```python

MEDIA_URL = '/media/'

MEDIA_ROOT = BASE_DIR / 'media'

```


Update `camera_stream/urls.py` to serve media files during development:


```python

from django.conf import settings

from django.conf.urls.static import static


urlpatterns = [

    ...

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

```


#### 5. Create Views for Streaming

Edit `streamapp/views.py` to create a view for streaming video from the IP camera:


```python

from django.shortcuts import render

from django.http import StreamingHttpResponse

import cv2


def gen(camera):

    while True:

        ret, frame = camera.read()

        if not ret:

            break

        _, jpeg = cv2.imencode('.jpg', frame)

        frame = jpeg.tobytes()

        yield (b'--frame\r\n'

               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')


def video_feed(request):

    ip_camera_url = 'http://your_ip_camera_address'

    camera = cv2.VideoCapture(ip_camera_url)

    return StreamingHttpResponse(gen(camera),

                                 content_type='multipart/x-mixed-replace; boundary=frame')

```


#### 6. Configure URLs

Update `streamapp/urls.py` to include the new view:


```python

from django.urls import path

from .views import video_feed


urlpatterns = [

    path('video_feed/', video_feed, name='video_feed'),

]

```


Include these URLs in the main URL configuration in `camera_stream/urls.py`:


```python

from django.contrib import admin

from django.urls import path, include


urlpatterns = [

    path('admin/', admin.site.urls),

    path('', include('streamapp.urls')),

]

```


#### 7. Create Templates

Create a template to display the video stream. Create a `templates` directory in your app and add a `video_stream.html` file in it:


```html

<!DOCTYPE html>

<html>

<head>

    <title>IP Camera Stream</title>

</head>

<body>

    <h1>IP Camera Stream</h1>

    <img src="{% url 'video_feed' %}" width="800" height="600">

</body>

</html>

```


Update the view to render this template:


```python

from django.shortcuts import render


def index(request):

    return render(request, 'video_stream.html')

```


Update `streamapp/urls.py` to include the index view:


```python

urlpatterns = [

    path('', index, name='index'),

    path('video_feed/', video_feed, name='video_feed'),

]

```


#### 8. Storing Video Footage

To store footage, you can modify the `gen` function to save frames to a video file:


```python

import cv2


def gen(camera):

    fourcc = cv2.VideoWriter_fourcc(*'XVID')

    out = cv2.VideoWriter('media/videos/output.avi', fourcc, 20.0, (640, 480))

    while True:

        ret, frame = camera.read()

        if not ret:

            break

        out.write(frame)

        _, jpeg = cv2.imencode('.jpg', frame)

        frame = jpeg.tobytes()

        yield (b'--frame\r\n'

               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

    camera.release()

    out.release()

```


### Running the Project

1. Apply migrations to create the database schema:

   ```bash

   python manage.py makemigrations

   python manage.py migrate

   ```


2. Create a superuser to access the Django admin:

   ```bash

   python manage.py createsuperuser

   ```


3. Start the development server:

   ```bash

   python manage.py runserver

   ```


Visit `http://127.0.0.1:8000/` to see your streaming IP camera video.


### Additional Enhancements

- Implement authentication to secure the stream.

- Add functionality to start/stop recording manually.

- Improve error handling and add logging for better debugging.


This setup should provide you with a basic Django project that streams and stores footage from an IP camera.