ammariii08 commited on
Commit
1393304
·
verified ·
1 Parent(s): 79da9b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image, ImageEnhance
4
+ from ultralytics import YOLO
5
+ import cv2
6
+
7
+ # Load YOLO model
8
+ model_path = "./best.pt"
9
+ modelY = YOLO(model_path)
10
+ modelY.to('cpu')
11
+
12
+ # Preprocessing function
13
+ def preprocessing(image):
14
+ if image.mode != 'RGB':
15
+ image = image.convert('RGB')
16
+ image = ImageEnhance.Sharpness(image).enhance(2.0)
17
+ image = ImageEnhance.Contrast(image).enhance(1.5)
18
+ image = ImageEnhance.Brightness(image).enhance(0.8)
19
+ width = 448
20
+ aspect_ratio = image.height / image.width
21
+ height = int(width * aspect_ratio)
22
+ return image.resize((width, height))
23
+
24
+ # YOLO document detection and cropping
25
+ def detect_and_crop_document(image):
26
+ image_np = np.array(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() # Ensure class names are capitalized
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[0], predictions[0]
43
+
44
+ # Gradio interface
45
+ def process_image(image):
46
+ preprocessed_image = preprocessing(image)
47
+ cropped_image, prediction = detect_and_crop_document(preprocessed_image)
48
+ return cropped_image, prediction
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(
52
+ """
53
+ <h1 style="text-align: center; color: #4caf50;">📜 License Registration Classification</h1>
54
+ <p style="text-align: center; font-size: 18px;">Upload an image and let the YOLO model detect and crop license documents automatically.</p>
55
+ """
56
+ )
57
+ with gr.Row():
58
+ with gr.Column(scale=1, min_width=300):
59
+ input_image = gr.Image(type="pil", label="Upload License Image", interactive=True)
60
+ with gr.Row():
61
+ clear_btn = gr.Button("Clear")
62
+ submit_btn = gr.Button("Detect Document")
63
+ with gr.Column(scale=2):
64
+ output_image = gr.Image(label="Cropped Document", interactive=False)
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])
68
+ clear_btn.click(lambda: (None, ""), outputs=[output_image, output_text])
69
+
70
+ demo.launch()