Spaces:
Runtime error
Runtime error
Commit
·
c8ae93f
0
Parent(s):
Duplicate from Zengyf-CVer/watermarking_lab
Browse filesCo-authored-by: ZengYifu <[email protected]>
- .gitattributes +31 -0
- .gitignore +64 -0
- README.md +14 -0
- app.py +112 -0
- img_examples/bus.jpg +0 -0
- img_examples/zidane.jpg +0 -0
- requirements.txt +3 -0
- style.css +7 -0
- util/fonts_opt.py +67 -0
.gitattributes
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
23 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Gradio YOLOv5 Det
|
2 |
+
# 创建人:曾逸夫
|
3 |
+
# 项目地址:https://gitee.com/CV_Lab/gradio_yolov5_det
|
4 |
+
|
5 |
+
# 图片格式
|
6 |
+
*.jpg
|
7 |
+
*.jpeg
|
8 |
+
*.png
|
9 |
+
*.svg
|
10 |
+
*.gif
|
11 |
+
|
12 |
+
# 视频格式
|
13 |
+
*.mp4
|
14 |
+
*.avi
|
15 |
+
.ipynb_checkpoints
|
16 |
+
/__pycache__
|
17 |
+
*/__pycache__
|
18 |
+
|
19 |
+
# 日志格式
|
20 |
+
*.log
|
21 |
+
*.data
|
22 |
+
*.txt
|
23 |
+
|
24 |
+
# 生成文件
|
25 |
+
*.pdf
|
26 |
+
*.xlsx
|
27 |
+
*.csv
|
28 |
+
|
29 |
+
# 参数文件
|
30 |
+
*.yaml
|
31 |
+
*.json
|
32 |
+
|
33 |
+
# 压缩文件格式
|
34 |
+
*.zip
|
35 |
+
*.tar
|
36 |
+
*.tar.gz
|
37 |
+
*.rar
|
38 |
+
|
39 |
+
# 字体格式
|
40 |
+
*.ttc
|
41 |
+
*.ttf
|
42 |
+
*.otf
|
43 |
+
*.pkl
|
44 |
+
|
45 |
+
# 模型文件
|
46 |
+
*.pt
|
47 |
+
*.db
|
48 |
+
|
49 |
+
/flagged
|
50 |
+
/run
|
51 |
+
!requirements.txt
|
52 |
+
!cls_name/*
|
53 |
+
!model_config/*
|
54 |
+
!img_examples/*
|
55 |
+
!video_examples/*
|
56 |
+
|
57 |
+
!requirements.txt
|
58 |
+
!.pre-commit-config.yaml
|
59 |
+
|
60 |
+
app copy.py
|
61 |
+
test.py
|
62 |
+
test*.py
|
63 |
+
|
64 |
+
model_download.py
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Watermarking Lab
|
3 |
+
emoji: 🚀
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.1.4
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: gpl-3.0
|
11 |
+
duplicated_from: Zengyf-CVer/watermarking_lab
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Watermarking Lab
|
2 |
+
# 创建人:曾逸夫
|
3 |
+
# 创建时间:2022-08-10
|
4 |
+
# 功能描述:单一图片加水印 透明度 字体颜色
|
5 |
+
|
6 |
+
import math
|
7 |
+
import sys
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
from PIL import Image, ImageDraw, ImageFont
|
11 |
+
|
12 |
+
from util.fonts_opt import is_fonts
|
13 |
+
|
14 |
+
ROOT_PATH = sys.path[0] # 根目录
|
15 |
+
DESCRIPTION = '''# Watermarking Lab'''
|
16 |
+
|
17 |
+
|
18 |
+
def Hex_to_RGB(hex):
|
19 |
+
r = int(hex[1:3], 16)
|
20 |
+
g = int(hex[3:5], 16)
|
21 |
+
b = int(hex[5:7], 16)
|
22 |
+
|
23 |
+
return r, g, b
|
24 |
+
|
25 |
+
|
26 |
+
def watermarking(img, text, text_size, text_font, wm_location, wm_trans, wm_textColor):
|
27 |
+
# 参考:https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html?highlight=text#example-draw-partial-opacity-text
|
28 |
+
|
29 |
+
img_base = img.convert("RGBA")
|
30 |
+
txt = Image.new('RGBA', img_base.size, (255, 255, 255, 0))
|
31 |
+
|
32 |
+
text_size = int(text_size)
|
33 |
+
draw = ImageDraw.Draw(txt)
|
34 |
+
|
35 |
+
font = ImageFont.truetype(f"./fonts/{text_font}.ttf", text_size)
|
36 |
+
textwidth, textheight = draw.textsize(text, font)
|
37 |
+
|
38 |
+
width, height = img_base.size # 图片尺寸
|
39 |
+
|
40 |
+
# ---------- 水印位置 ----------
|
41 |
+
if wm_location == "center":
|
42 |
+
x = width / 2 - textwidth / 2
|
43 |
+
y = height / 2 - textheight / 2
|
44 |
+
elif wm_location == "bottom right":
|
45 |
+
x = width - textwidth - text_size
|
46 |
+
y = height - textheight - text_size
|
47 |
+
elif wm_location == "bottom left":
|
48 |
+
x = text_size
|
49 |
+
y = height - textheight - text_size
|
50 |
+
elif wm_location == "top right":
|
51 |
+
x = width - textwidth - text_size
|
52 |
+
y = text_size
|
53 |
+
elif wm_location == "top left":
|
54 |
+
x = text_size
|
55 |
+
y = text_size
|
56 |
+
|
57 |
+
r, g, b = Hex_to_RGB(wm_textColor)
|
58 |
+
draw.text((x, y), text, fill=(r, g, b, math.ceil(float(wm_trans) * 255)), font=font)
|
59 |
+
img_combined = Image.alpha_composite(img_base, txt)
|
60 |
+
|
61 |
+
return img_combined
|
62 |
+
|
63 |
+
|
64 |
+
def main():
|
65 |
+
is_fonts(f"{ROOT_PATH}/fonts") # 检查字体文件
|
66 |
+
|
67 |
+
with gr.Blocks(css='style.css') as gyd:
|
68 |
+
gr.Markdown(DESCRIPTION)
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column():
|
71 |
+
with gr.Row():
|
72 |
+
input_img = gr.Image(image_mode="RGB", source="upload", type="pil", label="原始图片")
|
73 |
+
with gr.Row():
|
74 |
+
wm_location = gr.Radio(choices=["center", "bottom right", "bottom left", "top right", "top left"],
|
75 |
+
value="center",
|
76 |
+
label="位置")
|
77 |
+
with gr.Row():
|
78 |
+
wm_text = gr.Textbox(value="水印内容", label="水印内容")
|
79 |
+
with gr.Row():
|
80 |
+
wm_textFont = gr.Dropdown(choices=["SimSun", "TimesNewRoman", "malgun"], value="SimSun", label="字体")
|
81 |
+
with gr.Row():
|
82 |
+
wm_textSize = gr.Number(value=50, label="文字大小")
|
83 |
+
with gr.Row():
|
84 |
+
wm_textTrans = gr.Slider(minimum=0, maximum=1, value=1, step=0.1, label="水印透明度")
|
85 |
+
with gr.Row():
|
86 |
+
wm_textColor = gr.ColorPicker(label="水印颜色")
|
87 |
+
with gr.Row():
|
88 |
+
btn_01 = gr.Button(value='加水印', variant="primary")
|
89 |
+
|
90 |
+
with gr.Column():
|
91 |
+
with gr.Row():
|
92 |
+
output_img = gr.Image(type="pil", label="水印图片")
|
93 |
+
|
94 |
+
with gr.Row():
|
95 |
+
example_list = [[
|
96 |
+
"./img_examples/bus.jpg", "Watermarking Text", 50, "TimesNewRoman", "bottom right", "0.9", "#FFFFFF"],
|
97 |
+
["./img_examples/zidane.jpg", "水印文字", 50, "SimSun", "center", "0.5", "#2488C6"]]
|
98 |
+
gr.Examples(example_list,
|
99 |
+
[input_img, wm_text, wm_textSize, wm_textFont, wm_location, wm_textTrans, wm_textColor],
|
100 |
+
output_img,
|
101 |
+
watermarking,
|
102 |
+
cache_examples=False)
|
103 |
+
|
104 |
+
btn_01.click(fn=watermarking,
|
105 |
+
inputs=[input_img, wm_text, wm_textSize, wm_textFont, wm_location, wm_textTrans, wm_textColor],
|
106 |
+
outputs=[output_img])
|
107 |
+
|
108 |
+
gyd.launch(inbrowser=True)
|
109 |
+
|
110 |
+
|
111 |
+
if __name__ == '__main__':
|
112 |
+
main()
|
img_examples/bus.jpg
ADDED
![]() |
img_examples/zidane.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pillow
|
2 |
+
wget
|
3 |
+
rich
|
style.css
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
|
5 |
+
#content_align {
|
6 |
+
text-align: center;
|
7 |
+
}
|
util/fonts_opt.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 字体管理
|
2 |
+
# 创建人:曾逸夫
|
3 |
+
# 创建时间:2022-05-01
|
4 |
+
|
5 |
+
import os
|
6 |
+
import sys
|
7 |
+
from pathlib import Path
|
8 |
+
|
9 |
+
import wget
|
10 |
+
from rich.console import Console
|
11 |
+
|
12 |
+
ROOT_PATH = sys.path[0] # 项目根目录
|
13 |
+
|
14 |
+
# 中文、英文、俄语、西班牙语、阿拉伯语、韩语
|
15 |
+
fonts_list = ["SimSun.ttf", "TimesNewRoman.ttf", "malgun.ttf"] # 字体列表
|
16 |
+
fonts_suffix = ["ttc", "ttf", "otf"] # 字体后缀
|
17 |
+
|
18 |
+
data_url_dict = {
|
19 |
+
"SimSun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053539/download/SimSun.ttf",
|
20 |
+
"TimesNewRoman.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053537/download/TimesNewRoman.ttf",
|
21 |
+
"malgun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053538/download/malgun.ttf",}
|
22 |
+
|
23 |
+
console = Console()
|
24 |
+
|
25 |
+
|
26 |
+
# 创建字体库
|
27 |
+
def add_fronts(font_diff):
|
28 |
+
|
29 |
+
global font_name
|
30 |
+
|
31 |
+
for k, v in data_url_dict.items():
|
32 |
+
if k in font_diff:
|
33 |
+
font_name = v.split("/")[-1] # 字体名称
|
34 |
+
Path(f"{ROOT_PATH}/fonts").mkdir(parents=True, exist_ok=True) # 创建目录
|
35 |
+
|
36 |
+
file_path = f"{ROOT_PATH}/fonts/{font_name}" # 字体路径
|
37 |
+
|
38 |
+
try:
|
39 |
+
# 下载字体文件
|
40 |
+
wget.download(v, file_path)
|
41 |
+
except Exception as e:
|
42 |
+
print("路径错误!程序结束!")
|
43 |
+
print(e)
|
44 |
+
sys.exit()
|
45 |
+
else:
|
46 |
+
print()
|
47 |
+
console.print(f"{font_name} [bold green]字体文件下载完成![/bold green] 已保存至:{file_path}")
|
48 |
+
|
49 |
+
|
50 |
+
# 判断字体文件
|
51 |
+
def is_fonts(fonts_dir):
|
52 |
+
if os.path.isdir(fonts_dir):
|
53 |
+
# 如果字体库存在
|
54 |
+
f_list = os.listdir(fonts_dir) # 本地字体库
|
55 |
+
|
56 |
+
font_diff = list(set(fonts_list).difference(set(f_list)))
|
57 |
+
|
58 |
+
if font_diff != []:
|
59 |
+
# 字体不存在
|
60 |
+
console.print("[bold red]字体不存在,正在加载。。。[/bold red]")
|
61 |
+
add_fronts(font_diff) # 创建字体库
|
62 |
+
else:
|
63 |
+
console.print(f"{fonts_list}[bold green]字体已存在![/bold green]")
|
64 |
+
else:
|
65 |
+
# 字体库不存在,创建字体库
|
66 |
+
console.print("[bold red]字体库不存在,正在创建。。。[/bold red]")
|
67 |
+
add_fronts(fonts_list) # 创建字体库
|