Professional Documents
Culture Documents
SENARIO:
You are part of a team, building a real-time surveillance system that performs
analytics on live camera streams. The system needs to process video frames in real-time,
apply analytics, and store or transmit the results. You are tasked with developing a
critical component of the system.
SCOPE:
The user might ask for the camera footage for a particular time period. Provided the
duration and timestamp, the application must provide the respective footage to the user.
The timestamp and the duration of the video will be given by the user
VIDEO SERVELLINCE PIPELINE:
DATA INGESTION:
In this pipeline first step to ingest data(video streaming) from camera-1, Using
OpenCV-python library video streaming takes place.
COMPUTE:
Data from video stream is analyses and prepared based on requirement into 25fps.
Data after preparation it converted to batches under video length and stream duration.
STORAGE:
Here, to store data in raw form, we use json file system. But to convert and use for
future data analysis, used Microsoft Azure Sql Database engine( as Datawarehouse)
Here, to monitor we use python logging function and also handled error using
Exception handing.
PAGE 1
DATA VISUALIZATION:
In this task we didn’t used data visualization tool as power bi, etc. But as a Data
Engineering pipeline we represent data viz as end of pipeline future data scientist take
care of task.
REQUIREMENTS:
In task 1 and 2, They are several libraries are used based on requirement,
Before working with code install these libraries
Those Are:
1.Python
2.Opencv
3.Json
4.Imageio
5.Os
6.Time
7.pyodbc
Here also, used Microsoft Azure Sql Database for data Storage in Relational Format.
So, Need to have Azure account and sql db in resource group.
PAGE 2
TASK 1:
Write Python code for a real-time video analytics pipeline that performs the following
tasks: For any configurations related tasks create a python config file and must create a
SQL Database for storing information.
CODE:
import cv2
import json
import time
import os
import math
import pyodbc
class img_stream_process:
def __init__(self, camera_id, geo_location, output_folder,
config_filename):
self.camera_id = camera_id
self.geo_location = geo_location
self.output_folder = output_folder
self.frame_id = 0
self.cap = cv2.VideoCapture(camera_id)
self.config_filename = config_filename
self.batch_duration = self.read_config_duration()[0]
self.batch_size = self.read_config_duration()[1]
def read_config_duration(self):
# Read the duration from the config file
with open(self.config_filename, "r") as config_file:
config_data = json.load(config_file)
duration = config_data.get("duration")
batch_size = config_data.get("batch_size")
return duration,batch_size
PAGE 3
def video_capture(self):
# Check if the video stream was opened successfully
if not self.cap.isOpened():
print("Error: Could not open video source.")
exit()
COMMENT:
CODE:
def frame_processing(self):
# Set the desired frames per second (fps)
desired_fps = 25
frame_count = 0
frame_to_save = None
while True:
# Read a frame from the video stream
ret, frame = self.cap.read()
frame_count += 1
PAGE 4
cv2.imwrite(frame_filename, frame_to_save) # Save the
frame as an image
# Reuse the saved frame for the next 24 frames within the same
second
if frame_to_save is not None:
cv2.imshow("Frame", frame_to_save)
COMMENT:
Based on requirements given in scenario in, 25 Frames taken in one second but
only one frame is used rest 24 frames are reused same file.
Function followed by desired_frame, frame_count,frame_to_save. While loop
read the each frame, converted to one frame stored as a file.
Each file with frame_id, geo_location, camera_id,image file path will be form a
json file. It will append until video will be streamed.
At the end by pressing letter “r” the streaming will be stop.
III. Batching:
CODE:
def batch_frames(self):
# Calculate the number of batches.
num_batches = math.ceil(self.batch_duration / self.batch_size)
PAGE 5
for i in range(num_batches):
batch_id = i + 1
# Calculate the starting and ending frame IDs for the batch.
starting_frame_id = batch_id * self.batch_size -
self.batch_size + 1
ending_frame_id = min(starting_frame_id + self.batch_size - 1,
self.batch_duration)
COMMENT:
In this process duration of video with batch size is mentioned in config file as per
requirements.
It extract data from config file and followed by forming a batch of dictionary
which contain batch id, starting frame id, ending frame id, timestamp all are
append into a list called batches.
Batch id calculated by using number of batches(duration/batches), starting frame
id is cal by “batch_id * self.batch_size - self.batch_size + 1”
IV. DATA STORAGE:
CODE:
import json
import pyodbc
# Create a connection to the database
conn = pyodbc.connect(Driver='{ODBC Driver 18 for SQL
Server}',Server="covid19-srv01.database.windows.net",Database="covid-
db",Uid="myadmin",Pwd="Hooked@8",Encrypt="yes",TrustServerCertificate="no"
,Connection_Timeout=30)
PAGE 6
# Create a cursor object for executing SQL queries
cursor = conn.cursor()
COMMENT:
def setup_logger(self):
logger = logging.getLogger("frame_processor")
logger.setLevel(logging.DEBUG)
PAGE 7
file_handler.setLevel(logging.DEBUG)
return logger
COMMENT:
CODE:
PAGE 8
self.json_data.append(frame_info)
# Increment frame ID
self.frame_id += 1
except Exception as e:
self.logger.error(f"Error processing frame: {str(e)}")
COMMENT:
CODE:
Link:
Comment:
I had changed the code which work for two cameras.
It performance is depend on CPU and cameras data processing speed.
TASK 2:
Write a user driven python program that accepts,
➢ TIMESTAMP
Based on the above information, iterate through the batch information in the Database.
Create a metadata out of it which will be helpful in gathering the frame information from
the json file. Once the necessary frames are gathered convert them to a mp4 file and
present them to the user.
PAGE 9
CODE:
LINK
Comment:
import imageio
import json
# Loop through the frame metadata and add frames to the list
for frame_info in frame_metadata:
frame = imageio.imread(frame_info["image_path"]) # Load the frame
image
frames.append(frame)
PAGE 10