khulnasoft commited on
Commit
d9a010b
·
verified ·
1 Parent(s): 5fb1e5d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +205 -0
app.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from typing import List
3
+
4
+ import cv2
5
+ import os
6
+
7
+ import numpy as np
8
+ import gradio as gr
9
+ import supervision as sv
10
+ from inference_sdk import (
11
+ InferenceHTTPClient,
12
+ InferenceConfiguration,
13
+ VisualisationResponseFormat
14
+ )
15
+
16
+
17
+ def read_json_file(file_path: str) -> dict:
18
+ with open(file_path, 'r') as file:
19
+ return json.load(file)
20
+
21
+
22
+ def split_and_strip(text: str) -> List[str]:
23
+ return [part.strip() for part in text.split(',')]
24
+
25
+
26
+ MARKDOWN = """
27
+ # WORKFLOWS 🛠
28
+ Define complex ML pipelines in JSON and execute it, running multiple models, fusing
29
+ outputs seamlessly.
30
+ Use self-hosted Inference HTTP [container](https://inference.khulnasoft.com/inference_helpers/inference_cli/#inference-server-start)
31
+ or run against KhulnaSoft [API](https://detect.khulnasoft.com/docs)
32
+ to get results without single line of code written.
33
+ """
34
+
35
+ # LICENSE PLATES WORKFLOW
36
+
37
+ LICENSE_PLATES_MARKDOWN = """
38
+ ![license-plates-detection-workflow](
39
+ https://media.khulnasoft.com/inference/license-plates-detection-workflow.png)
40
+ """
41
+ LICENSE_PLATES_EXAMPLES = [
42
+ "https://media.khulnasoft.com/inference/license_plate_1.jpg",
43
+ "https://media.khulnasoft.com/inference/license_plate_2.jpg",
44
+ ]
45
+ LICENSE_PLATES_SPECIFICATION_PATH = 'configs/license_plates.json'
46
+ LICENSE_PLATES_SPECIFICATION = read_json_file(LICENSE_PLATES_SPECIFICATION_PATH)
47
+ LICENSE_PLATES_SPECIFICATION_STRING = f"""
48
+ ```json
49
+ {json.dumps(LICENSE_PLATES_SPECIFICATION, indent=4)}
50
+ ```
51
+ """
52
+
53
+ # CAR BRAND WORKFLOW
54
+
55
+ CAR_BRANDS_MARKDOWN = """
56
+ ![car-brand-workflow](
57
+ https://media.khulnasoft.com/inference/car-brand-workflow.png.png)
58
+ """
59
+ CAR_BRANDS_EXAMPLES = [
60
+ ["Lexus, Honda, Seat", "https://media.khulnasoft.com/inference/multiple_cars_1.jpg"],
61
+ ["Volkswagen, Renault, Mercedes", "https://media.khulnasoft.com/inference/multiple_cars_2.jpg"],
62
+ ]
63
+ CAR_BRANDS_SPECIFICATION_PATH = 'configs/car_brands.json'
64
+ CAR_BRANDS_SPECIFICATION = read_json_file(CAR_BRANDS_SPECIFICATION_PATH)
65
+ CAR_BRANDS_SPECIFICATION_STRING = f"""
66
+ ```json
67
+ {json.dumps(CAR_BRANDS_SPECIFICATION, indent=4)}
68
+ ```
69
+ """
70
+
71
+ API_URL = os.getenv('API_URL', None)
72
+ API_KEY = os.getenv('API_KEY', None)
73
+ print("API_URL", API_URL)
74
+
75
+ if API_KEY is None or API_URL is None:
76
+ raise ValueError("API_URL and API_KEY environment variables are required")
77
+
78
+
79
+ CLIENT = InferenceHTTPClient(api_url=API_URL, api_key=API_KEY)
80
+
81
+ CLIENT.configure(InferenceConfiguration(
82
+ output_visualisation_format=VisualisationResponseFormat.NUMPY))
83
+
84
+
85
+ def annotate_image(image: np.ndarray, detections: sv.Detections) -> np.ndarray:
86
+ h, w, _ = image.shape
87
+ annotated_image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
88
+ line_thickness = sv.calculate_dynamic_line_thickness(resolution_wh=(w, h))
89
+ text_scale = sv.calculate_dynamic_text_scale(resolution_wh=(w, h))
90
+ bounding_box_annotator = sv.BoundingBoxAnnotator(thickness=line_thickness)
91
+ label_annotator = sv.LabelAnnotator(
92
+ text_scale=text_scale,
93
+ text_thickness=line_thickness
94
+ )
95
+ annotated_image = bounding_box_annotator.annotate(
96
+ annotated_image, detections)
97
+ annotated_image = label_annotator.annotate(
98
+ annotated_image, detections)
99
+ return cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
100
+
101
+
102
+ def inference_license_plates(input_image: np.ndarray) -> np.ndarray:
103
+ result = CLIENT.infer_from_workflow(
104
+ specification=LICENSE_PLATES_SPECIFICATION["specification"],
105
+ images={"image": input_image},
106
+ )
107
+ detections = sv.Detections.from_inference(result)
108
+ if len(detections) == 0:
109
+ return input_image
110
+
111
+ detections['class_name'] = (
112
+ result["recognised_plates"]
113
+ if isinstance(result["recognised_plates"], list)
114
+ else [result["recognised_plates"]]
115
+ )
116
+ return annotate_image(input_image, detections)
117
+
118
+
119
+ def inference_car_brands(input_text: str, input_image: np.ndarray) -> np.ndarray:
120
+ classes = split_and_strip(input_text)
121
+ result = CLIENT.infer_from_workflow(
122
+ specification=CAR_BRANDS_SPECIFICATION["specification"],
123
+ images={"image": input_image},
124
+ parameters={"car_types": classes}
125
+ )
126
+
127
+ detections = sv.Detections.from_inference(result)
128
+ if len(detections) == 0:
129
+ return input_image
130
+
131
+ if len(detections) > 1:
132
+ class_ids = np.argmax(result["clip"], axis=1)
133
+ else:
134
+ class_ids = np.array([np.argmax(result["clip"], axis=0)])
135
+
136
+ detections.class_ids = class_ids
137
+ detections['class_name'] = [classes[class_id] for class_id in class_ids]
138
+
139
+ return annotate_image(input_image, detections)
140
+
141
+
142
+ with gr.Blocks() as demo:
143
+ gr.Markdown(MARKDOWN)
144
+ with gr.Tab(label="License Plates"):
145
+ gr.Markdown(LICENSE_PLATES_MARKDOWN)
146
+ with gr.Accordion("Configuration JSON", open=False):
147
+ gr.Markdown(LICENSE_PLATES_SPECIFICATION_STRING)
148
+ with gr.Row():
149
+ license_plates_input_image_component = gr.Image(
150
+ type='numpy',
151
+ label='Input Image'
152
+ )
153
+ license_plates_output_image_component = gr.Image(
154
+ type='numpy',
155
+ label='Output Image'
156
+ )
157
+ with gr.Row():
158
+ license_plates_submit_button_component = gr.Button('Submit')
159
+ gr.Examples(
160
+ fn=inference_license_plates,
161
+ examples=LICENSE_PLATES_EXAMPLES,
162
+ inputs=license_plates_input_image_component,
163
+ outputs=license_plates_output_image_component,
164
+ cache_examples=True
165
+ )
166
+ with gr.Tab(label="Car Brands"):
167
+ gr.Markdown(CAR_BRANDS_MARKDOWN)
168
+ with gr.Accordion("Configuration JSON", open=False):
169
+ gr.Markdown(CAR_BRANDS_SPECIFICATION_STRING)
170
+ with gr.Row():
171
+ with gr.Column():
172
+ car_brands_input_image_component = gr.Image(
173
+ type='numpy',
174
+ label='Input Image'
175
+ )
176
+ car_brands_input_text = gr.Textbox(
177
+ label='Car Brands',
178
+ placeholder='Enter car brands separated by comma'
179
+ )
180
+ car_brands_output_image_component = gr.Image(
181
+ type='numpy',
182
+ label='Output Image'
183
+ )
184
+ with gr.Row():
185
+ car_brands_submit_button_component = gr.Button('Submit')
186
+ gr.Examples(
187
+ fn=inference_car_brands,
188
+ examples=CAR_BRANDS_EXAMPLES,
189
+ inputs=[car_brands_input_text, car_brands_input_image_component],
190
+ outputs=car_brands_output_image_component,
191
+ cache_examples=True
192
+ )
193
+
194
+ license_plates_submit_button_component.click(
195
+ fn=inference_license_plates,
196
+ inputs=license_plates_input_image_component,
197
+ outputs=license_plates_output_image_component
198
+ )
199
+ car_brands_submit_button_component.click(
200
+ fn=inference_car_brands,
201
+ inputs=[car_brands_input_text, car_brands_input_image_component],
202
+ outputs=car_brands_output_image_component
203
+ )
204
+
205
+ demo.launch(debug=False, show_error=True)