class Captioner: def __init__(self): self.caption_bot = CaptionBot() self.prev_caption = '' def caption_img(self, frame, get_prev=False): if not get_prev: img_name = 'to_caption.jpg' cv2.imwrite(img_name, frame) caption = self.caption_bot.file_caption(img_name) self.prev_caption = caption return self.prev_caption
def describe(): msgtype = request.form['type'] ##question=request.form['question'] print(str(msgtype) + "is message ") imagefile = request.files['imagefile'] filename = secure_filename(imagefile.filename) imagefile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) print(filename) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) print(filepath) if (msgtype == '1'): c = CaptionBot() print("caption bot running") result = c.file_caption(filepath) print(result) return jsonify({ 'message': result, 'type': msgtype, 'filename': filename, 'filepath': filepath }) elif (msgtype == '2'): question = request.form['question'] ##establish connection ##if (question is None) return jsonify({'message':'error , didnt include question','type':'2'}) response = requests.get(url, headers=header) ##upload image url1 = 'http://vqa.daylen.com/api/upload_image' files = {'file': open(filepath, 'rb')} response = requests.post(url1, files=files, headers=header) data = json.loads(response.text) im_id = data['img_id'] print im_id ## upload question url2 = 'http://vqa.daylen.com/api/upload_question' #'fa10956c0743670ec5c924ce1523ab16' datas = {'img_id': im_id, 'question': question} response = requests.post(url2, data=datas, headers=header) ##get response print(response.text) data = json.loads(response.text) return jsonify({'message': data[u'answer'], 'type': '2'}) else: return jsonify({'message': 'error in type ', 'type': '1'})
def captioning(video_id, scenes): print('Captioning {}'.format(video_id)) start_time = datetime.now() c = CaptionBot() result = [] cached_path = 'cached/{}.data'.format(video_id) if os.path.isfile(cached_path): with open(cached_path, 'rb') as cached_file: result = pickle.load(cached_file) for x in result: print('At {}: {}'.format(int(x['start_time']), x['caption'])) else: for x in scenes: caption = c.file_caption(x[0]) print('At {}: {}'.format(int(x[1]), caption)) result.append({'start_time': x[1], 'caption': caption}) with open(cached_path, 'wb') as cached_file: pickle.dump(result, cached_file) print('Captioning: {} seconds'.format(datetime.now() - start_time)) return result
from captionbot import CaptionBot c = CaptionBot() #msg = c.url_caption("https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg") #msg2 = c.url_caption("https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Collection_of_military_aircraft.jpg/1200px-Collection_of_military_aircraft.jpg") msg2 = c.file_caption("/Users/khanh/Desktop/bbb.jpg") print(msg2)
import cv2 from captionbot import CaptionBot c = CaptionBot() cap = cv2.VideoCapture(0) while True: r, f = cap.read() if r == True: cv2.imshow("Stream", f) key = cv2.waitKey(1) if key == ord('a'): print("Generating Caption...") cv2.imwrite('image.jpg', f) caption = c.file_caption('/home/aditya/Hack-a-bit2019/' + 'image.jpg') print(caption) elif key == 27: break else: continue cv2.destroyAllWindows() cap.release()
json_files = 0 for f in os.listdir('data/frames/'): parsed = f.split(".") if parsed[1] == 'png': frame_files += 1 else: json_files += 1 index = json_files + 1 print "Total frames:", str(frame_files) print "Total JSONs:", str(json_files) print "To process now:", str(frame_files - json_files) print "Starting at frame:", str(index) for i in range(index, frame_files): #c.url_caption('your image url here') data = c.file_caption('data/frames/' + str(i) + '.png') print "Frame " + str(i) + " :: " + data json_object['data']['caption'] = data json_object['data']['filename'] = 'data/frames/' + str(i) + '.png' json_object['data']['film-frame'] = i os.system('touch data/frames/' + str(i) + ".json") with open("data/frames/" + str(i) + ".json", 'r+') as json_data: json.dump(json_object, json_data, indent=2) print "Finished!"