Spaces:
Runtime error
Runtime error
up
Browse files- gradio_seg2image.py +93 -3
gradio_seg2image.py
CHANGED
|
@@ -6,6 +6,93 @@ from PIL import Image
|
|
| 6 |
#first elem of gallery is ^^ - {'name': '/tmp/tmpw60bbw6k.png', 'data': 'file=/tmp/tmpw60bbw6k.png', 'is_file': True}
|
| 7 |
#first elem of gallery is ^^ - {'name': '/tmp/tmpba0d5dt5.png', 'data': 'file=/tmp/tmpba0d5dt5.png', 'is_file': True}
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def dummyfun(result_gallery):
|
| 10 |
print(f"type of gallery is ^^ - {type(result_gallery)}")
|
| 11 |
print(f"length of gallery is ^^ - {len(result_gallery)}")
|
|
@@ -19,7 +106,7 @@ def dummyfun(result_gallery):
|
|
| 19 |
|
| 20 |
# Print the filename
|
| 21 |
#print(f"filename is ^^ - {filename}")
|
| 22 |
-
return result_gallery[
|
| 23 |
|
| 24 |
def create_demo(process, max_images=12):
|
| 25 |
with gr.Blocks() as demo:
|
|
@@ -73,12 +160,14 @@ def create_demo(process, max_images=12):
|
|
| 73 |
'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'
|
| 74 |
)
|
| 75 |
with gr.Column():
|
| 76 |
-
result_gallery = gr.Gallery(label='Output',
|
| 77 |
show_label=False,
|
| 78 |
elem_id='gallery').style(
|
| 79 |
grid=2, height='auto')
|
| 80 |
b1 = gr.Button('Get filenames')
|
| 81 |
filename = gr.Textbox(label="image file names")
|
|
|
|
|
|
|
| 82 |
ips = [
|
| 83 |
input_image, prompt, a_prompt, n_prompt, num_samples,
|
| 84 |
image_resolution, detect_resolution, ddim_steps, scale, seed, eta
|
|
@@ -88,5 +177,6 @@ def create_demo(process, max_images=12):
|
|
| 88 |
outputs=[result_gallery],
|
| 89 |
api_name='seg')
|
| 90 |
b1.click(dummyfun, [result_gallery], [filename])
|
|
|
|
| 91 |
|
| 92 |
-
return demo
|
|
|
|
| 6 |
#first elem of gallery is ^^ - {'name': '/tmp/tmpw60bbw6k.png', 'data': 'file=/tmp/tmpw60bbw6k.png', 'is_file': True}
|
| 7 |
#first elem of gallery is ^^ - {'name': '/tmp/tmpba0d5dt5.png', 'data': 'file=/tmp/tmpba0d5dt5.png', 'is_file': True}
|
| 8 |
|
| 9 |
+
import numpy as np
|
| 10 |
+
import base64
|
| 11 |
+
|
| 12 |
+
def encode(input_image):
|
| 13 |
+
# Convert NumPy array to bytes
|
| 14 |
+
img_bytes = np.ndarray.tobytes(img_array)
|
| 15 |
+
|
| 16 |
+
# Encode the bytes using Base64
|
| 17 |
+
encoded_string = base64.b64encode(img_bytes).decode('utf-8')
|
| 18 |
+
|
| 19 |
+
# Print and return the encoded string
|
| 20 |
+
#print(encoded_string)
|
| 21 |
+
return encoded_string
|
| 22 |
+
|
| 23 |
+
def create_imgcomp(input_image, filename):
|
| 24 |
+
encoded_string = encode(input_image)
|
| 25 |
+
htmltag = '<img src= "data:image/jpeg;base64,' + encoded_string + '"/></div> <img src= "https://ysharma-ControlNetSegmentation.hf.space/file=' + filename + '"/>'
|
| 26 |
+
#https://ysharma-controlnetsegmentation.hf.space/file=/tmp/tmpqcz9yeta.png
|
| 27 |
+
|
| 28 |
+
desc = """
|
| 29 |
+
<!DOCTYPE html>
|
| 30 |
+
<html lang="en">
|
| 31 |
+
<head>
|
| 32 |
+
<style>
|
| 33 |
+
body {
|
| 34 |
+
background: rgb(17, 17, 17);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
.image-slider {
|
| 38 |
+
margin-left: 3rem;
|
| 39 |
+
position: relative;
|
| 40 |
+
display: inline-block;
|
| 41 |
+
line-height: 0;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.image-slider img {
|
| 45 |
+
user-select: none;
|
| 46 |
+
max-width: 400px;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
.image-slider > div {
|
| 50 |
+
position: absolute;
|
| 51 |
+
width: 25px;
|
| 52 |
+
max-width: 100%;
|
| 53 |
+
overflow: hidden;
|
| 54 |
+
resize: horizontal;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
.image-slider > div:before {
|
| 58 |
+
content: '';
|
| 59 |
+
display: block;
|
| 60 |
+
width: 13px;
|
| 61 |
+
height: 13px;
|
| 62 |
+
overflow: hidden;
|
| 63 |
+
position: absolute;
|
| 64 |
+
resize: horizontal;
|
| 65 |
+
right: 3px;
|
| 66 |
+
bottom: 3px;
|
| 67 |
+
background-clip: content-box;
|
| 68 |
+
background: linear-gradient(-45deg, black 50%, transparent 0);
|
| 69 |
+
-webkit-filter: drop-shadow(0 0 2px black);
|
| 70 |
+
filter: drop-shadow(0 0 2px black);
|
| 71 |
+
}
|
| 72 |
+
</style>
|
| 73 |
+
</head>
|
| 74 |
+
<body>
|
| 75 |
+
<div style="margin: 3rem;
|
| 76 |
+
font-family: Roboto, sans-serif">
|
| 77 |
+
<h1 style="color: green">
|
| 78 |
+
GeeksforGeeks
|
| 79 |
+
</h1>
|
| 80 |
+
<h3 style="color: aliceblue">
|
| 81 |
+
CSS Image Comparison Slider
|
| 82 |
+
</h3>
|
| 83 |
+
</div>
|
| 84 |
+
<div>
|
| 85 |
+
<div class="image-slider">
|
| 86 |
+
<div> """ + htmltag +
|
| 87 |
+
"""</div>
|
| 88 |
+
</div>
|
| 89 |
+
</body>
|
| 90 |
+
</html>
|
| 91 |
+
"""
|
| 92 |
+
return desc
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
def dummyfun(result_gallery):
|
| 97 |
print(f"type of gallery is ^^ - {type(result_gallery)}")
|
| 98 |
print(f"length of gallery is ^^ - {len(result_gallery)}")
|
|
|
|
| 106 |
|
| 107 |
# Print the filename
|
| 108 |
#print(f"filename is ^^ - {filename}")
|
| 109 |
+
return result_gallery[1]['name'] #+ ',' + result_gallery[1]['name'] #filename
|
| 110 |
|
| 111 |
def create_demo(process, max_images=12):
|
| 112 |
with gr.Blocks() as demo:
|
|
|
|
| 160 |
'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'
|
| 161 |
)
|
| 162 |
with gr.Column():
|
| 163 |
+
result_gallery = gr.Gallery(label='Output', visible= False,
|
| 164 |
show_label=False,
|
| 165 |
elem_id='gallery').style(
|
| 166 |
grid=2, height='auto')
|
| 167 |
b1 = gr.Button('Get filenames')
|
| 168 |
filename = gr.Textbox(label="image file names")
|
| 169 |
+
b2 = gr.Button('Show Image-Comparison')
|
| 170 |
+
imagecomp = gr.HTML(value = desc)
|
| 171 |
ips = [
|
| 172 |
input_image, prompt, a_prompt, n_prompt, num_samples,
|
| 173 |
image_resolution, detect_resolution, ddim_steps, scale, seed, eta
|
|
|
|
| 177 |
outputs=[result_gallery],
|
| 178 |
api_name='seg')
|
| 179 |
b1.click(dummyfun, [result_gallery], [filename])
|
| 180 |
+
b2.click(create_imgcomp, [input_image, filename], [imagecomp])
|
| 181 |
|
| 182 |
+
return demo
|