-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweetOutput.py
181 lines (137 loc) · 5.01 KB
/
TweetOutput.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
global userImage, userTranslateLanguage, transLang
transLang = ""
# Imports the Google Cloud client library
from google.cloud import translate
#Imports necessary libraries for google vision API
import io
import os
from google.cloud import vision
from google.cloud.vision import types
import tweepy
from time import sleep
import nltk
from PIL import Image
import requests
from io import BytesIO
auth = tweepy.OAuthHandler(YOUR_KEY, YOUR_SECRET)
auth.set_access_token(YOUR_TOKEN, YOUR_TOKEN_SECRET)
api = tweepy.API(auth)
userImage = input("Enter an image url: ")
userTranslateLanguage = input("Enter a number that corresponds to the language you'd like the output to be translated to - [1] Spanish, [2] Japanese, [3] Arabic, [4] Russian, [5] German, [6] English: ")
if userTranslateLanguage == "1":
userTranslateLanguage = "es-US"
elif userTranslateLanguage == "2":
userTranslateLanguage = "ja-JP"
elif userTranslateLanguage == "3":
userTranslateLanguage = "ar-AE"
elif userTranslateLanguage == "4":
userTranslateLanguage = "ru-RU"
elif userTranslateLanguage == "5":
userTranslateLanguage = "de-DE"
transLang = userTranslateLanguage[0:2]
response = requests.get(userImage)
img = Image.open(BytesIO(response.content))
img.save("temp.jpg", "JPEG")
myFile = open('Twitter_Sayings.txt', 'w', encoding="utf-8")
def translateStuff(targetLanguage, text):
global userTranslateLanguage
translate_client = translate.Client()
translation = translate_client.translate(text, target_language = targetLanguage)
translatedText = translation["translatedText"]
myFile.write(" (Translation: " + translatedText + ")")
def labelsUrl(uri):
"""Detects labels in the file located in Google Cloud Storage or on the
Web."""
counter = 0
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
myFile.write("\n" + label.description)
translateStuff(transLang, label.description)
counter += 1
if counter == 3:
break
def searchFace(uri):
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = uri
response = client.face_detection(image=image)
faces = response.face_annotations
# Names of likelihood from google.cloud.vision.enums
chances = ('UNKNOWN', 'VERY UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY LIKELY')
print('Faces:')
for face in faces:
myFile.write("\n" + 'anger: {}'.format(chances[face.anger_likelihood]))
myFile.write("\n" + 'joy: {}'.format(chances[face.joy_likelihood]))
myFile.write("\n" + 'sorrow: {}'.format(chances[face.sorrow_likelihood]))
break
def searchLandmark(uri):
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = uri
response = client.landmark_detection(image=image)
landmarks = response.landmark_annotations
for landmark in landmarks:
myFile.write("\nThe landmark is: " + landmark.description)
break
def searchLogos(uri):
# Instantiates a client
translate_client = translate.Client()
# Instantiates a client
vision_client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = uri
response = vision_client.logo_detection(image=image)
logos = response.logo_annotations
print('Logos:')
for logo in logos:
myFile.write("\n" + logo.description)
translateStuff(transLang, logo.description)
break
def searchText(uri):
# Instantiates a client
translate_client = translate.Client()
# Instantiates a client
vision_client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = uri
# Performs text detection on the image file
response = vision_client.text_detection(image=image)
texts = response.text_annotations
print('Text:')
for text in texts:
myFile.write("\n" + text.description)
translateStuff(transLang, text.description)
break
def runProgram(runLabel = "1", runFace = "0", runLandmark = "0", runLogos = "0", runText = "0"):
if runLabel == "1":
labelsUrl(userImage)
if runFace == "1":
searchFace(userImage)
if runLandmark == "1":
searchLandmark(userImage)
if runLogos == "1":
searchLogos(userImage)
if runText == "1":
searchText(userImage)
runProgram(runLabel="1", runLandmark="0", runLogos="0", runText="0", runFace="0")
myFile.close()
myFile2 = open('Twitter_Sayings.txt', 'r', encoding="utf-8")
newline = "\n"
fileLines = myFile2.readlines()
newLines = []
for line in fileLines:
print(line)
line1 = line.replace("\n", "")
if line1 != "":
newLines.append(line1)
print(newLines)
def tweetStuff(newLines):
api.update_with_media('temp.jpg', newLines)
tweetStuff(newLines)
myFile2.close()