Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

6/19/23, 7:11 PM KLTimagecompress-Copy1

In [1]: import time


tik = time.time()

In [2]: import platform


import psutil
import subprocess
import sys

def get_ubuntu_version():
try:
result = subprocess.run(['lsb_release', '-r'], capture_output=True, text=Tr
if result.returncode == 0:
output = result.stdout.strip()
version = output.split('\t')[1]
return version
else:
return "Unknown"
except FileNotFoundError:
return "Unknown"

def get_system_specifications():
system = platform.uname()
os_version = platform.version()
processor = system.processor
memory = psutil.virtual_memory()
disk_usage = psutil.disk_usage('/')
ubuntu_ver = get_ubuntu_version()
python_ver = sys.version.split()[0]

print(f"Operating System: {system.system} {os_version}")


print(f"Ubuntu Version: {ubuntu_ver}")
print(f"Python Version: {python_ver}")
print(f"Processor: {processor}")
print(f"Memory - Total: {round(memory.total / (1024**3), 2)} GB")
print(f"Memory - Available: {round(memory.available / (1024**3), 2)} GB")
print(f"Disk Usage - Total: {round(disk_usage.total / (1024**3), 2)} GB")
print(f"Disk Usage - Used: {round(disk_usage.used / (1024**3), 2)} GB")
print(f"Disk Usage - Free: {round(disk_usage.free / (1024**3), 2)} GB")
print(sys.getrecursionlimit())
get_system_specifications()

Operating System: Windows 10.0.17763


Ubuntu Version: Unknown
Python Version: 3.10.9
Processor: AMD64 Family 23 Model 49 Stepping 0, AuthenticAMD
Memory - Total: 27.95 GB
Memory - Available: 22.59 GB
Disk Usage - Total: 126.45 GB
Disk Usage - Used: 19.22 GB
Disk Usage - Free: 107.23 GB
3000

In [3]: #import library


import os
os.sys.path
import matplotlib.pyplot as plt
import cv2
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
import psutil
import GPUtil

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 1/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

def calculate_image_size(image):
# Menghitung ukuran dalam bytes dari gambar
_, buffer = cv2.imencode('.jpg', image)
size_bytes = len(buffer.tobytes())
return size_bytes

In [4]: %%time
def klt_compression(image, num_coeffs):
# Konversi gambar ke skala keabuan
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Ubah gambar menjadi array float32


float_image = np.float32(gray_image)

# Hitung matriks kovarians


cov_matrix = np.cov(float_image.T)

# Hitung eigenvektor dan eigennilai


eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)

# Ambil eigenvektor dengan nilai eigen terbesar


sorted_indices = np.argsort(eigenvalues)[::-1][:num_coeffs]
selected_vectors = eigenvectors[:, sorted_indices]

# Kompress gambar dengan KLT


compressed_image = np.dot(float_image, selected_vectors)

# Rekonstruksi gambar
reconstructed_image = np.dot(compressed_image, selected_vectors.T)

# Kembalikan gambar yang direkonstruksi


return reconstructed_image
def get_cpu_usage():
cpu_percent = psutil.cpu_percent(interval=1)
return cpu_percent

def get_gpu_usage():
gpus = GPUtil.getGPUs()
gpu_percent = []
for gpu in gpus:
gpu_percent.append(gpu.load * 100)
return gpu_percent

def get_memory_usage():
memory = psutil.virtual_memory()
total_memory = memory.total
used_memory = memory.used
memory_percent = (used_memory / total_memory) * 100
return memory_percent
def get_file_size(file_path):
if os.path.isfile(file_path):
file_size = os.path.getsize(file_path)
return file_size
else:
return -1 # -1 menandakan file tidak ditemukan

# Daftar nama file gambar


image_files = ['jyothi-kumar.jpg','adrien-bruneau.jpg','bryan-garces.jpg','david-em

# Tentukan jumlah koefisien KLT yang ingin digunakan


num_coeffs = 50

# Proses kompresi dan rekonstruksi untuk setiap gambar

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 2/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

for file in image_files:


# Load gambar
image_path = 'image_compress/image_compress/' + file
image = cv2.imread(image_path)
new_width=500
new_height=500
image=cv2.resize(image,(new_width,new_height))

# Lakukan kompresi KLT


compressed_image = klt_compression(image, num_coeffs)

# Konversi gambar yang direkonstruksi menjadi format uint8


reconstructed_image = np.uint8(compressed_image)

# Tampilkan gambar asli dan gambar yang direkonstruksi


print('nama file :',file)
cv2.imshow('Original Image', image)
plt.imshow(image)
plt.show()
size = calculate_image_size(image)
print(f"Ukuran gambar original: {size} bytes")
print(f'original image: {image.shape}')
cv2.imshow('Reconstructed Image', reconstructed_image)
plt.imshow(reconstructed_image)
plt.show()
size = calculate_image_size(reconstructed_image)
print(f"Ukuran gambar compress: {size} bytes")
print(f'compress image: {reconstructed_image.shape}')
tok = time.time()
print(f'time image {file} by notebook: {round(tok-tik, 2)} sec')
print("\n")
# cv2.waitKey(0)
# cv2.destroyAllWindows()

if __name__ == '__main__':
cpu_usage = get_cpu_usage()
gpu_usage = get_gpu_usage()
memory_usage = get_memory_usage()

print(f'CPU Usage: {cpu_usage}%')


if len(gpu_usage) > 0:
for i, gpu in enumerate(gpu_usage):
print(f'GPU {i} Usage: {gpu}%')
else:
print('No GPUs found.')
print(f'Memory Usage: {memory_usage}%')

tok = time.time()
print(f'total time KLT by notebook: {round(tok-tik, 2)} sec')

nama file : jyothi-kumar.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 3/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

Ukuran gambar original: 71053 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 49665 bytes


compress image: (500, 500)
time image jyothi-kumar.jpg by notebook: 4.68 sec

nama file : adrien-bruneau.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 4/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

Ukuran gambar original: 92213 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 115103 bytes


compress image: (500, 500)
time image adrien-bruneau.jpg by notebook: 5.22 sec

nama file : bryan-garces.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 5/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

Ukuran gambar original: 101163 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 81581 bytes


compress image: (500, 500)
time image bryan-garces.jpg by notebook: 5.82 sec

nama file : david-emrich.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 6/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

Ukuran gambar original: 151922 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 152733 bytes


compress image: (500, 500)
time image david-emrich.jpg by notebook: 6.36 sec

nama file : emmanuelle-magnenat.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 7/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

Ukuran gambar original: 97210 bytes


original image: (500, 500, 3)

Ukuran gambar compress: 113878 bytes


compress image: (500, 500)
time image emmanuelle-magnenat.jpg by notebook: 6.9 sec

nama file : keiron-crasktellanos.jpg

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 8/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

Ukuran gambar original: 78885 bytes


original image: (500, 500, 3)

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 9/10
6/19/23, 7:11 PM KLTimagecompress-Copy1

Ukuran gambar compress: 94535 bytes


compress image: (500, 500)
time image keiron-crasktellanos.jpg by notebook: 7.47 sec

CPU Usage: 12.9%


No GPUs found.
Memory Usage: 20.090378855551467%
total time KLT by notebook: 8.47 sec
CPU times: total: 9.02 s
Wall time: 4.41 s

https://15f3-74-235-47-136.ngrok-free.app/nbconvert/html/Downloads/cloud/KLTimagecompress-Copy1.ipynb?download=false 10/10

You might also like