Arifzyn19 commited on
Commit
8b019f8
1 Parent(s): 6cd4595

Add application file

Browse files
Files changed (3) hide show
  1. README.md +11 -8
  2. main.py +45 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,13 +1,16 @@
1
  ---
2
- title: Upscale Api
3
- emoji: 👁
4
- colorFrom: red
5
- colorTo: indigo
6
- sdk: streamlit
7
- sdk_version: 1.37.1
 
8
  app_file: app.py
9
- pinned: false
10
  license: apache-2.0
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Face Real ESRGAN 2x 4x 8x
3
+ emoji: 😻
4
+ colorFrom: green
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 4.41.0
8
+ python_version: 3.11.9
9
  app_file: app.py
10
+ pinned: true
11
  license: apache-2.0
12
+ models:
13
+ - ai-forever/Real-ESRGAN
14
  ---
15
 
16
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
main.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ from RealESRGAN import RealESRGAN
4
+ import os
5
+
6
+ # Function to perform inference and upscale the image
7
+ def upscale_image(image_path, scale):
8
+ # Initialize device
9
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
+
11
+ # Initialize and load models
12
+ model = RealESRGAN(device, scale=scale, weights_only=False)
13
+ model.load_weights(f'weights/RealESRGAN_x{scale}.pth', download=True)
14
+
15
+ # Open the image from the file path
16
+ try:
17
+ image = Image.open(image_path)
18
+ except Exception as e:
19
+ print(f"Error opening the image: {e}")
20
+ return
21
+
22
+ # Perform inference
23
+ try:
24
+ result = model.predict(image.convert('RGB'))
25
+ except Exception as e:
26
+ print(f"Error during inference: {e}")
27
+ return
28
+
29
+ # Save the upscaled image
30
+ output_path = f'upscaled_image_x{scale}.png'
31
+ result.save(output_path, 'PNG')
32
+ print(f"Upscaled image saved to {output_path}")
33
+
34
+ if __name__ == '__main__':
35
+ # Path of the image to be upscaled
36
+ image_path = './groot.jpeg'
37
+
38
+ # Scaling factor (2x, 4x, or 8x)
39
+ scale = input("Enter the scaling factor (2, 4, or 8): ")
40
+
41
+ # Validate scale
42
+ if scale not in ['2', '4', '8']:
43
+ print("Invalid scale factor. Please enter 2, 4, or 8.")
44
+ else:
45
+ upscale_image(image_path, int(scale))
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ git+https://github.com/doevent/Real-ESRGAN.git
2
+ torch
3
+ Pillow
4
+ flask