示例#1
0
def captionImage(url):
    image_url = url
    cBot = CaptionBot()
    caption = cBot.url_caption(image_url)
    caption = caption.split(" ")[2:]
    caption = " ".join(caption)
    return caption
class ImageDescriber:
    def __init__(self):
        self.captionBot = CaptionBot()  #www.captionbot.ai
        self.translator = Translator()
        self.dest = 'ru'
        self.description = 'send_photo - Я могу рассказать, что изображено на картинке \n'

    def describeImg(self, bot, update):
        imgId = update.message.photo[-1]
        imgPath = bot.getFile(imgId)
        imgPath = imgPath['file_path']
        caption = self.getImageCaption(imgPath)
        bot.send_message(chat_id=update.message.chat_id, text=caption)

    def getImageCaption(self, imgPath):
        caption = self.captionBot.url_caption(imgPath)
        caption = self.translator.translate(caption, dest=self.dest).text
        return caption
示例#3
0
def getDescription():
    if request.method == "POST":
        img = request.data[5:]
        data = img
        c = CaptionBot()
        caption = c.url_caption(str(data))
        r = {"success":caption}
        lines = getText(str(data))
        text = ""
        if lines:
            text = parseText(lines)
            if text:
                response = {"success":text}
                return json.dumps(response)
            aux = "this image has the following text: " + text
            response = {"success" : aux}
            return json.dumps(response)
        try:
            conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
            body = {"url":data}
            conn.request("POST", "/face/v1.0/detect?%s" % params, json.dumps(body), headers)
            response = conn.getresponse()
            data = response.read()
            data = json.loads(data)
            if(data):
                age = data[0]["faceAttributes"]["age"]
                gender = data[0]["faceAttributes"]["gender"]
                moustache = data[0]["faceAttributes"]["facialHair"]["moustache"]
                beard = data[0]["faceAttributes"]["facialHair"]["beard"]
                glasses = data[0]["faceAttributes"]["glasses"]
                description = " It's gender is " + gender + " with an age of " + str(age) + " years. It has a "+str(100*float(moustache)) +" percent probability of having a moustache and "+str(100*float(beard))+" percent probability of having a beard. He has "+glasses
                r["success"] += description 
            conn.close()
        except Exception as e:
            print(e)
        return json.dumps(r)
	#--------- Clean Instagram Caption ---------#
	# if there is no caption skip it
	if len(caption) < 1: continue
	# currently emojis look like (apparently called mojibake): 😘
	# should turn emojis into actual emojis
	#caption.encode('cp1252').decode('utf-8')
	# now try converting emojis to aliases
	try:
		caption = emoji.demojize(caption)
	except e:
		print >> sys.stderr, 'Error converting caption to alias for image {}'.format(img_url)
		# if you can't do it, it's not a big deal, leave the funny looking characters in there
	
	# try getting description from captionbot
	try:
		desc = c.url_caption(img_url)
	except e:
		print >> sys.stderr, 'Error getting captionbot desc for image {}: {}'.format(img_url, e)
		continue
	# try converting emojis to aliases
	try:
		desc = emoji.demojize(desc)
	except e:
		print >> sys.stderr, 'Error converting desc to alias for image {}'.format(img_url)
		continue
	#------------ Clean Description ------------#
	# insert spaces between emoji aliases 
	desc = desc.replace('::', ': :')
	# remove commas from captionbot description
	desc = desc.replace(',', '')
	# remove newlines and return chars
示例#5
0
 def captionimage(url):
     c = CaptionBot()
     return c.url_caption(url)