qfisch commited on
Commit
f94ad82
·
1 Parent(s): 0aaea9a

feat(front): add app.py

Browse files
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import PIL
3
+ import gradio as gr
4
+ import numpy as np
5
+ import os
6
+
7
+ def predict(input_img) -> tuple[np.ndarray | PIL.Image.Image | str, list[tuple[np.ndarray | tuple[int, int, int, int], str]]]:
8
+ res = model(input_img)
9
+ if len(res) == 0:
10
+ return input_img, "No watermark detected"
11
+ res = res[0]
12
+ # convert res.boxes.xyxy to a tuple of (x1, y1, x2, y2)
13
+ bbox = res.boxes.xyxy[0].tolist()
14
+ bbox = (int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]))
15
+ # convert res.boxes.cls to a string
16
+ label = res.boxes.cls[0]
17
+ str_label = "Watermark is a logo" if label == 0 else "Watermark is a text"
18
+ print(bbox, str_label)
19
+ return input_img, [(bbox, str_label)]
20
+
21
+
22
+ gradio_app = gr.Interface(
23
+ predict,
24
+ inputs=gr.Image(label="Update your watermaked image", sources=['upload'], type="pil"),
25
+ # output displays the image with the bounding boxes
26
+ outputs=gr.AnnotatedImage(),
27
+ title="Detect Watermark in Images",
28
+ examples=[
29
+ os.path.join(os.path.dirname(__file__), "samples/example_text1.jpg"),
30
+ os.path.join(os.path.dirname(__file__), "samples/example_text2.jpg"),
31
+ os.path.join(os.path.dirname(__file__), "samples/example_text3.jpg"),
32
+ os.path.join(os.path.dirname(__file__), "samples/example_logo1.jpg"),
33
+ os.path.join(os.path.dirname(__file__), "samples/example_logo2.jpg"),
34
+ os.path.join(os.path.dirname(__file__), "samples/example_logo3.jpg"),
35
+ ],
36
+ allow_flagging="never"
37
+ )
38
+
39
+
40
+ if __name__ == "__main__":
41
+ model = YOLO("best.pt")
42
+ gradio_app.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ ultralytics
samples/example_logo1.jpg ADDED
samples/example_logo2.jpg ADDED
samples/example_logo3.jpg ADDED
samples/example_text1.jpg ADDED
samples/example_text2.jpg ADDED
samples/example_text3.jpg ADDED