JaganathC commited on
Commit
1742599
·
verified ·
1 Parent(s): 8f7cda9

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +102 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,104 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import cv2
3
+ import pytesseract
4
+ from googletrans import Translator
5
+ from PIL import Image, ImageDraw, ImageFont
6
+ import numpy as np
7
+
8
+ # Step 1: Load the image
9
+ def load_image(image_file):
10
+ # Read the image using OpenCV
11
+ img = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), cv2.IMREAD_COLOR)
12
+ return img
13
+
14
+ # Step 2: Extract text and coordinates using pytesseract
15
+ def extract_text_with_position(image):
16
+ # Convert the image to grayscale for better OCR performance
17
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
18
+
19
+ # Use pytesseract to get OCR results along with bounding boxes
20
+ details = pytesseract.image_to_data(gray_image, output_type=pytesseract.Output.DICT)
21
+
22
+ text_data = []
23
+ for i in range(len(details['text'])):
24
+ if details['text'][i].strip() != '':
25
+ text_data.append({
26
+ 'text': details['text'][i],
27
+ 'left': details['left'][i],
28
+ 'top': details['top'][i],
29
+ 'width': details['width'][i],
30
+ 'height': details['height'][i]
31
+ })
32
+
33
+ return text_data
34
+
35
+ # Step 3: Translate the extracted text using Google Translate
36
+ def translate_text(text, target_language='en'):
37
+ translator = Translator()
38
+ translation = translator.translate(text, dest=target_language)
39
+ return translation.text
40
+
41
+ # Step 4: Recreate the image with translated text
42
+ def recreate_image_with_translated_text(original_img, text_data, target_language):
43
+ # Convert OpenCV image to Pillow image for easier manipulation
44
+ pil_img = Image.fromarray(cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB))
45
+ draw = ImageDraw.Draw(pil_img)
46
+
47
+ try:
48
+ # Load a default font (you can use a specific font if you want)
49
+ font = ImageFont.load_default()
50
+ except IOError:
51
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30)
52
+
53
+ for item in text_data:
54
+ # Translate each piece of text
55
+ translated_text = translate_text(item['text'], target_language)
56
+
57
+ # Draw the translated text on the new image at the same position
58
+ draw.text((item['left'], item['top']), translated_text, font=font, fill="black")
59
+
60
+ # Convert the image back to an OpenCV format
61
+ output_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
62
+ return output_img
63
+
64
+ # Step 5: Display or download the translated image
65
+ def display_or_download_image(output_img):
66
+ # Convert image to bytes to allow for download
67
+ _, img_bytes = cv2.imencode('.png', output_img)
68
+ st.image(output_img, channels="BGR", caption="Translated Image", use_column_width=True)
69
+
70
+ # Provide a button to download the image
71
+ st.download_button(
72
+ label="Download Translated Image",
73
+ data=img_bytes.tobytes(),
74
+ file_name="translated_image.png",
75
+ mime="image/png"
76
+ )
77
+
78
+ # Streamlit Interface
79
+ def main():
80
+ st.title("Image Translation App")
81
+
82
+ # Upload image
83
+ image_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
84
+
85
+ if image_file is not None:
86
+ # Load the image and show it
87
+ img = load_image(image_file)
88
+ st.image(img, caption="Uploaded Image", channels="BGR", use_column_width=True)
89
+
90
+ # Get the target language from the user
91
+ target_language = st.text_input("Enter the target language code (e.g., 'es' for Spanish):")
92
+
93
+ if target_language:
94
+ # Extract text from image
95
+ text_data = extract_text_with_position(img)
96
+
97
+ # Recreate the image with translated text
98
+ translated_img = recreate_image_with_translated_text(img, text_data, target_language)
99
+
100
+ # Display or allow download of the translated image
101
+ display_or_download_image(translated_img)
102
 
103
+ if __name__ == "__main__":
104
+ main()