NeuralFalcon commited on
Commit
58407db
·
verified ·
1 Parent(s): 0bdc2ce

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import os
4
+ import zipfile
5
+ import uuid
6
+ import gradio as gr
7
+
8
+
9
+
10
+ def remove_watermark_area(original_image, text_mask_path):
11
+ # Ensure the mask is binary
12
+ text_mask = cv2.imread(text_mask_path, cv2.IMREAD_GRAYSCALE)
13
+ _, binary_mask = cv2.threshold(text_mask, 1, 255, cv2.THRESH_BINARY)
14
+
15
+ # Resize the mask to match the size of the original image area
16
+ mask_resized = cv2.resize(binary_mask, (original_image.shape[1], original_image.shape[0]))
17
+
18
+ # Expand the mask to cover more area if needed
19
+ kernel = np.ones((5, 5), np.uint8)
20
+ expanded_mask = cv2.dilate(mask_resized, kernel, iterations=1)
21
+
22
+ # Inpainting using the mask
23
+ inpainted_image = cv2.inpaint(original_image, expanded_mask, inpaintRadius=5, flags=cv2.INPAINT_TELEA)
24
+
25
+ # Optionally apply post-processing to improve results
26
+ cleaned_image = cv2.GaussianBlur(inpainted_image, (3, 3), 0)
27
+
28
+ return cleaned_image
29
+
30
+ def remove_watermark(image_path,saved_path):
31
+ # Load the original image
32
+ image = cv2.imread(image_path)
33
+
34
+ # Define the area of the watermark (adjust this based on the watermark size)
35
+ height, width, _ = image.shape
36
+ watermark_width = 185 # Adjust based on your watermark size
37
+ watermark_height = 185 # Adjust based on your watermark size
38
+ x_start = 50
39
+ y_start = height - watermark_height+17
40
+ x_end = watermark_width-17
41
+ y_end = height-50
42
+
43
+ # Extract the watermark area
44
+ watermark_area = image[y_start:y_end, x_start:x_end]
45
+ # cv2.imwrite('watermark_area.jpg', watermark_area)
46
+
47
+ # Create the mask for the watermark area
48
+ text_mask_path = 'watermark_mask.png'
49
+ cleaned_image = remove_watermark_area(watermark_area, text_mask_path)
50
+ # cv2.imwrite('cleaned_watermark.jpg', cleaned_image)
51
+ # Paste back the cleaned watermark on the original image
52
+ image[y_start:y_end, x_start:x_end] = cleaned_image
53
+ cv2.imwrite(saved_path, image)
54
+ return image
55
+
56
+ def make_zip(image_list):
57
+ zip_path = f"./temp/{uuid.uuid4().hex[:6]}.zip"
58
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
59
+ for image in image_list:
60
+ zipf.write(image, os.path.basename(image))
61
+ return zip_path
62
+
63
+ def process_files(image_files):
64
+ image_list = []
65
+ if len(image_files) == 1:
66
+ saved_path = os.path.basename(image_files[0])
67
+ saved_path = f"./temp/{saved_path}"
68
+ remove_watermark(image_files[0], saved_path)
69
+ return saved_path, saved_path
70
+ else:
71
+ for image_path in image_files:
72
+ saved_path = os.path.basename(image_path)
73
+ saved_path = f"./temp/{saved_path}"
74
+ remove_watermark(image_path, saved_path)
75
+ image_list.append(saved_path)
76
+ zip_path = make_zip(image_list)
77
+ return zip_path,None
78
+
79
+ if not os.path.exists("./temp"):
80
+ os.mkdir("./temp")
81
+
82
+ demo = gr.Interface(
83
+ process_files,
84
+ [gr.File(type='filepath', file_count='multiple')],
85
+ [gr.File(),gr.Image()],
86
+ cache_examples=True
87
+ )
88
+
89
+ demo.launch(debug=True)