Create app2.py
Browse files
app2.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image, ImageEnhance
|
| 6 |
+
from ultralytics import YOLO
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
model_path = "best.pt"
|
| 10 |
+
model = YOLO(model_path)
|
| 11 |
+
|
| 12 |
+
def preprocessing(image):
|
| 13 |
+
|
| 14 |
+
image = Image.fromarray(np.array(image))
|
| 15 |
+
|
| 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 |
+
|
| 20 |
+
|
| 21 |
+
width = 800
|
| 22 |
+
aspect_ratio = image.height / image.width
|
| 23 |
+
height = int(width * aspect_ratio)
|
| 24 |
+
image = image.resize((width, height))
|
| 25 |
+
|
| 26 |
+
return image
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def imageRotation(image):
|
| 30 |
+
"""Dummy function for image rotation."""
|
| 31 |
+
return image
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def detect_document(image):
|
| 35 |
+
"""Detects front and back of the document using YOLO."""
|
| 36 |
+
image = np.array(image)
|
| 37 |
+
results = model(image, conf=0.85)
|
| 38 |
+
|
| 39 |
+
detected_classes = set()
|
| 40 |
+
labels = []
|
| 41 |
+
bounding_boxes = []
|
| 42 |
+
|
| 43 |
+
for result in results:
|
| 44 |
+
for box in result.boxes:
|
| 45 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 46 |
+
conf = box.conf[0]
|
| 47 |
+
cls = int(box.cls[0])
|
| 48 |
+
class_name = model.names[cls]
|
| 49 |
+
|
| 50 |
+
detected_classes.add(class_name)
|
| 51 |
+
label = f"{class_name} {conf:.2f}"
|
| 52 |
+
labels.append(label)
|
| 53 |
+
bounding_boxes.append((x1, y1, x2, y2, class_name, conf)) # Store bounding box with class and confidence
|
| 54 |
+
|
| 55 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 56 |
+
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 57 |
+
|
| 58 |
+
possible_classes = {"front", "back"}
|
| 59 |
+
missing_classes = possible_classes - detected_classes
|
| 60 |
+
if missing_classes:
|
| 61 |
+
labels.append(f"Missing: {', '.join(missing_classes)}")
|
| 62 |
+
|
| 63 |
+
return Image.fromarray(image), labels, bounding_boxes
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def crop_image(image, bounding_boxes):
|
| 67 |
+
"""Crops detected bounding boxes from the image."""
|
| 68 |
+
cropped_images = {}
|
| 69 |
+
image = np.array(image)
|
| 70 |
+
|
| 71 |
+
for (x1, y1, x2, y2, class_name, conf) in bounding_boxes:
|
| 72 |
+
cropped = image[y1:y2, x1:x2]
|
| 73 |
+
cropped_images[class_name] = Image.fromarray(cropped)
|
| 74 |
+
|
| 75 |
+
return cropped_images
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def vision_ai_api(image, doc_type):
|
| 79 |
+
"""Dummy API call for Vision AI, returns a fake JSON response."""
|
| 80 |
+
return {
|
| 81 |
+
"document_type": doc_type,
|
| 82 |
+
"extracted_text": "Dummy OCR result for " + doc_type,
|
| 83 |
+
"confidence": 0.99
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
# ---------------- Prediction Function ---------------- #
|
| 87 |
+
def predict(image):
|
| 88 |
+
"""Pipeline: Preprocess -> Detect -> Crop -> Vision AI API."""
|
| 89 |
+
processed_image = preprocessing(image)
|
| 90 |
+
rotated_image = imageRotation(processed_image)
|
| 91 |
+
detected_image, labels, bounding_boxes = detect_document(rotated_image)
|
| 92 |
+
|
| 93 |
+
cropped_images = crop_image(rotated_image, bounding_boxes)
|
| 94 |
+
|
| 95 |
+
# Call Vision AI separately for front and back if detected
|
| 96 |
+
front_result, back_result = None, None
|
| 97 |
+
if "front" in cropped_images:
|
| 98 |
+
front_result = vision_ai_api(cropped_images["front"], "front")
|
| 99 |
+
if "back" in cropped_images:
|
| 100 |
+
back_result = vision_ai_api(cropped_images["back"], "back")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
api_results = {
|
| 104 |
+
"front": front_result,
|
| 105 |
+
"back": back_result
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
return detected_image, labels, api_results
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
iface = gr.Interface(
|
| 112 |
+
fn=predict,
|
| 113 |
+
inputs="image",
|
| 114 |
+
outputs=["image", "text", "json"],
|
| 115 |
+
title="License Field Detection (Front & Back Card)"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
iface.launch()
|