Update app.py
Browse files
app.py
CHANGED
@@ -27,25 +27,30 @@ def detect_and_crop_document(image):
|
|
27 |
results = modelY(image_np, conf=0.85, device='cpu')
|
28 |
cropped_images = []
|
29 |
predictions = []
|
|
|
30 |
for result in results:
|
31 |
for box in result.boxes:
|
32 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
33 |
conf = int(box.conf[0] * 100) # Convert confidence to percentage
|
34 |
cls = int(box.cls[0])
|
35 |
-
class_name = modelY.names[cls].capitalize() #
|
36 |
cropped_image_np = image_np[y1:y2, x1:x2]
|
37 |
cropped_image = Image.fromarray(cropped_image_np)
|
38 |
cropped_images.append(cropped_image)
|
39 |
-
predictions.append(f"STNK {class_name} -- (Confidence: {conf}%)")
|
|
|
40 |
if not cropped_images:
|
41 |
return None, "No document detected"
|
42 |
-
return cropped_images
|
43 |
|
44 |
# Gradio interface
|
45 |
def process_image(image):
|
46 |
preprocessed_image = preprocessing(image)
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
with gr.Blocks(css=".gr-button {background-color: #4caf50; color: white; font-size: 16px; padding: 10px 20px; border-radius: 8px;}") as demo:
|
51 |
gr.Markdown(
|
@@ -61,7 +66,7 @@ with gr.Blocks(css=".gr-button {background-color: #4caf50; color: white; font-si
|
|
61 |
clear_btn = gr.Button("Clear")
|
62 |
submit_btn = gr.Button("Detect Document")
|
63 |
with gr.Column(scale=2):
|
64 |
-
output_image = gr.
|
65 |
output_text = gr.Textbox(label="Detection Result", interactive=False)
|
66 |
|
67 |
submit_btn.click(process_image, inputs=input_image, outputs=[output_image, output_text])
|
|
|
27 |
results = modelY(image_np, conf=0.85, device='cpu')
|
28 |
cropped_images = []
|
29 |
predictions = []
|
30 |
+
|
31 |
for result in results:
|
32 |
for box in result.boxes:
|
33 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
34 |
conf = int(box.conf[0] * 100) # Convert confidence to percentage
|
35 |
cls = int(box.cls[0])
|
36 |
+
class_name = modelY.names[cls].capitalize() # Capitalize class names
|
37 |
cropped_image_np = image_np[y1:y2, x1:x2]
|
38 |
cropped_image = Image.fromarray(cropped_image_np)
|
39 |
cropped_images.append(cropped_image)
|
40 |
+
predictions.append(f"Detected: STNK {class_name} -- (Confidence: {conf}%)")
|
41 |
+
|
42 |
if not cropped_images:
|
43 |
return None, "No document detected"
|
44 |
+
return cropped_images, predictions
|
45 |
|
46 |
# Gradio interface
|
47 |
def process_image(image):
|
48 |
preprocessed_image = preprocessing(image)
|
49 |
+
cropped_images, predictions = detect_and_crop_document(preprocessed_image)
|
50 |
+
|
51 |
+
if cropped_images:
|
52 |
+
return cropped_images, '\n'.join(predictions)
|
53 |
+
return None, "No document detected"
|
54 |
|
55 |
with gr.Blocks(css=".gr-button {background-color: #4caf50; color: white; font-size: 16px; padding: 10px 20px; border-radius: 8px;}") as demo:
|
56 |
gr.Markdown(
|
|
|
66 |
clear_btn = gr.Button("Clear")
|
67 |
submit_btn = gr.Button("Detect Document")
|
68 |
with gr.Column(scale=2):
|
69 |
+
output_image = gr.Gallery(label="Cropped Documents", interactive=False)
|
70 |
output_text = gr.Textbox(label="Detection Result", interactive=False)
|
71 |
|
72 |
submit_btn.click(process_image, inputs=input_image, outputs=[output_image, output_text])
|