Camera Webserver

Create a Python script with name like CameraWebServer.py and contents as:

#!/usr/bin/env python3

import jetson_utils
import cv2
import numpy as np
from flask import Flask, Response, render_template
import cv2
import threading

# Initialize cameras using jetson.utils.videoSource
camera1 = jetson_utils.videoSource("csi://0")  # Adjust the source string as needed
camera2 = jetson_utils.videoSource("csi://1")  # For the second camera

def generate_frame(camera):
    while True:
        # Capture the frame
        cuda_img = camera.Capture()

        # Convert cudaImage to numpy array
        numpy_img = jetson_utils.cudaToNumpy(cuda_img)

        # Encode the frame in JPEG format
        ret, buffer = cv2.imencode('.jpg', numpy_img)

        # Ensure the frame was successfully encoded
        if not ret:
            continue

        frame = buffer.tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


app = Flask(__name__)

@app.route('/')
def index():
    # Serve the HTML template
    return render_template('index.html')
    
@app.route('/video_feed_1')
def video_feed_1():
    return Response(generate_frame(camera1),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/video_feed_2')
def video_feed_2():
    return Response(generate_frame(camera2),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, threaded=True)

In the same folder as CameraWebServer.py, create templates/index.html with contents as shown in the picture below:

Install Flake with: pip install flask if it is not installed.

Run the script in terminal like:

 

As indicated by the tip above, in the Explorer of a PC on the same network, go to page http://192.168.110.97:5000 to see the camera captured by the two cameras. 

 

 

 

Problems Met

A camera stops working after some warnings: refresh the page will get them back to work again.