Profiling Energy Consumption in Image Processing with CodeCarbon: A Step Towards Sustainable Computing

Table of contents
- Introduction
- What Does the Code Do?
- Understanding CodeCarbon: What It Is and Why It’s Used?
- How CodeCarbon Tracks Energy Consumption?
- Operations Performed in This Script Using OpenCV
- Code Breakdown and Execution Flow - CODE-LINK
- 1. Importing Required Libraries
- 2. Initializing the Carbon Emissions Tracker
- 3. Loading an Image
- 4. Converting Image to Grayscale
- 5. Applying Edge Detection Using Canny Algorithm
- 6. Running a Computational Workload
- 7. Blending Two Images (Conditional Check)
- 8. Stopping the Carbon Emissions Tracker
- 9. Completion Message
- Detailed Explanation of the Sustainability Profiling Script: Sustainability Computation Index (SCI) - CODE-LINK
- 1. Overview of the Script
- 2. Importing Required Libraries
- 3. Setting Up the Log Directory
- 4. Defining the Sustainability Computation Index (SCI)
- 5. Capturing System Usage
- 6. Initializing Energy Tracking
- 7. Image Processing Task
- 8. Stopping Energy Tracking and Retrieving Data
- 9. Computing the SCI Score and Saving Metrics
- 10. Data Visualization
- Final Output
- Project: Energy Profiling of Image Processing Tasks- Visualization : CODE-LINK
- Step 1: Environment Setup & Logging
- Step 2: Energy & Resource Tracking Setup
- Step 3: Generating a Synthetic Image for Processing
- Step 4: Monitoring System Resource Usage (CPU & Memory)
- Step 5: Implementing Image Processing Methods
- Step 6: Running the Experiment & Collecting Data
- Step 7: Extracting Energy Data
- Step 8: Comparing & Visualizing Performance

Introduction
In the modern computing landscape, sustainability has become a crucial concern. With the increasing use of computationally expensive tasks, such as deep learning and image processing, it is vital to measure their energy consumption and environmental impact. This script performs image processing using OpenCV while tracking its energy consumption using CodeCarbon, a tool designed to estimate the carbon footprint of computational workloads. By executing various image operations, the script profiles how much energy is used and how emissions vary during execution.
What Does the Code Do?
The script follows a structured sequence of operations to perform image processing tasks while monitoring system energy consumption. It first initializes the CodeCarbon EmissionsTracker, loads an input image, processes it using grayscale conversion, edge detection, Gaussian blur, and rotation, and finally blends the processed images. Throughout execution, CodeCarbon tracks the power usage, calculates emissions, and logs the results for further analysis. This enables users to quantify the environmental impact of their computations and optimize workloads for energy efficiency.
Understanding CodeCarbon: What It Is and Why It’s Used?
CodeCarbon is an open-source tool designed to estimate the energy consumption and CO₂ emissions produced by Python scripts, particularly for AI/ML workloads. It helps developers and researchers measure their computational carbon footprint and make informed decisions to optimize energy efficiency.
How CodeCarbon Tracks Energy Consumption?
Energy Estimation Models: CodeCarbon estimates power consumption based on hardware specifications, such as CPU, GPU, RAM usage, and power draw.
Cloud & Local Tracking: It supports tracking on local machines (by monitoring power usage) and cloud platforms (by estimating based on region-specific energy sources).
Carbon Intensity Factor: CodeCarbon considers the electricity mix of the region where the computation is performed, assigning a carbon intensity factor (kg CO₂/kWh) to calculate the emissions.
Logging & Reporting: It stores all metrics in a CSV file, which helps in analyzing energy consumption patterns over time.
CodeCarbon starts tracking before the computations begin and stops after processing is completed. The final energy consumption and emissions data are logged in carbon_emissions.csv.
How CodeCarbon Tracks Energy Consumption?
Feature | Description |
Energy Estimation | Estimates power consumption based on CPU, GPU, and RAM usage. |
Cloud & Local Tracking | Tracks execution on local machines and cloud platforms by analyzing energy sources. |
Carbon Intensity Factor | Assigns a CO₂ intensity (kg CO₂/kWh) based on region-specific energy sources. |
Logging & Reporting | Stores metrics in a CSV file, enabling trend analysis over time. |
CodeCarbon - Visualized DashBoard
Introduction to OpenCV and Why It’s Used?
OpenCV (Open Source Computer Vision Library) is a widely used library for real-time image processing and computer vision applications. It provides fast and efficient functions for performing various operations on images, making it a preferred choice for image manipulation tasks.
Operations Performed in This Script Using OpenCV
Loading an Image
- The script reads an image file (Tom_Cruise.jpg) using cv2.imread(). If the file is missing, it exits with an error message to prevent unexpected behavior.
Converting Image to Grayscale
- The color image is converted to grayscale using cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), reducing the complexity of computations by removing color information.
Applying Canny Edge Detection
- Edge detection is performed using cv2.Canny(gray_image, 100, 200), where 100 and 200 are the threshold values that define the intensity gradient for edge detection.
Computationally Intensive Workload Simulation
To create a stress test for the system, the script runs a loop 100 times performing:
Gaussian Blur: Applied using cv2.GaussianBlur(edges, (5,5), 0) to reduce noise in the image.
Image Rotation: Performed using cv2.rotate(blurred, cv2.ROTATE_90_CLOCKWISE), simulating a computationally heavy operation.
Blending Processed Images
- If the blurred and rotated images have the same dimensions, they are merged using cv2.addWeighted(blurred, 0.5, rotated, 0.5, 0), where both images contribute 50% to the final result. If the dimensions do not match, an error message is printed.
Operations Performed in This Script Using OpenCV
Operation | Function | Purpose |
Loading Image | cv2.imread("Tom_Cruise.jpg") | Reads an image into memory. |
Grayscale Conversion | cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | Simplifies computations by removing color information. |
Edge Detection | cv2.Canny(gray_image, 100, 200) | Identifies object boundaries using Canny edge detection. |
Gaussian Blur | cv2.GaussianBlur(edges, (5,5), 0) | Reduces noise to enhance edge detection. |
Image Rotation | cv2.rotate(blurred, cv2.ROTATE_90_CLOCKWISE) | Simulates a computationally heavy transformation. |
Blending Images | cv2.addWeighted(blurred, 0.5, rotated, 0.5, 0) | Merges two processed images with equal weights. |
How CodeCarbon Detects Emissions in Deep Learning and Image Processing?
In deep learning, models require intensive GPU and CPU resources, leading to high energy consumption. CodeCarbon helps track emissions by analyzing:
Hardware Power Consumption: It estimates the power used by CPUs, GPUs, and RAM, considering their efficiency.
Deep Learning Workload Intensity: The library accounts for the duration and intensity of computations performed by DL frameworks (TensorFlow, PyTorch, OpenCV, etc.).
Energy Sources & Regional Impact: CodeCarbon uses grid energy mix data to determine whether power comes from renewable sources or fossil fuels, refining its emission calculations.
Comparison & Optimization: Researchers can compare multiple hardware configurations, optimization techniques, and batch sizes to determine which setup consumes the least energy.
While this script focuses on image processing, the same principles apply to deep learning models, making CodeCarbon a valuable tool for AI researchers to measure sustainability.
Why This Matters?
The growing adoption of AI, deep learning, and image processing has increased the demand for computational power. However, this comes at an environmental cost due to high energy consumption and carbon emissions. By integrating tools like CodeCarbon, developers can measure, analyze, and optimize their workloads to reduce energy waste and improve sustainability.
This script demonstrates a practical approach to profiling energy usage in an image processing task, providing insights into how computation affects environmental impact. Such techniques can be extended to optimize machine learning pipelines, scientific simulations, and large-scale data processing workflows for a greener and more efficient future.
Code Breakdown and Execution Flow - CODE-LINK
1. Importing Required Libraries
The script starts by importing necessary Python libraries:
codecarbon.EmissionsTracker: Tracks and logs energy consumption.
cv2 (OpenCV): Handles image processing operations.
numpy: Used for array operations.
time: Provides time-related functions.
2. Initializing the Carbon Emissions Tracker
tracker = EmissionsTracker(output_file="carbon_emissions.csv") |
The EmissionsTracker is initialized with a CSV output file named "carbon_emissions.csv".
It starts recording energy consumption from this point onward.
3. Loading an Image
print("Loading Image.....") |
The script attempts to read an image file named "Tom_Cruise.jpg".
If the image file is missing, an error message is displayed, and the program exits:
if image is None: |
- This ensures that the program does not proceed with an invalid input.
4. Converting Image to Grayscale
print("Converting to grayscale.....") |
The loaded image is converted from BGR (Blue-Green-Red) format to grayscale.
This reduces the complexity of the image by removing color channels, keeping only intensity values.
5. Applying Edge Detection Using Canny Algorithm
print("Applying Canny edge detection..") |
The Canny Edge Detection algorithm detects prominent edges in the grayscale image.
Threshold values 100 and 200 define the intensity gradient required to classify edges.
6. Running a Computational Workload
print("Running workload...") |
The script simulates an intensive computation workload by performing:
Gaussian Blur: Smooths the edges using a 5×5 kernel to reduce noise.
Image Rotation: Rotates the blurred image 90 degrees clockwise.
This loop runs 100 times, stressing the CPU and memory.
7. Blending Two Images (Conditional Check)
if blurred.shape == rotated.shape: |
If the shapes of the blurred and rotated images match, image blending is performed:
- cv2.addWeighted() merges two images with equal weight (0.5).
If shapes do not match, an error message is displayed.
8. Stopping the Carbon Emissions Tracker
tracker.stop() |
Stops the energy tracking process and logs results in "carbon_emissions.csv".
The logged data includes:
Energy consumed (kWh)
CO₂ emissions (kg)
9. Completion Message
print("Profiling complete! Check logs for energy usage") |
Run-ID | Experiment-ID | Duration | Emissions | Emissions-Rate | CPU-Power | Ram-Power | CPU-Energy | Ram-Energy | Energy -Consumed |
cbdba861-93c8-4999-befb-825fa0fa106a | 5b0fa12a-3dd7-45bb-9766-cc326314d9f1 | 4.764563 | 1.60E-05 | 3.35E-06 | 42.5 | 0.110868 | 5.58E-05 | 1.45E-07 | 5.60E-05 |
e2422456-6076-4940-894b-dba02f76c5f5 | 5b0fa12a-3dd7-45bb-9766-cc326314d9f1 | 4.455262 | 1.49E-05 | 3.35E-06 | 42.5 | 0.111108 | 5.22E-05 | 1.36E-07 | 5.23E-05 |
0cf0c38e-fab8-4c48-afe4-1079cd957cfa | 5b0fa12a-3dd7-45bb-9766-cc326314d9f1 | 5.119229 | 2.07E-05 | 4.04E-06 | 42.5 | 0.114919 | 7.23E-05 | 1.94E-07 | 7.25E-05 |
Detailed Explanation of the Sustainability Profiling Script: Sustainability Computation Index (SCI) - CODE-LINK
This script is designed to measure and analyze energy consumption, system performance, and carbon emissions during an image processing task. It achieves this by tracking CPU and memory usage, monitoring energy consumption, and computing a Sustainability Computation Index (SCI) to evaluate the environmental impact of running the task. The script also provides visualizations to help understand trends in energy usage and system performance.
1. Overview of the Script
The script follows these main steps:
Setup and Configuration
Import necessary libraries for system monitoring, energy tracking, and image processing.
Create a logging directory to store the results.
System Performance Monitoring
- Capture CPU, memory, and RAM usage before and after processing.
Energy Consumption and Emission Tracking
- Use the CodeCarbon library to measure power consumption and CO₂ emissions.
Image Processing Simulation
- Apply Gaussian Blur and Canny Edge Detection on a sample image.
Sustainability Computation
- Compute the Sustainability Computation Index (SCI) to quantify the environmental impact.
Data Visualization
- Generate graphs showing energy consumption and memory usage.
Save and Log the Results
- Store energy usage, emissions, and SCI scores in a CSV file for future analysis.
2. Importing Required Libraries
import os |
Library | Purpose |
os | Handles file operations (creating directories, managing logs). |
time | Measures execution time for performance tracking. |
numpy | Creates and manipulates images using arrays. |
pandas | Stores and processes data efficiently in CSV format. |
matplotlib.pyplot & seaborn | Visualizes system performance and energy consumption. |
psutil | Monitors CPU, RAM, and memory usage before and after processing. |
codecarbon | Tracks energy consumption and CO₂ emissions during execution. |
The script relies on multiple Python libraries for different functionalities:
3. Setting Up the Log Directory
To store logs and results, a directory named logs is created if it does not exist:
log_dir = "logs" |
This ensures that all the emission and performance data is saved in an organized manner.
4. Defining the Sustainability Computation Index (SCI)
The script defines a Sustainability Computation Index (SCI) to measure the environmental impact of the image processing task. The formula used is:
SCI=((Energyconsumed*I)+M ) / R |
Parameter | Meaning |
I = 0.4 | Marginal carbon emissions factor (kg CO₂/kWh), depends on energy source. |
M = 10 | Embodied carbon emissions per functional unit (kg CO₂), representing manufacturing costs. |
R = 1 | Functional unit, here representing one processed image. |
I = 0.4 |
This formula helps quantify how much energy and emissions are used for processing each image.
5. Capturing System Usage
A function is created to fetch real-time system resource usage before and after the image processing task.
def get_system_usage(): |
This function returns a dictionary containing:
Metric | Description |
cpu | CPU usage in percentage. |
memory | Percentage of total memory used. |
ram_used | RAM usage in gigabytes. |
By calling this function before and after the image processing step, the script can measure how much CPU and RAM resources were consumed.
6. Initializing Energy Tracking
To track the power consumption and carbon footprint, the CodeCarbon EmissionsTracker is used:
tracker = EmissionsTracker(output_dir=log_dir, output_file="carbon_emission.csv") |
This starts logging the energy consumed and carbon emissions produced by the script while it runs.
7. Image Processing Task
The script simulates an image processing task using OpenCV (cv2) to apply blurring and edge detection on an image.
image = np.ones((512, 512, 3), dtype=np.uint8) * 255 # Creating a blank white image |
Step | Functionality |
Create Image | A blank 512x512 white image is created using NumPy. |
Gaussian Blur | Reduces noise by blurring the image. |
Canny Edge Detection | Detects edges by identifying intensity changes in the image. |
Before and after this step, system performance metrics are recorded.
8. Stopping Energy Tracking and Retrieving Data
Once the processing is complete, the script stops the tracker and reads the recorded energy usage from carbon_emission.csv.
tracker.stop() |
This extracts energy consumed (in kWh) and carbon emissions (in kg CO₂) from the logged data.
9. Computing the SCI Score and Saving Metrics
The SCI score is calculated using the previously defined formula:
SCI = ((energy_consumed * I) + M) / R |
All collected metrics are stored in a structured DataFrame:
data = { |
This makes it easy to store and analyze results.
10. Data Visualization
The script generates two graphs:
Bar graph showing energy consumption and emissions
Line graph showing memory usage before and after processing
plt.figure(figsize=(10, 5)) |
plt.figure(figsize=(10, 5)) |
Final Output
The results are saved to a CSV file:
df_metrics.to_csv(os.path.join(log_dir, "sci_results.csv"), index=False) |
This allows users to analyze the energy efficiency of the process.
Project: Energy Profiling of Image Processing Tasks- Visualization : CODE-LINK
This script evaluates energy consumption, CPU usage, memory usage, and execution time for different image processing methods. The goal is to highlight the efficiency differences between low-efficiency (brute force) and high-efficiency (optimized) techniques using:
CodeCarbon → Tracks power usage and emissions.
psutil → Monitors CPU & RAM usage.
OpenCV (cv2) → Performs image processing.
Matplotlib & Seaborn → Creates visual comparisons.
Step 1: Environment Setup & Logging
Before running any process, we ensure a clean working environment. If a lock file from a previous CodeCarbon run exists, we remove it. This prevents conflicts in logging energy consumption.
import os |
**🔹 Why?
**✔ CodeCarbon creates a lock file during execution, which might interfere with new runs.
✔ Creating a log directory ensures energy data is stored for future analysis.
Step 2: Energy & Resource Tracking Setup
To monitor energy usage, we initialize the EmissionsTracker from CodeCarbon. It logs power consumption and carbon emissions while our script runs.
tracker = EmissionsTracker( |
**🔹 Why?
**✔ Helps analyze the environmental impact of different processing methods.
✔ Captures energy usage per second for fine-grained monitoring.
Step 3: Generating a Synthetic Image for Processing
Instead of using real images, we generate a random grayscale image (512x512 pixels). This ensures we always have a consistent test case.
|
**🔹 Why?
**✔ Using a fixed synthetic image ensures reproducibility.
✔ A grayscale image (1 channel) simplifies computation compared to RGB (3 channels).
Step 4: Monitoring System Resource Usage (CPU & Memory)
Before and after each image processing task, we record CPU and memory usage using psutil.
def get_system_usage(): |
**🔹 Why?
**✔ Provides real-time CPU and RAM usage data.
✔ Helps compare resource utilization across different methods.
Step 5: Implementing Image Processing Methods
Low-Efficiency Processing (Brute Force)
This method applies multiple redundant transformations inside a loop, making it inefficient.
def process_image(image, efficiency="low"): |
**🔹 Why?
**✔ The low-efficiency method repeatedly processes the same data, wasting resources.
✔ The high-efficiency method minimizes redundant operations, reducing execution time and energy use.
Step 6: Running the Experiment & Collecting Data
We apply both methods and track performance.
tracker.start() |
✔ The CodeCarbon tracker runs before and after processing to measure energy consumption.
Step 7: Extracting Energy Data
We extract energy and emissions data from the saved logs.
log_file_path = os.path.join(log_dir, "carbon_emission.csv") |
✔ Helps quantify the energy footprint of each method.
Step 8: Comparing & Visualizing Performance
Comparison Table
Metric | Low-Efficiency | High-Efficiency |
CPU Usage (Before) | X% | Y% |
CPU Usage (After) | X+Δ% | Y+Δ% |
Memory Usage (Before) | X% | Y% |
Memory Usage (After) | X+Δ% | Y+Δ% |
Processing Time | High | Low |
Energy Consumption | High | Low |
Carbon Emissions | High | Low |
Bar Chart Visualization
sns.set(style="darkgrid") |
Visualizes the performance differences between both methods.
Conclusion:
As AI and computational workloads grow, optimizing energy consumption is crucial for sustainable development. CodeCarbon leverages AI/ML-based estimation models to track power usage across CPUs, GPUs, and cloud platforms while factoring in regional carbon intensity. By profiling energy consumption in deep learning and image processing, it helps identify inefficiencies and optimize workloads for lower emissions.
This enables developers to make informed choices, such as using energy-efficient hardware or greener cloud providers. Integrating carbon tracking tools like CodeCarbon ensures that technological advancements remain environmentally responsible, promoting a sustainable future in AI and software development.
Subscribe to my newsletter
Read articles from ADITYA BHATTACHARYA directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

ADITYA BHATTACHARYA
ADITYA BHATTACHARYA
A fervent and a passionate fellow for Computer Science and Research, trying to bridge the gap between real life and code life. From print("Hello World ! ") to writing Scalable Language Codes, I am yet to discover the emerging future of AI in our forecasting life of Modern Technology.