Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from azure.ai.vision.imageanalysis import ImageAnalysisClient
|
| 3 |
+
from azure.ai.vision.imageanalysis.models import VisualFeatures
|
| 4 |
+
from azure.core.credentials import AzureKeyCredential
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def sample_ocr_image_file(image1, end_point, API_Key):
|
| 8 |
+
|
| 9 |
+
if (end_point == '' or API_Key == ''):
|
| 10 |
+
output = "*** Please provide EndPoint URL and API_KEY ***"
|
| 11 |
+
else:
|
| 12 |
+
try:
|
| 13 |
+
endpoint = end_point
|
| 14 |
+
key = API_Key
|
| 15 |
+
# Create an Image Analysis client
|
| 16 |
+
client = ImageAnalysisClient(
|
| 17 |
+
endpoint=endpoint,
|
| 18 |
+
credential=AzureKeyCredential(key)
|
| 19 |
+
)
|
| 20 |
+
# [START read]
|
| 21 |
+
# Load image to analyze into a 'bytes' object
|
| 22 |
+
with open(image1, "rb") as f:
|
| 23 |
+
image_data = f.read()
|
| 24 |
+
|
| 25 |
+
# Extract text (OCR) from an image stream. This will be a synchronously (blocking) call.
|
| 26 |
+
result = client.analyze(
|
| 27 |
+
image_data=image_data,
|
| 28 |
+
visual_features=[VisualFeatures.READ]
|
| 29 |
+
)
|
| 30 |
+
if result.read is not None:
|
| 31 |
+
sentence = ''
|
| 32 |
+
for line in result.read.blocks[0].lines:
|
| 33 |
+
sentence += f" {line.text}" + "\n"
|
| 34 |
+
print(f" Line: '{line.text}'")
|
| 35 |
+
output = sentence
|
| 36 |
+
|
| 37 |
+
except :
|
| 38 |
+
output = "*** Please check EndPoint URL and API_KEY ***"
|
| 39 |
+
|
| 40 |
+
return output
|
| 41 |
+
|
| 42 |
+
demo = gr.Interface( sample_ocr_image_file,
|
| 43 |
+
[gr.UploadButton("Click to Upload an Image File", file_types=["image"]), gr.Textbox(label="Enter your Endpoint URL", placeholder="URL", lines=1),gr.Textbox(type = "password", label="Enter your API-Key", placeholder="API-Key", lines=1)],
|
| 44 |
+
[gr.Textbox(label = "Output")]
|
| 45 |
+
|
| 46 |
+
).launch()
|