Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import gradio as gr
|
5 |
+
import webbrowser
|
6 |
+
import threading
|
7 |
+
from typing import Dict, Any
|
8 |
+
from vision_agent.tools import (
|
9 |
+
load_image,
|
10 |
+
save_image,
|
11 |
+
overlay_bounding_boxes,
|
12 |
+
ocr
|
13 |
+
)
|
14 |
+
|
15 |
+
def analyze_pills(image_path: str) -> Dict[str, Any]:
|
16 |
+
|
17 |
+
# Load the image
|
18 |
+
image = load_image(image_path)
|
19 |
+
|
20 |
+
# Perform OCR to detect text
|
21 |
+
detections = ocr(image)
|
22 |
+
detected_texts = [d['label'] for d in detections]
|
23 |
+
|
24 |
+
# Initialize drug description
|
25 |
+
drug_description = "No description found."
|
26 |
+
if detected_texts:
|
27 |
+
drug_description = f"Click the button below to search for: {''.join(detected_texts)}"
|
28 |
+
|
29 |
+
# Prepare bounding boxes for visualization
|
30 |
+
boxes_for_overlay = [
|
31 |
+
{
|
32 |
+
'label': f"{d['label']}",
|
33 |
+
'score': d['score'],
|
34 |
+
'bbox': d['bbox'] # Already normalized coordinates
|
35 |
+
}
|
36 |
+
for d in detections
|
37 |
+
]
|
38 |
+
|
39 |
+
# Create and save annotated image
|
40 |
+
annotated_image = overlay_bounding_boxes(image, boxes_for_overlay)
|
41 |
+
save_image(annotated_image, "annotated_pills.jpg")
|
42 |
+
|
43 |
+
return detected_texts, annotated_image, drug_description
|
44 |
+
|
45 |
+
def open_split_window(url):
|
46 |
+
"""Opens the Drugs.com search in a new window."""
|
47 |
+
threading.Thread(target=lambda: webbrowser.open(url, new=1)).start()
|
48 |
+
|
49 |
+
def search_drug_online(imprint_text: list):
|
50 |
+
"""Opens a web search for the given pill imprint in a window."""
|
51 |
+
if imprint_text:
|
52 |
+
combined_query = ''.join(imprint_text)
|
53 |
+
search_url = f"https://www.drugs.com/search.php?searchterm={combined_query}"
|
54 |
+
open_split_window(search_url)
|
55 |
+
return f"Searching online for: {combined_query}"
|
56 |
+
return "No imprint text available."
|
57 |
+
|
58 |
+
# Gradio Interface
|
59 |
+
with gr.Blocks() as app:
|
60 |
+
gr.Markdown("## 🏥 **Pill Analysis Tool**")
|
61 |
+
gr.Markdown("Upload an image of a pill to detect imprint text.")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
image_input = gr.Image(type="filepath", label="Upload Pill Image")
|
65 |
+
output_text = gr.Textbox(label="Detected Imprint Text")
|
66 |
+
output_description = gr.Textbox(label="Drug Identifiers")
|
67 |
+
|
68 |
+
image_output = gr.Image(label="Annotated Image with Text Detection")
|
69 |
+
analyze_button = gr.Button("Analyze Pill")
|
70 |
+
search_button = gr.Button("Search Drug Info - New Browser Tab will Open")
|
71 |
+
|
72 |
+
analyze_button.click(
|
73 |
+
fn=analyze_pills,
|
74 |
+
inputs=image_input,
|
75 |
+
outputs=[output_text, image_output, output_description]
|
76 |
+
)
|
77 |
+
|
78 |
+
search_button.click(
|
79 |
+
fn=search_drug_online,
|
80 |
+
inputs=[output_text],
|
81 |
+
outputs=output_description
|
82 |
+
)
|
83 |
+
|
84 |
+
app.launch()
|