danhtran2mind commited on
Commit
b100e5f
·
verified ·
1 Parent(s): 6c38f8e

Update apps/gradio_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Gradio Interface
14
- with gr.Blocks(css=custom_css) as iface:
15
- gr.Markdown(
16
- """
17
- # License Plate Detection and OCR
18
- Upload an image or video to detect and read license plates. Outputs are saved in `apps/gradio_app/temp_data/`.
19
- Debug logs are saved in `apps/gradio_app/debug.log`.
20
- """,
21
- elem_classes="markdown-title"
22
- )
23
-
24
- with gr.Row():
25
- with gr.Column(scale=1):
26
- input_file = gr.File(label="Upload Image or Video", elem_classes="custom-file-input")
27
- input_type = gr.Radio(choices=["Image", "Video"], label="Input Type", value="Image", elem_classes="custom-radio")
28
- with gr.Blocks():
29
- input_preview_image = gr.Image(label="Input Preview", visible=True, elem_classes="custom-image")
30
- input_preview_video = gr.Video(label="Input Preview", visible=False, elem_classes="custom-video")
31
- with gr.Row():
32
- clear_button = gr.Button("Clear", variant="secondary", elem_classes="custom-button secondary")
33
- submit_button = gr.Button("Submit", variant="primary", elem_classes="custom-button primary")
34
- with gr.Column(scale=2):
35
- with gr.Blocks():
36
- output_image = gr.Image(label="Processed Output (Image)", type="numpy", visible=True, elem_classes="custom-image")
37
- output_video = gr.Video(label="Processed Output (Video)", visible=False, elem_classes="custom-video")
38
- output_text = gr.Textbox(label="Detected License Plates", lines=10, elem_classes="custom-textbox")
39
-
40
- # Update preview and output visibility when input type changes
41
- input_type.change(
42
- fn=update_visibility,
43
- inputs=input_type,
44
- outputs=[input_preview_image, input_preview_video, output_image, output_video]
45
- )
46
-
47
- # Update preview when file is uploaded
48
- input_file.change(
49
- fn=update_preview,
50
- inputs=[input_file, input_type],
51
- outputs=[input_preview_image, input_preview_video]
52
- )
53
-
54
- # Bind the processing function
55
- submit_button.click(
56
- fn=gradio_process,
57
- inputs=[input_file, input_type],
58
- outputs=[output_image, output_video, output_text, input_preview_image, input_preview_video]
59
- )
60
-
61
- # Clear button functionality
62
- clear_button.click(
63
- fn=lambda: (None, None, None, "Image", None, None, None, None),
64
- outputs=[input_file, output_image, output_video, input_type, input_preview_image, input_preview_video, output_image, output_video]
65
- )
66
-
67
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)