-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompression.py
More file actions
57 lines (47 loc) · 1.91 KB
/
decompression.py
File metadata and controls
57 lines (47 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
from matplotlib.image import imread
import matplotlib.pyplot as plt
import os
import pywt
import threading
def decompress_video():
input_dir = 'C:/Users/hp/Desktop/piDecrypted'
output_dir = 'C:/Users/hp/Desktop/piDecrypted_Decompressed'
current_frame = 0
t= os.listdir(input_dir)
t.sort(key=lambda x: int(x.split(".")[0][5:]))
for frame in t:
if frame.endswith('.jpg'):
# Read image file
filepath = os.path.join(input_dir, frame)
print(filepath)
A = imread(filepath)
B = np.mean(A, -1); # Convert RGB to grayscale
## Wavelet Compression
n = 5
w = 'db1'
coeffs = pywt.wavedec2(B,wavelet=w,level=n)
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs)
Csort = np.sort(np.abs(coeff_arr.reshape(-1)))
keep = 0.1
thresh = Csort[int(np.floor((1-keep)*len(Csort)))]
ind = np.abs(coeff_arr) > thresh
Cfilt = coeff_arr * ind # Threshold small indices
coeffs_filt = pywt.array_to_coeffs(Cfilt,coeff_slices,output_format='wavedec2')
# Perform inverse DWT on filtered coefficients
Arecon = pywt.waverec2(coeffs_filt,wavelet=w)
# Plot and save reconstructed image
name = f"{output_dir}/frame{current_frame}.jpg"
plt.imsave(name, Arecon.astype('uint8'), cmap='gray')
current_frame += 1
def decompress_video_thread():
"""
This function runs the compress_video() function in a separate thread.
"""
t = threading.Thread(target=decompress_video)
t.start()
def on_decompress_button_click():
"""
This function is called when the user clicks the compress button.
"""
decompress_video_thread()