Spaces:
Sleeping
Sleeping
varunmeena51307
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import pytesseract
|
|
7 |
|
8 |
# Load YOLO model from HuggingFace
|
9 |
repo_config = dict(
|
10 |
-
repo_id="arnabdhar/YOLOv8-nano-aadhar-card",
|
11 |
filename="model.pt",
|
12 |
local_dir="./models"
|
13 |
)
|
@@ -17,24 +17,28 @@ model = YOLO(hf_hub_download(**repo_config))
|
|
17 |
id2label = model.names
|
18 |
|
19 |
def predict(image):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
results = []
|
24 |
-
for box, confidence, class_id in zip(detections.xyxy, detections.confidence, detections.class_id):
|
25 |
-
x1, y1, x2, y2 = map(int, box)
|
26 |
-
label = id2label[class_id]
|
27 |
-
|
28 |
-
# Crop the detected region
|
29 |
-
cropped_region = image_np[y1:y2, x1:x2]
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
|
36 |
-
|
|
|
37 |
|
38 |
# Create Gradio interface
|
39 |
iface = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="text")
|
40 |
-
iface.launch()
|
|
|
7 |
|
8 |
# Load YOLO model from HuggingFace
|
9 |
repo_config = dict(
|
10 |
+
repo_id="arnabdhar/YOLOv8-nano-aadhar-card", # Ensure the repo is accessible
|
11 |
filename="model.pt",
|
12 |
local_dir="./models"
|
13 |
)
|
|
|
17 |
id2label = model.names
|
18 |
|
19 |
def predict(image):
|
20 |
+
try:
|
21 |
+
image_np = np.array(image)
|
22 |
+
detections = model.predict(image_np)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
results = []
|
25 |
+
for box, confidence, class_id in zip(detections.xyxy, detections.confidence, detections.class_id):
|
26 |
+
x1, y1, x2, y2 = map(int, box)
|
27 |
+
label = id2label[class_id]
|
28 |
+
|
29 |
+
# Crop the detected region
|
30 |
+
cropped_region = image_np[y1:y2, x1:x2]
|
31 |
+
|
32 |
+
# Perform OCR on the cropped region
|
33 |
+
ocr_text = pytesseract.image_to_string(cropped_region, config='--psm 6')
|
34 |
+
|
35 |
+
results.append(f"Detected {label}: {ocr_text.strip()} with confidence {confidence:.2f}")
|
36 |
|
37 |
+
return "\n".join(results) # Return as a single string for easier display in Gradio
|
38 |
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error: {str(e)}" # Basic error handling
|
41 |
|
42 |
# Create Gradio interface
|
43 |
iface = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="text")
|
44 |
+
iface.launch()
|