def update_info(): content = request.json try: eid=content['eid'] except KeyError: return json.dumps({ 'status': 'error', 'msg':'Request with wrong arguments' }) try: e=Event.query.filter(Event.id==eid).one() except NoResultFound: return json.dumps({ 'status': 'error', 'msg':'Event not found' }) datetime_=datetime.datetime.now() analysis=None read=None for i in e.infos: if i.type.name=="analysis": analysis=i elif i.type.name=="read": read=i if content['type']=="analysis": if not analysis: analysis_info=InfoSourceType.query.filter(InfoSourceType.name=="analysis").one() analysis = InfoSource( datetime=datetime_, type = analysis_info ) ana=golfred.cs_img2analize(e.filename) filename_pref=e.filename.rsplit('.',1) filename_fred=filename_pref[0]+".analysis.fred" fred=golfred.text2fred(ana['description']['captions'][0]['text'],filename_fred) ana['fred']=fred analysis.json=json.dumps(ana) analysis.updated_at=datetime_ db.session.add(analysis) db.session.commit() elif content['type']=="read": if not read: read_info=InfoSourceType.query.filter(InfoSourceType.name=="read").one() read = InfoSource( datetime=datetime_, type = read_info ) if analysis: cat=golfred.getCategory(json.loads(analysis.json)) regions,text=golfred.cs_img2text(e.filename,type=cat) else: regions,text=golfred.cs_img2text(e.filename,type="text_") filename_pref=e.filename.rsplit('.',1) filename_fred=filename_pref[0]+".read.fred" fred=golfred.text2fred(text,filename_fred) read.updated_at=datetime_ read.json=json.dumps( { 'regions':regions, 'text':text, 'fred':fred } ) db.session.add(read) db.session.commit() return json.dumps({ 'status': 'ok' })
def add_events(uuid): visual_event=EventType.query.filter(EventType.name=="visual").one() try: exp=Experience.query.filter(Experience.uuid==uuid).one() except NoResultFound: return json.dumps({ 'status': 'error', 'msg':'Experience not found' }) files = request.files.getlist('files') print(files) datetime_=datetime.datetime.now() for f in files: ana=None text=None infos=[] if f and allowed_file(f.filename): filename_ = secure_filename(f.filename) if not os.path.isdir(os.path.join('memories',uuid)): os.mkdir(os.path.join('memories',uuid)) filename=os.path.join("memories", uuid,filename_) f.save(filename) if request.args.get('analize', '')=="true": analisys_info=InfoSourceType.query.filter(InfoSourceType.name=="analysis").one() ana=golfred.cs_img2analize(filename) if request.args.get('fred', '')=="true": filename_pref=filename.rsplit('.',1) filename_fred=filename_pref[0]+"analysis.fred" fred=golfred.text2fred(ana['description']['captions'][0],filename_fred) ana['fred']=fred pt = InfoSource( json=json.dumps(ana), type = analisys_info, updated_at=datetime_, datetime=datetime_ ) infos.append(pt) if request.args.get('read', '')=="true": read_info=InfoSourceType.query.filter(InfoSourceType.name=="read").one() if ana: cat=golfred.getCategory(ana) regions,text=golfred.cs_img2text(filename,type=cat) else: regions,text=golfred.cs_img2text(filename,type="text_") if len(text)>0: pt = InfoSource( json=json.dumps( { 'regions':regions, 'text':text } ), updated_at=datetime_, datetime=datetime_, type = read_info ) infos.append(pt) total_events=Event.query.filter(Event.experience_id==exp.id).count() mem=Event( filename=filename, created_at=datetime.datetime.now(), datetime=datetime.datetime.now(), type=visual_event, experience_id=exp.id, infos=infos, order=total_events ) db.session.add(mem) db.session.commit() return json.dumps({ 'status': 'ok' })