Akjava commited on
Commit
f24d19f
·
1 Parent(s): 4b746a9
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import subprocess
4
+ import re
5
+ from PIL import Image
6
+
7
+ import opencvinpaint
8
+
9
+ def sanitize_prompt(prompt):
10
+ # Allow only alphanumeric characters, spaces, and basic punctuation
11
+ allowed_chars = re.compile(r"[^a-zA-Z0-9\s.,!?-]")
12
+ sanitized_prompt = allowed_chars.sub("", prompt)
13
+ return sanitized_prompt
14
+
15
+ #@spaces.GPU(duration=120)
16
+ def process_images(image1, image2,inpaint_radius,blur_radius,edge_expand):
17
+ if image1!=None:
18
+ pass
19
+ #image1.save("input.jpg","JPEG")
20
+ if image2!= None:
21
+
22
+ pass
23
+ #image2.save("mask.jpg","JPEG")
24
+ if image1!=None and image2!=None:
25
+
26
+ output = opencvinpaint.process_image(image1,image2,inpaint_radius,blur_radius,edge_expand)
27
+
28
+ return output
29
+
30
+ return None
31
+
32
+ # code from https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/blob/main/app.py
33
+ def read_file(file_path: str) -> str:
34
+ """read the text of target file
35
+ """
36
+ with open(file_path, 'r', encoding='utf-8') as f:
37
+ content = f.read()
38
+
39
+ return content
40
+
41
+ #css=css,
42
+ demo_blocks = gr.Blocks( elem_id="demo-container")
43
+ with demo_blocks as demo:
44
+ gr.HTML(read_file("demo_header.html"))
45
+ with gr.Row():
46
+ with gr.Column():
47
+ image = gr.Image(sources=['upload','clipboard'], elem_id="image_upload", type="pil", label="Upload",height=400)
48
+ with gr.Row(elem_id="prompt-container", equal_height=True):
49
+ with gr.Row():
50
+ btn = gr.Button("Inpaint!", elem_id="run_button")
51
+ image_mask = gr.Image(sources=['upload','clipboard'], elem_id="mask_upload", type="pil", label="Mask_Upload",height=400)
52
+ with gr.Accordion(label="Advanced Settings", open=False):
53
+ with gr.Row( equal_height=True):
54
+ inpaint_radius = gr.Number(value=3, minimum=1.0, maximum=20.0, step=1, label="Inpaint Radius")
55
+ blur_radius = gr.Number(value=25, minimum=0.0, maximum=50.0, step=1, label="Blur Radius")
56
+ edge_expand = gr.Number(value=8, minimum=0.0, maximum=20.0, step=1, label="Edge Expand")
57
+
58
+ with gr.Column():
59
+ image_out = gr.Image(sources=[],label="Output", elem_id="output-img", height=400)
60
+ with gr.Group(elem_id="share-btn-container", visible=False) as share_btn_container:
61
+ pass
62
+
63
+
64
+ btn.click(fn=process_images, inputs=[image, image_mask,inpaint_radius,blur_radius,edge_expand], outputs =image_out, api_name='run')
65
+ gr.Examples(
66
+ examples=[["examples/street.jpg", "examples/street_mask.jpg"]]
67
+ ,
68
+ #fn=predict,
69
+ inputs=[image,image_mask],
70
+ cache_examples=False,
71
+ )
72
+ gr.HTML(
73
+ """
74
+ <div style="text-align: center;">
75
+ <p>Inpaint Code <a href="https://github.com/opencv/opencv/blob/da3debda6d233af90e421e95700c63fc08b83b75/samples/python/inpaint.py" style="text-decoration: underline;" target="_blank">OpenCV inpaint example</a> - Gradio Demo by 🤗 Hugging Face
76
+ </p>
77
+ </div>
78
+ """
79
+ )
80
+
81
+ demo_blocks.queue(max_size=25).launch(share=True)
demo_header.html ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <div style="text-align: center;">
2
+ <h1>
3
+ OpenCV Inpaint(Classic Style Inapint) CPU
4
+ </h1>
5
+ <p>This is old style inpaint.but background image effects new-inapint app.for smooth fill</p>
6
+ </div>
examples/street.jpg ADDED
examples/street_mask.jpg ADDED
opencvinpaint.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ import cv2
5
+ import numpy as np
6
+ from PIL import Image
7
+
8
+
9
+ debug = False
10
+
11
+ def gray3d_to_2d(grayscale: np.ndarray) -> np.ndarray:
12
+ channel = grayscale.shape[2] if grayscale.ndim == 3 else 1
13
+ if channel!=1:
14
+ text = f"grayscale shape = {grayscale.shape} channel = {channel} ndim = {grayscale.ndim} size = {grayscale.size}"
15
+ raise ValueError(f"color maybe rgb or rgba {text}")
16
+
17
+ if grayscale.ndim == 2:
18
+ return grayscale
19
+ return np.squeeze(grayscale)
20
+
21
+ def pil_to_cv(image):
22
+ cv_image = np.array(image, dtype=np.uint8)
23
+ if cv_image.shape[2] == 3: # カラー
24
+ cv_image = cv2.cvtColor(cv_image, cv2.COLOR_RGB2BGR)
25
+ elif cv_image.shape[2] == 4: #
26
+ cv_image = cv2.cvtColor(cv_image, cv2.COLOR_RGBA2BGR)
27
+ return cv_image
28
+
29
+ def blend_rgb_images(image1: np.ndarray, image2: np.ndarray, mask: np.ndarray) -> np.ndarray:
30
+
31
+ if image1.shape != image2.shape or image1.shape[:2] != mask.shape:
32
+ raise ValueError("not same shape")
33
+
34
+ # 画像を float 型に変換
35
+ image1 = image1.astype(float)
36
+ image2 = image2.astype(float)
37
+
38
+ # mask to 3 chan 0 -1 value
39
+ alpha = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR).astype(float) / 255.0
40
+
41
+ # calcurate blend
42
+ blended = (1 - alpha) * image1 + alpha * image2
43
+
44
+ return blended.astype(np.uint8)
45
+
46
+ def process_image(image,mask_image,inpaint_radius,blur_radius,edge_expand):
47
+ #print(blur_radius,",",edge_expand)
48
+ cv_image = pil_to_cv(image)
49
+
50
+ cv_mask = pil_to_cv(mask_image)
51
+ cv_gray = cv2.cvtColor(cv_mask,cv2.COLOR_BGR2GRAY)
52
+
53
+ mask = gray3d_to_2d(cv_gray)
54
+ cv2.imwrite("_mask.jpg",mask)
55
+ cv2.imwrite("_image.jpg",cv_image)
56
+ img_inpainted = cv2.inpaint(cv_image, mask,inpaint_radius, cv2.INPAINT_TELEA)
57
+ if debug:
58
+ cv2.imwrite("close_eye_inpaint.jpg",img_inpainted)
59
+
60
+
61
+ ## blur
62
+ if blur_radius > 0:
63
+ if blur_radius%2==0:
64
+ blur_radius += 1
65
+ print(blur_radius)
66
+ blurred_image = cv2.GaussianBlur(img_inpainted, (blur_radius, blur_radius), 0) #should be odd
67
+ if debug:
68
+ cv2.imwrite("close_eye_inpaint_burred.jpg",blurred_image)
69
+ else:
70
+ blurred_image = img_inpainted
71
+
72
+ # expand edge and blur
73
+ kernel = np.ones((edge_expand, edge_expand), np.uint8)
74
+ extend_mask = cv2.dilate(mask, kernel, iterations=1)
75
+
76
+ if edge_expand > 0 and blur_radius > 0:
77
+ extend_burred_mask = cv2.GaussianBlur(extend_mask, (blur_radius, blur_radius), 0)
78
+ else:
79
+ extend_burred_mask = extend_mask
80
+
81
+
82
+ img_inpainted = blend_rgb_images(img_inpainted,blurred_image,extend_burred_mask)
83
+
84
+ output_image = img_inpainted.copy()
85
+
86
+ if output_image.shape[2] == 3: # カラー
87
+ output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
88
+
89
+ return Image.fromarray(output_image)
90
+
91
+ if __name__ == "__main__":
92
+ image = Image.open(sys.argv[1])
93
+ mask = Image.open(sys.argv[2])
94
+ output = process_image(image,mask)
95
+ output.save(sys.argv[3])
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy
2
+ torch
3
+ spaces
4
+ opencv