Update apps/gradio_app.py
Browse files- apps/gradio_app.py +103 -67
apps/gradio_app.py
CHANGED
|
@@ -1,68 +1,104 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
from gradio_app.config import setup_logging, setup_sys_path
|
| 4 |
-
from gradio_app.processor import gradio_process, update_preview, update_visibility
|
| 5 |
-
|
| 6 |
-
# Initialize logging and sys.path
|
| 7 |
-
setup_logging()
|
| 8 |
-
setup_sys_path()
|
| 9 |
-
|
| 10 |
-
# Load custom CSS
|
| 11 |
-
custom_css = open(os.path.join(os.path.dirname(__file__), "gradio_app", "static", "styles.css"), "r").read()
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
""",
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
iface.launch(share=True)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from gradio_app.config import setup_logging, setup_sys_path
|
| 4 |
+
from gradio_app.processor import gradio_process, update_preview, update_visibility
|
| 5 |
+
|
| 6 |
+
# Initialize logging and sys.path
|
| 7 |
+
setup_logging()
|
| 8 |
+
setup_sys_path()
|
| 9 |
+
|
| 10 |
+
# Load custom CSS
|
| 11 |
+
custom_css = open(os.path.join(os.path.dirname(__file__), "gradio_app", "static", "styles.css"), "r").read()
|
| 12 |
+
|
| 13 |
+
# Path to examples directory
|
| 14 |
+
examples_dir = os.path.join(os.path.dirname(__file__), "gradio_app", "assets", "examples", "license_plate_detector_ocr")
|
| 15 |
+
|
| 16 |
+
# Collect example files
|
| 17 |
+
examples = [
|
| 18 |
+
{
|
| 19 |
+
"Input File": os.path.join(examples_dir, "1", "lp_image.jpg"),
|
| 20 |
+
"Output File": os.path.join(examples_dir, "1", "lp_image_output.jpg"),
|
| 21 |
+
"Input Type": "Image"
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"Input File": os.path.join(examples_dir, "2", "lp_video.mp4"),
|
| 25 |
+
"Output File": os.path.join(examples_dir, "2", "lp_video_output.mp4"),
|
| 26 |
+
"Input Type": "Video"
|
| 27 |
+
}
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
# Function to handle example selection
|
| 31 |
+
def select_example(input_file, input_type):
|
| 32 |
+
return input_file, input_type, None, None, None # Reset outputs
|
| 33 |
+
|
| 34 |
+
# Gradio Interface
|
| 35 |
+
with gr.Blocks(css=custom_css) as iface:
|
| 36 |
+
gr.Markdown(
|
| 37 |
+
"""
|
| 38 |
+
# License Plate Detection and OCR
|
| 39 |
+
Detect license plates from images or videos and read their text using
|
| 40 |
+
advanced computer vision and OCR for accurate identification.
|
| 41 |
+
""",
|
| 42 |
+
elem_classes="markdown-title"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column(scale=1):
|
| 47 |
+
input_file = gr.File(label="Upload Image or Video", elem_classes="custom-file-input")
|
| 48 |
+
input_type = gr.Radio(choices=["Image", "Video"], label="Input Type", value="Image", elem_classes="custom-radio")
|
| 49 |
+
with gr.Blocks():
|
| 50 |
+
input_preview_image = gr.Image(label="Input Preview", visible=True, elem_classes="custom-image")
|
| 51 |
+
input_preview_video = gr.Video(label="Input Preview", visible=False, elem_classes="custom-video")
|
| 52 |
+
with gr.Row():
|
| 53 |
+
clear_button = gr.Button("Clear", variant="secondary", elem_classes="custom-button secondary")
|
| 54 |
+
submit_button = gr.Button("Submit", variant="primary", elem_classes="custom-button primary")
|
| 55 |
+
|
| 56 |
+
# Examples table
|
| 57 |
+
gr.Markdown("### Examples")
|
| 58 |
+
examples_table = gr.Dataframe(
|
| 59 |
+
value=[[ex["Input File"], ex["Output File"], ex["Input Type"]] for ex in examples],
|
| 60 |
+
headers=["Input File", "Output File", "Input Type"],
|
| 61 |
+
interactive=False,
|
| 62 |
+
elem_classes="custom-table"
|
| 63 |
+
)
|
| 64 |
+
examples_table.click(
|
| 65 |
+
fn=select_example,
|
| 66 |
+
inputs=[examples_table, examples_table],
|
| 67 |
+
outputs=[input_file, input_type, output_image, output_video, output_text]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
with gr.Column(scale=2):
|
| 71 |
+
with gr.Blocks():
|
| 72 |
+
output_image = gr.Image(label="Processed Output (Image)", type="numpy", visible=True, elem_classes="custom-image")
|
| 73 |
+
output_video = gr.Video(label="Processed Output (Video)", visible=False, elem_classes="custom-video")
|
| 74 |
+
output_text = gr.Textbox(label="Detected License Plates", lines=10, elem_classes="custom-textbox")
|
| 75 |
+
|
| 76 |
+
# Update preview and output visibility when input type changes
|
| 77 |
+
input_type.change(
|
| 78 |
+
fn=update_visibility,
|
| 79 |
+
inputs=input_type,
|
| 80 |
+
outputs=[input_preview_image, input_preview_video, output_image, output_video]
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Update preview when file is uploaded
|
| 84 |
+
input_file.change(
|
| 85 |
+
fn=update_preview,
|
| 86 |
+
inputs=[input_file, input_type],
|
| 87 |
+
outputs=[input_preview_image, input_preview_video]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Bind the processing function
|
| 91 |
+
submit_button.click(
|
| 92 |
+
fn=gradio_process,
|
| 93 |
+
inputs=[input_file, input_type],
|
| 94 |
+
outputs=[output_image, output_video, output_text, input_preview_image, input_preview_video]
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Clear button functionality
|
| 98 |
+
clear_button.click(
|
| 99 |
+
fn=lambda: (None, None, None, "Image", None, None, None, None),
|
| 100 |
+
outputs=[input_file, output_image, output_video, input_type, input_preview_image, input_preview_video, output_image, output_video]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
iface.launch(share=True)
|