Nayera-2025 commited on
Commit
e3ea99d
·
verified ·
1 Parent(s): fde8e7c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import easyocr
3
+ import torch
4
+ from transformers import pipeline
5
+ import numpy as np
6
+
7
+ # Load OCR model (English, Arabic, French, Chinese)
8
+ reader = easyocr.Reader(['en', 'ar', 'fr', 'zh'])
9
+
10
+ # Load YOLOv5 model (small model for faster performance)
11
+ model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
12
+
13
+ # Load translation model (multilingual to English)
14
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
15
+
16
+ def process_image(image):
17
+ # Convert PIL Image to numpy array if needed
18
+ if not isinstance(image, np.ndarray):
19
+ image = np.array(image)
20
+
21
+ # Step 1: OCR - Text Extraction
22
+ text_results = reader.readtext(image)
23
+ extracted_texts = [res[1] for res in text_results]
24
+ extracted_text = " ".join(extracted_texts)
25
+
26
+ # Step 2: Translate Text
27
+ if extracted_text:
28
+ translation = translator(extracted_text)[0]['translation_text']
29
+ else:
30
+ translation = "No text detected."
31
+
32
+ # Step 3: Object Detection - Bounding Boxes
33
+ results = model(image)
34
+ detected_img = results.render()[0] # returns a list, take first image
35
+
36
+ return detected_img, extracted_text, translation
37
+
38
+ # Define Gradio Interface
39
+ iface = gr.Interface(
40
+ fn=process_image,
41
+ inputs=gr.Image(type="pil", label="Upload Image (Signs, ID, License Plate)"),
42
+ outputs=[
43
+ gr.Image(label="Detected Objects in Image"),
44
+ gr.Textbox(label="Extracted Text"),
45
+ gr.Textbox(label="Translated Text to English")
46
+ ],
47
+ title="🚨 Police Smart Glasses AI Demo",
48
+ description="Upload an image to simulate smart glasses detecting text, translating it, and recognizing objects."
49
+ )
50
+
51
+ if __name__ == "__main__":
52
+ iface.launch()