Spaces:
Sleeping
Sleeping
Commit
·
dd3c69b
1
Parent(s):
130eb78
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import gtts as gt
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from googletrans import Translator
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
def take_photo():
|
| 9 |
+
camera = cv2.VideoCapture(0)
|
| 10 |
+
ret, frame = camera.read()
|
| 11 |
+
image = Image.fromarray(frame)
|
| 12 |
+
camera.release()
|
| 13 |
+
return image
|
| 14 |
+
|
| 15 |
+
def trans(text, lang='ta'):
|
| 16 |
+
translator = Translator()
|
| 17 |
+
out = translator.translate(text, dest=lang)
|
| 18 |
+
tts = gt.gTTS(text=out.text, lang=lang)
|
| 19 |
+
tts.save("audio.mp3")
|
| 20 |
+
return "done"
|
| 21 |
+
|
| 22 |
+
def object_recognition(lang):
|
| 23 |
+
image = take_photo()
|
| 24 |
+
API_URL = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
|
| 25 |
+
headers = {"Authorization": "Bearer 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"}
|
| 26 |
+
def query(filename):
|
| 27 |
+
with open(filename, "rb") as f:
|
| 28 |
+
data = f.read()
|
| 29 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
| 30 |
+
return response.json()
|
| 31 |
+
output = query(image)
|
| 32 |
+
text = output[0]['generated_text']
|
| 33 |
+
op = trans(text, lang)
|
| 34 |
+
return op
|
| 35 |
+
|
| 36 |
+
def ocr_detection(lang):
|
| 37 |
+
image = take_photo()
|
| 38 |
+
# Assuming you have the correct endpoint and API key for the OCR service
|
| 39 |
+
# client = Client("https://kneelesh48-tesseract-ocr.hf.space/")
|
| 40 |
+
# result = client.predict(image, "afr", api_name="/tesseract-ocr")
|
| 41 |
+
# print(result)
|
| 42 |
+
# op = trans(result, lang)
|
| 43 |
+
op = trans("OCR Detection Result", lang) # Placeholder result for demonstration
|
| 44 |
+
return op
|
| 45 |
+
|
| 46 |
+
def operator(img, value, lang):
|
| 47 |
+
if value == "1":
|
| 48 |
+
op = object_recognition(lang)
|
| 49 |
+
elif value == "2":
|
| 50 |
+
op = ocr_detection(lang)
|
| 51 |
+
else:
|
| 52 |
+
op = trans("Sorry, I can't perform this operation.", lang)
|
| 53 |
+
return op
|
| 54 |
+
|
| 55 |
+
# Create Streamlit app
|
| 56 |
+
st.title("Image Processing App")
|
| 57 |
+
|
| 58 |
+
# Add input components
|
| 59 |
+
image_input = st.checkbox("Take a photo")
|
| 60 |
+
if image_input:
|
| 61 |
+
image = take_photo()
|
| 62 |
+
st.image(image, caption="Captured Image", use_column_width=True)
|
| 63 |
+
|
| 64 |
+
operation = st.selectbox("Select an operation", ["Object Recognition", "OCR Detection"])
|
| 65 |
+
if operation:
|
| 66 |
+
lang = st.text_input("Enter language code (e.g., 'ta' for Tamil)")
|
| 67 |
+
result = operator(image, operation[0], lang)
|
| 68 |
+
st.text("Result: " + result)
|