NeuralFalcon commited on
Commit
929da13
·
verified ·
1 Parent(s): 7c9488e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -22
app.py CHANGED
@@ -28,23 +28,18 @@ def remove_watermark_area(original_image, text_mask_path):
28
  return cleaned_image
29
  from PIL import Image
30
 
31
- def remove_watermark(image_path,saved_path):
32
- if isinstance(image_path, str) and os.path.isfile(image_path):
 
 
 
33
  # Load the image using OpenCV
34
  image = cv2.imread(image_path)
35
- elif isinstance(image_path, np.ndarray):
36
- # Directly use OpenCV image (NumPy array)
37
- image = image_path
38
- if len(image_path.shape) == 3 and image_path.shape[2] == 3:
39
- # Assuming it's in RGB format; convert to BGR
40
- image = cv2.cvtColor(image_path, cv2.COLOR_RGB2BGR)
41
- else:
42
- # Otherwise, assume it's already in BGR format
43
- image = image_path
44
- else:
45
- raise TypeError("Invalid image_path format")
46
- print(type(image))
47
- cv2.imwrite("test.jpg",image)
48
  image=cv2.resize(image,(1280,1280))
49
  # Define the area of the watermark (adjust this based on the watermark size)
50
  height, width, _ = image.shape
@@ -65,7 +60,10 @@ def remove_watermark(image_path,saved_path):
65
  # cv2.imwrite('cleaned_watermark.jpg', cleaned_image)
66
  # Paste back the cleaned watermark on the original image
67
  image[y_start:y_end, x_start:x_end] = cleaned_image
68
- cv2.imwrite(saved_path, image)
 
 
 
69
  return image
70
 
71
  def make_zip(image_list):
@@ -82,7 +80,7 @@ def random_image_name():
82
 
83
  def process_file(pil_image):
84
  saved_path = f"./temp/{random_image_name()}.jpg"
85
- remove_watermark(pil_image, saved_path)
86
  return saved_path, saved_path
87
 
88
 
@@ -92,19 +90,59 @@ def process_files(image_files):
92
  # saved_path = os.path.basename(image_files[0])
93
  # saved_path = f"./temp/{saved_path}"
94
  saved_path = f"./temp/{random_image_name()}.jpg"
95
- remove_watermark(image_files[0], saved_path)
96
  return saved_path, saved_path
97
  else:
98
  for image_path in image_files:
99
  # saved_path = os.path.basename(image_path)
100
  # saved_path = f"./temp/{saved_path}"
101
  saved_path = f"./temp/{random_image_name()}.jpg"
102
- remove_watermark(image_path, saved_path)
103
  image_list.append(saved_path)
104
  zip_path = make_zip(image_list)
105
  return zip_path,None
106
 
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  if not os.path.exists("./temp"):
109
  os.mkdir("./temp")
110
 
@@ -114,7 +152,7 @@ meta_examples = ["./images/1.jpg", "./images/2.jpg", "./images/3.jpg", "./images
114
  gradio_input=[gr.Image(label='Upload an Image')]
115
  gradio_Output=[gr.File(label='Download Image'),gr.Image(label='Display Image')]
116
  gradio_interface = gr.Interface(fn=process_file, inputs=gradio_input,outputs=gradio_Output ,
117
- title="Meta Watermark Remover",
118
  examples=meta_examples)
119
  # gradio_interface.launch(debug=True)
120
 
@@ -127,6 +165,15 @@ gradio_multiple_images = gr.Interface(
127
  cache_examples=True
128
  )
129
 
130
- demo = gr.TabbedInterface([gradio_interface, gradio_multiple_images], ["Meta Watermark Remover","Bluk Meta Watermark Remover"],title="Meta Watermark Remover")
131
- demo.launch(debug=True)
132
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  return cleaned_image
29
  from PIL import Image
30
 
31
+ def remove_watermark(image_path,file_type="",saved_path=""):
32
+ # file_type="pil"
33
+ # file_type="opencv"
34
+ # file_type="filepath"
35
+ if file_type=="filepath":
36
  # Load the image using OpenCV
37
  image = cv2.imread(image_path)
38
+ if file_type=="pil":
39
+ image = cv2.cvtColor(image_path, cv2.COLOR_RGB2BGR)
40
+ if file_type=="opencv":
41
+ image=image_path
42
+ # cv2.imwrite("test.jpg",image)
 
 
 
 
 
 
 
 
43
  image=cv2.resize(image,(1280,1280))
44
  # Define the area of the watermark (adjust this based on the watermark size)
45
  height, width, _ = image.shape
 
60
  # cv2.imwrite('cleaned_watermark.jpg', cleaned_image)
61
  # Paste back the cleaned watermark on the original image
62
  image[y_start:y_end, x_start:x_end] = cleaned_image
63
+ if saved_path=="":
64
+ pass
65
+ else:
66
+ cv2.imwrite(saved_path, image)
67
  return image
68
 
69
  def make_zip(image_list):
 
80
 
81
  def process_file(pil_image):
82
  saved_path = f"./temp/{random_image_name()}.jpg"
83
+ remove_watermark(pil_image,"pil",saved_path)
84
  return saved_path, saved_path
85
 
86
 
 
90
  # saved_path = os.path.basename(image_files[0])
91
  # saved_path = f"./temp/{saved_path}"
92
  saved_path = f"./temp/{random_image_name()}.jpg"
93
+ remove_watermark(image_files[0],"filepath", saved_path)
94
  return saved_path, saved_path
95
  else:
96
  for image_path in image_files:
97
  # saved_path = os.path.basename(image_path)
98
  # saved_path = f"./temp/{saved_path}"
99
  saved_path = f"./temp/{random_image_name()}.jpg"
100
+ remove_watermark(image_path,"filepath",saved_path)
101
  image_list.append(saved_path)
102
  zip_path = make_zip(image_list)
103
  return zip_path,None
104
 
105
 
106
+ import cv2
107
+ import numpy as np
108
+
109
+
110
+
111
+ def process_video(input_video_path):
112
+ print(input_video_path)
113
+ # output_video_path=""
114
+ # if output_video_path=="":
115
+ output_video_path=f"./temp/{random_image_name()}.mp4"
116
+ cap = cv2.VideoCapture(input_video_path)
117
+
118
+ if not cap.isOpened():
119
+ raise ValueError(f"Unable to open video file {input_video_path}")
120
+
121
+ fps = cap.get(cv2.CAP_PROP_FPS)
122
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
123
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
124
+
125
+ # Create VideoWriter object for output video
126
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You might try 'XVID' or 'H264' if issues persist
127
+ video_writer = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
128
+
129
+ # Process frames
130
+ while cap.isOpened():
131
+ ret, frame = cap.read()
132
+ if not ret:
133
+ break
134
+ no_watermark_frame=remove_watermark(frame,"opencv")
135
+
136
+ # Ensure the frame has the same size and type
137
+ if no_watermark_frame.shape[1] != width or no_watermark_frame.shape[0] != height:
138
+ no_watermark_frame = cv2.resize(no_watermark_frame, (width, height))
139
+
140
+ video_writer.write(no_watermark_frame)
141
+
142
+ cap.release()
143
+ video_writer.release()
144
+ return output_video_path,output_video_path
145
+
146
  if not os.path.exists("./temp"):
147
  os.mkdir("./temp")
148
 
 
152
  gradio_input=[gr.Image(label='Upload an Image')]
153
  gradio_Output=[gr.File(label='Download Image'),gr.Image(label='Display Image')]
154
  gradio_interface = gr.Interface(fn=process_file, inputs=gradio_input,outputs=gradio_Output ,
155
+ title="Meta Watermark Remover From Image",
156
  examples=meta_examples)
157
  # gradio_interface.launch(debug=True)
158
 
 
165
  cache_examples=True
166
  )
167
 
 
 
168
 
169
+ meta_video_examples = [ "./videos/1.mp4"]
170
+
171
+ gradio_video_input=[gr.Video(label='Upload an Video')]
172
+ gradio_video_Output=[gr.File(label='Download Video'),gr.Video(label='Display Video')]
173
+ gradio_video_interface = gr.Interface(fn=process_video, inputs=gradio_video_input,outputs=gradio_video_Output ,
174
+ title="Meta Watermark Remover from Video",
175
+ examples=meta_video_examples)
176
+
177
+
178
+ demo = gr.TabbedInterface([gradio_interface, gradio_video_interface,gradio_multiple_images], ["Meta Watermark Remover","Meta Watermark Remover from Video","Bluk Meta Watermark Remover"],title="Meta Watermark Remover")
179
+ demo.launch(debug=True)