Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +130 -0
- back_cnic_model.pt +3 -0
- front_cnic_model.pt +3 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from ultralytics import YOLO
|
5 |
+
import pytesseract
|
6 |
+
import qrcode
|
7 |
+
from pyzbar.pyzbar import decode
|
8 |
+
import json
|
9 |
+
from PIL import Image
|
10 |
+
|
11 |
+
# Load YOLO models
|
12 |
+
front_model = YOLO("front_cnic_model.pt")
|
13 |
+
back_model = YOLO("back_cnic_model.pt")
|
14 |
+
|
15 |
+
def preprocess_image(image):
|
16 |
+
# Convert PIL Image to numpy array
|
17 |
+
img = np.array(image)
|
18 |
+
# Convert RGB to BGR for OpenCV
|
19 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
20 |
+
return img
|
21 |
+
|
22 |
+
def extract_text(image, boxes):
|
23 |
+
# Perform OCR on detected regions
|
24 |
+
results = {}
|
25 |
+
for box in boxes:
|
26 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
27 |
+
cls_name = front_model.names[int(box.cls)]
|
28 |
+
# Crop the region
|
29 |
+
roi = image[y1:y2, x1:x2]
|
30 |
+
# Convert to grayscale
|
31 |
+
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
|
32 |
+
# Apply OCR
|
33 |
+
text = pytesseract.image_to_string(gray, config='--psm 6').strip()
|
34 |
+
if text:
|
35 |
+
results[cls_name] = text
|
36 |
+
return results
|
37 |
+
|
38 |
+
def process_front_cnic(image):
|
39 |
+
try:
|
40 |
+
# Preprocess image
|
41 |
+
img = preprocess_image(image)
|
42 |
+
|
43 |
+
# Run front CNIC detection
|
44 |
+
results = front_model.predict(img, conf=0.5)
|
45 |
+
|
46 |
+
# Extract text from detected regions
|
47 |
+
extracted_info = extract_text(img, results[0].boxes)
|
48 |
+
|
49 |
+
return extracted_info if extracted_info else {"error": "No text detected"}
|
50 |
+
except Exception as e:
|
51 |
+
return {"error": str(e)}
|
52 |
+
|
53 |
+
def process_back_cnic(image):
|
54 |
+
try:
|
55 |
+
# Preprocess image
|
56 |
+
img = preprocess_image(image)
|
57 |
+
|
58 |
+
# Run back CNIC detection
|
59 |
+
results = back_model.predict(img, conf=0.5)
|
60 |
+
|
61 |
+
output = {}
|
62 |
+
# Process detected objects
|
63 |
+
for box in results[0].boxes:
|
64 |
+
cls_name = back_model.names[int(box.cls)]
|
65 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
66 |
+
roi = img[y1:y2, x1:x2]
|
67 |
+
|
68 |
+
if cls_name.lower() == "qr scan":
|
69 |
+
# Decode QR code
|
70 |
+
qr_result = decode(Image.fromarray(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)))
|
71 |
+
if qr_result:
|
72 |
+
output["QR Scan"] = qr_result[0].data.decode('utf-8')
|
73 |
+
else:
|
74 |
+
output["QR Scan"] = "No QR code detected"
|
75 |
+
|
76 |
+
elif cls_name.lower() == "barcode":
|
77 |
+
# Decode barcode
|
78 |
+
barcode_result = decode(Image.fromarray(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)))
|
79 |
+
if barcode_result:
|
80 |
+
output["Barcode"] = barcode_result[0].data.decode('utf-8')
|
81 |
+
else:
|
82 |
+
output["Barcode"] = "No barcode detected"
|
83 |
+
|
84 |
+
elif cls_name.lower() == "cnic":
|
85 |
+
# Extract CNIC number using OCR
|
86 |
+
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
|
87 |
+
cnic_text = pytesseract.image_to_string(gray, config='--psm 6').strip()
|
88 |
+
output["CNIC"] = cnic_text if cnic_text else "No CNIC number detected"
|
89 |
+
|
90 |
+
return output if output else {"error": "No objects detected"}
|
91 |
+
except Exception as e:
|
92 |
+
return {"error": str(e)}
|
93 |
+
|
94 |
+
# Gradio Interface
|
95 |
+
with gr.Blocks() as demo:
|
96 |
+
gr.Markdown("# CNIC Detection and Information Extraction")
|
97 |
+
|
98 |
+
with gr.Tab("Front CNIC"):
|
99 |
+
front_input = gr.Image(type="pil", label="Upload Front CNIC Image")
|
100 |
+
front_output = gr.JSON(label="Extracted Information")
|
101 |
+
front_button = gr.Button("Process Front CNIC")
|
102 |
+
|
103 |
+
with gr.Tab("Back CNIC"):
|
104 |
+
back_input = gr.Image(type="pil", label="Upload Back CNIC Image")
|
105 |
+
back_output = gr.JSON(label="Extracted Information")
|
106 |
+
back_button = gr.Button("Process Back CNIC")
|
107 |
+
|
108 |
+
# Connect buttons to processing functions
|
109 |
+
front_button.click(
|
110 |
+
fn=process_front_cnic,
|
111 |
+
inputs=front_input,
|
112 |
+
outputs=front_output
|
113 |
+
)
|
114 |
+
|
115 |
+
back_button.click(
|
116 |
+
fn=process_back_cnic,
|
117 |
+
inputs=back_input,
|
118 |
+
outputs=back_output
|
119 |
+
)
|
120 |
+
|
121 |
+
# API endpoints
|
122 |
+
api = gr.Interface(
|
123 |
+
fn=[process_front_cnic, process_back_cnic],
|
124 |
+
inputs=[gr.Image(type="pil"), gr.Image(type="pil")],
|
125 |
+
outputs=[gr.JSON(), gr.JSON()],
|
126 |
+
api_name="cnic_detection"
|
127 |
+
)
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
demo.launch()
|
back_cnic_model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f697acb582e8549bf44611b44f0c0534d0b0a81350e3d4b6c151cc67d0a7c735
|
3 |
+
size 6236899
|
front_cnic_model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6823c4e300a2d43513912e69f7b8883fc94914bf81e8cf31926b38b0a588da53
|
3 |
+
size 6258659
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.44.0
|
2 |
+
opencv-python==4.10.0.84
|
3 |
+
numpy==1.26.4
|
4 |
+
ultralytics==8.3.15
|
5 |
+
pytesseract==0.3.13
|
6 |
+
pyzbar==0.1.9
|
7 |
+
pillow==10.4.0
|