Spaces:
Sleeping
Sleeping
MonkeyJuice
commited on
Commit
•
166cd36
1
Parent(s):
611a640
Create cropImage.py
Browse files- app.py +10 -3
- cropImage.py +31 -0
app.py
CHANGED
@@ -6,10 +6,11 @@ import gradio as gr
|
|
6 |
import PIL.Image
|
7 |
import zipfile
|
8 |
from genTag import genTag
|
|
|
9 |
from checkIgnore import is_ignore
|
10 |
from createTagDom import create_tag_dom
|
11 |
|
12 |
-
def predict(image: PIL.Image.Image, score_threshold: float):
|
13 |
result_threshold = genTag(image, score_threshold)
|
14 |
result_html = ''
|
15 |
for label, prob in result_threshold.items():
|
@@ -17,7 +18,8 @@ def predict(image: PIL.Image.Image, score_threshold: float):
|
|
17 |
result_html = '<div>' + result_html + '</div>'
|
18 |
result_filter = {key: value for key, value in result_threshold.items() if not is_ignore(key, 1)}
|
19 |
result_text = '<div id="m5dd_result">' + ', '.join(result_filter.keys()) + '</div>'
|
20 |
-
|
|
|
21 |
|
22 |
def predict_batch(zip_file, score_threshold: float, progress=gr.Progress()):
|
23 |
result = ''
|
@@ -49,6 +51,11 @@ with gr.Blocks(css="style.css", js="script.js") as demo:
|
|
49 |
value=0.3)
|
50 |
run_button = gr.Button('Run')
|
51 |
result_text = gr.HTML(value="")
|
|
|
|
|
|
|
|
|
|
|
52 |
with gr.Column(scale=2):
|
53 |
result_html = gr.HTML(value="")
|
54 |
with gr.Tab(label='Batch'):
|
@@ -72,7 +79,7 @@ with gr.Blocks(css="style.css", js="script.js") as demo:
|
|
72 |
run_button.click(
|
73 |
fn=predict,
|
74 |
inputs=[image, score_threshold],
|
75 |
-
outputs=[result_html, result_text],
|
76 |
api_name='predict',
|
77 |
)
|
78 |
run_button2.click(
|
|
|
6 |
import PIL.Image
|
7 |
import zipfile
|
8 |
from genTag import genTag
|
9 |
+
from cropImage import cropImage
|
10 |
from checkIgnore import is_ignore
|
11 |
from createTagDom import create_tag_dom
|
12 |
|
13 |
+
def predict(image: PIL.Image.Image, score_threshold: float):
|
14 |
result_threshold = genTag(image, score_threshold)
|
15 |
result_html = ''
|
16 |
for label, prob in result_threshold.items():
|
|
|
18 |
result_html = '<div>' + result_html + '</div>'
|
19 |
result_filter = {key: value for key, value in result_threshold.items() if not is_ignore(key, 1)}
|
20 |
result_text = '<div id="m5dd_result">' + ', '.join(result_filter.keys()) + '</div>'
|
21 |
+
crop_image = cropImage(image)
|
22 |
+
return result_html, result_text, crop_image
|
23 |
|
24 |
def predict_batch(zip_file, score_threshold: float, progress=gr.Progress()):
|
25 |
result = ''
|
|
|
51 |
value=0.3)
|
52 |
run_button = gr.Button('Run')
|
53 |
result_text = gr.HTML(value="")
|
54 |
+
with gr.Accordion("Crop Image"):
|
55 |
+
crop_image = gr.Image(elem_classes='m5dd_image',
|
56 |
+
format='jpg',
|
57 |
+
show_label=False,
|
58 |
+
container=False)
|
59 |
with gr.Column(scale=2):
|
60 |
result_html = gr.HTML(value="")
|
61 |
with gr.Tab(label='Batch'):
|
|
|
79 |
run_button.click(
|
80 |
fn=predict,
|
81 |
inputs=[image, score_threshold],
|
82 |
+
outputs=[result_html, result_text, crop_image],
|
83 |
api_name='predict',
|
84 |
)
|
85 |
run_button2.click(
|
cropImage.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import PIL.Image
|
6 |
+
|
7 |
+
def cropImage(image: PIL.Image.Image):
|
8 |
+
original_width, original_height = image.size
|
9 |
+
scale = max(original_width, original_height) / min(original_width, original_height)
|
10 |
+
|
11 |
+
target_width = 512
|
12 |
+
target_height = 768
|
13 |
+
|
14 |
+
if scale < 1.1:
|
15 |
+
target_width = 640
|
16 |
+
target_height = 640
|
17 |
+
elif original_width > original_height:
|
18 |
+
target_width = 768
|
19 |
+
target_height = 512
|
20 |
+
|
21 |
+
if original_width / original_height > target_width / target_height:
|
22 |
+
new_width = int(original_height * (target_width / target_height))
|
23 |
+
crop_box = ((original_width - new_width) // 2, 0, (original_width + new_width) // 2, original_height)
|
24 |
+
else:
|
25 |
+
new_height = int(original_width * (target_height / target_width))
|
26 |
+
crop_box = (0, (original_height - new_height) // 2, original_width, (original_height + new_height) // 2)
|
27 |
+
cropped_image = image.crop(crop_box)
|
28 |
+
|
29 |
+
cropped_image = cropped_image.resize((target_width, target_height))
|
30 |
+
|
31 |
+
return cropped_image
|