universalml commited on
Commit
d5d0fa7
·
1 Parent(s): 1f434d6

files uploaded

Browse files
Files changed (2) hide show
  1. app.py +91 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import io
3
+ import os
4
+ import torch
5
+ from torchvision import transforms
6
+ import numpy as np
7
+ from PIL import Image
8
+ import matplotlib.pyplot as plt
9
+ from pytorch_msssim import ms_ssim
10
+
11
+ # loading the models
12
+ from compressai.zoo import bmshj2018_factorized
13
+ from compressai.zoo import bmshj2018_hyperprior
14
+ from compressai.zoo import cheng2020_anchor
15
+
16
+ import gradio as gr
17
+
18
+ # function to compress the image
19
+ def image_compress(input_img):
20
+ # removing the file from folder
21
+
22
+ folder_path = "./result/"
23
+ file_name = "compressed.jpg"
24
+
25
+ file_path = os.path.join(folder_path, file_name)
26
+ # checking the compressed file exist or not
27
+ if file_path:
28
+ try:
29
+ os.remove(file_path)
30
+ print(f"File {file_name} deleted successfully.")
31
+ except OSError as e:
32
+ # print(f"Error: {file_path} - {e.strerror}")
33
+ pass
34
+
35
+ # checking the device
36
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
37
+
38
+ net = bmshj2018_factorized(quality=1, pretrained=True).eval().to(device)
39
+
40
+ print(f'Parameters: {sum(p.numel() for p in net.parameters())}')
41
+
42
+ # img = Image.open(input_img).convert('RGB')
43
+ img = input_img.convert('RGB')
44
+ x = transforms.ToTensor()(img).unsqueeze(0).to(device)
45
+
46
+ with torch.no_grad():
47
+ out_net = net.forward(x)
48
+ out_net['x_hat'].clamp_(0, 1)
49
+
50
+ rec_net = transforms.ToPILImage()(out_net['x_hat'].squeeze().cpu())
51
+
52
+ rec_net.save("./result/compressed.jpg")
53
+
54
+ output_image = "./result/compressed.jpg"
55
+
56
+ # print("Your input image path is:::")
57
+ # print(input_img.name)
58
+
59
+ # Split the file path into components
60
+ # components = input_img.split('/')
61
+
62
+ # Get the last component (file name and extension)
63
+ # file_name_with_extension = components[-1]
64
+
65
+ # Split the file name and extension using rsplit()
66
+ # file_name, file_extension = file_name_with_extension.rsplit('.', 1)
67
+
68
+ # rec_net.save("./result/"+file_name+".jpg")
69
+
70
+ # output_image = "./result/" + file_name + ".jpg"
71
+
72
+ # calculatinig the reduction size
73
+ # file_size_bytes1 = os.path.getsize(input_img)
74
+ # file_size_bytes2 = os.path.getsize(output_image)
75
+ # file_size_mb1 = file_size_bytes1 / 1000000
76
+ # file_size_mb2 = file_size_bytes2 / 1000000
77
+ # final_percent = ((file_size_mb1 - file_size_mb2) / file_size_mb1) * 100
78
+
79
+ return output_image, output_image
80
+
81
+ # defining the components the inference
82
+ input_component = gr.Image(type="pil")
83
+ output_component = [gr.Image(type="pil"), gr.File(label="Download", extension=".png")]
84
+
85
+ interface = gr.Interface(
86
+ fn=image_compress,
87
+ inputs=input_component,
88
+ outputs=output_component
89
+ )
90
+
91
+ interface.launch()
requirements.txt ADDED
File without changes