def test_init(self): # Dimensions need rounding picture = Picture(user_id=4, width=10.2, height=5.7) pic = ar.Pic(picture=picture, margin=1) self.assertEqual(pic.w, 12) self.assertEqual(pic.h, 7) self.assertIsNone(pic.x1) self.assertIsNone(pic.x2) self.assertIsNone(pic.y1) self.assertIsNone(pic.y2) # Only one dimension will be rounded picture = Picture(user_id=4, width=10, height=5.7) pic = ar.Pic(picture=picture, margin=1) self.assertEqual(pic.w, 11) self.assertEqual(pic.h, 7) self.assertIsNone(pic.x1) self.assertIsNone(pic.x2) self.assertIsNone(pic.y1) self.assertIsNone(pic.y2) # No margin picture = Picture(user_id=4, width=10, height=5.7) pic = ar.Pic(picture=picture, margin=0) self.assertEqual(pic.w, 10) self.assertEqual(pic.h, 6) self.assertIsNone(pic.x1) self.assertIsNone(pic.x2) self.assertIsNone(pic.y1) self.assertIsNone(pic.y2)
def _render_template(self): # """Render the main page template.""" pictures_query = Picture.query(ancestor=Picture.picture_key(str(self.userid))).order(-Picture.date) pictures = pictures_query.fetch(3) template_values = {'userId': self.userid, 'pictures': pictures} template = jinja_environment.get_template('templates/index.html') self.response.out.write(template.render(template_values))
def query(self, picture: Picture) -> List[Picture]: mask, rec = detect_text(picture.get_image()) kp, des = self.sift.detectAndCompute(picture.get_image(), mask) return (seq( self.db).map(lambda p: (p[0], self.bf.knnMatch(p[2], des, k=2))). map(lambda p: (p[0], self._ratio_test(p[1]))).map(lambda p: (p[ 0], len(p[1]))).filter(lambda p: p[1] > 0).sorted( lambda p: p[1], reverse=True).map(lambda p: p[0]).take(10).to_list())
def query(self, picture: Picture) -> List[Picture]: mask, rec = detect_text(picture.get_image()) kp, des = self.surf.detectAndCompute(picture.get_image(), mask) return ( seq(self.db).map(lambda p: (p[0], self.bf.match(p[2], des))). map(lambda p: (p[0], seq(p[1]).filter(lambda d: d.distance < max( THRESHOLD, seq(p[1]).map(lambda m: m.distance).min())).to_list())). map(lambda p: (p[0], len(p[1]))).filter(lambda p: p[1] > 4).sorted( lambda p: p[1], reverse=True).map(lambda p: p[0]).take(10).to_list())
def query(self, picture: Picture) -> List[Picture]: mask, rec = detect_text(picture.get_image()) kp, des = self.sift.detectAndCompute(picture.get_image(), mask) FLANN_INDEX_KDTREE = 1 flann_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) flann = cv2.FlannBasedMatcher(flann_params, {}) return (seq(self.db).map( lambda p: (p[0], self.flann.knnMatch(np.asarray(p[2], np.float32), np.asarray(des, np.float32), 2)) ).map(lambda p: (p[0], self._ratio_test(p[1]))).map( lambda p: (p[0], len(p[1]))).filter(lambda p: p[1] > 4).sorted( lambda p: p[1], reverse=True).map(lambda p: p[0]).take(10).to_list())
def query(self, picture: Picture) -> List[Picture]: mask,rec=detect_text(picture.get_image()) kp, des = self.orb.detectAndCompute(picture.get_image(), mask) return ( seq(self.db) .map(lambda p: (p[0], p[1], self.bf.knnMatch(des, p[2], k=2))) .map(lambda p: (p[0], p[1], self._ratio_test(p[2]))) .filter(lambda p: len(p[2]) > MIN_MATCH_COUNT) .map(lambda p: (p[0], self._homography(kp, p[1], p[2]))) .filter(lambda p: len(p[1]) > MIN_MATCH_COUNT) .sorted(lambda p: p[1], reverse=True) .map(lambda p: p[0]) .take(10) .to_list() )
def add_picture(name, description, picture_link): product_object = Picture(name=name, description=description, picture_link=picture_link) session.add(product_object) session.commit()
def get(self): '''no arguments''' today = datetime.today() query = Picture.all().filter('uploaded >', datetime(today.year, today.month, today.day, 0, 0) ) if query.count(): logging.debug('New pictures for newsletter: %s' % query.count()) pictures_today = list(query) query = Subscription.all().filter('active =', True) if query.count(): logging.info('Sending newsletter to %s recipients' % query.count()) recipients = list(query) today = datetime.today() message = mail.EmailMessage() message.sender = '%s (Photoblog) <%s>' % ( conf.mail_from_name, conf.mail_from_address ) message.to = conf.mail_from_address message.bcc = [r.email for r in recipients] message.subject = conf.mail_newsletter_subject message.body = render('newsletter.txt', {'pics':pictures_today}) message.send()
def get(self): '''no arguments''' random_pic = Picture.gql( 'WHERE rand > :1 ORDER BY rand LIMIT 1', random() ).get() logging.debug('Random Picture: %s' % random_pic) self.redirect('/photo/%s' % random_pic.gphoto_id)
def query(self, picture: Picture) -> List[Picture]: mask, rec = detect_text(picture.get_image()) kp, des = self.orb.detectAndCompute(picture.get_image(), mask) return ( seq(self.db) .map( lambda p: (p[0], self.flann.knnMatch(np.asarray(p[2], np.float32), np.asarray(des, np.float32), 2))) .map(lambda p: (p[0], self._ratio_test(p[1]))) .map(lambda p: (p[0], len(p[1]))) .filter(lambda p: p[1] > 4) .sorted(lambda p: p[1], reverse=True) .map(lambda p: p[0]) .take(10) .to_list() )
def query(self, picture: Picture) -> List[Picture]: mask, rec = detect_text(picture.get_image()) kp, des = self.orb.detectAndCompute(picture.get_image(), mask) return (seq( self.db).map(lambda p: (p[0], p[1], self.bf.match(des, p[2]))).map( lambda p: (p[0], p[1], seq(p[2]).filter(lambda d: d.distance < max( THRESHOLD, seq(p[2]).map(lambda m: m.distance).min())).to_list())). filter(lambda p: len(p[2]) > MIN_MATCH_COUNT).map( lambda p: (p[0], self._homography(kp, p[1], p[2], p[0].get_image( ), picture.get_image()))). filter(lambda p: (p[1]) > MIN_MATCH_COUNT).sorted( lambda p: p[1], reverse=True).map(lambda p: p[0]).take(10).to_list())
def get(self): action = self.param('action') values = {'action':action} if(action == 'edit'): key = self.param('id') pic = Picture.get_by_id(int(key)) values.update({'pic':pic}) self.generateBasePage('manage/pic.html', values)
def process(owner, content): im_data = io.BytesIO(content) im_copy = io.BytesIO(content) im = Image.open(im_data) cf = imaging.ColorFinder(im) top = imaging.ColorUtil.generate_color_panes(tuple(cf.strategy_enhanced_complements())) output = StringIO.StringIO() top.save(output, format="JPEG") palette = io.BytesIO(output.getvalue()) palette_copy = io.BytesIO(output.getvalue()) output.close() upload = Picture(parent=Picture.picture_key(owner)) upload.owner = owner file_name = files.blobstore.create(mime_type='image/jpeg') with files.open(file_name, 'a') as f: f.write(im_copy.read()) files.finalize(file_name) picture_blob_key = files.blobstore.get_blob_key(file_name) upload.picture = str(picture_blob_key) palette_name = files.blobstore.create(mime_type='image/jpeg') with files.open(palette_name, 'a') as f: f.write(palette_copy.read()) files.finalize(palette_name) palette_blob_key = files.blobstore.get_blob_key(palette_name) upload.palette = str(palette_blob_key) upload.put() return palette
def get(self): """no arguments""" rss = memcache.get("feed") if rss is None: pics = Picture.gql("ORDER BY uploaded DESC LIMIT 11") host = self.request.host_url rss = render("feed.xml", {"pics": pics, "host": host}) memcache.set("feed", rss, time=900) self.response.out.write(rss)
def create_picture(collection_id, url): """Create and return a new picture.""" new_picture = Picture(collection_id=collection_id, url=url) db.session.add(new_picture) db.session.commit() return new_picture
def post(self): try: checkList = self.request.get_all('checks') for key in checkList: keyID = int(key) pic = Picture.get_by_id(keyID) pic.delete() finally: self.redirect('/picture') return
def load_pictures(): for _ in range(0, 30): new_feeling = random.choice(feels) print new_feeling # photos = fApi.get_photos(new_feeling) photos = get_giphy(feels) pic = Picture(giphy_url=photos) db.session.add(pic) db.session.commit()
def get(self): '''no arguments''' page = memcache.get('mosaic') if page is None: pics = Picture.gql('ORDER BY uploaded ASC') page = render('list.html', {'pics':pics}) if not memcache.add('mosaic', page): logging.error('Mosaic not written to memcache') else: logging.debug('New memcache entry: mosaic') logging.debug('All pictures listed') self.response.out.write(page)
def make_listing_profile(): """add listing info to database""" roommates = request.form.getlist("roommates") laundry = True if request.form.get("laundry") == "True" else False pets = True if request.form.get("pets") == "True" else False living_there = True if request.form.get( "living_there") == "True" else False listing_id = request.form.get("listing_name") user_id = session["current_user"] main_photo = functions.save_photo("main_photo") photos = [] photos.append(functions.save_photo("photo_1")) photos.append(functions.save_photo("photo_2")) photos.append(functions.save_photo("photo_3")) kwargs = dict(listing_id=listing_id, address=request.form.get("address"), neighborhood=request.form.get("neighborhood"), price=int(request.form.get("price")), avail_as_of=request.form.get("avail_date"), length_of_rental=request.form.get("duration"), main_photo=main_photo, bedrooms=request.form.get("bedrooms"), bathrooms=request.form.get("bathrooms"), laundry=laundry, pets=pets, description=request.form.get("description"), primary_lister=user_id) if Listing.query.get(listing_id): flash("Listing Name Taken") return redirect("/listings/{}".format(listing_id)) else: for key in kwargs.keys(): if kwargs[key] == "": del kwargs[key] db.session.add(Listing(**kwargs)) db.session.commit() if living_there: functions.add_UserListing(user_id, listing_id) for roommate in roommates: functions.add_UserListing(roommate, listing_id) for photo in photos: db.session.add(Picture(listing_id=listing_id, photo=photo)) db.session.commit() return redirect("/listings/{}".format(listing_id))
def _get_newer_or_older(pic, newer=True): '''query the database for adjacent pictures @param pic: center picture @param newer: True for LHS, False for RHS ''' if newer: options = ('>', 'ASC') else: options = ('<', 'DESC') query = 'WHERE uploaded %s :1 ORDER BY uploaded %s LIMIT 1' % options picture = Picture.gql(query, pic.uploaded) return picture.get()
def get(self, gphoto_id=None): '''display the picture @param gphoto_id: the numeric ID from picasa ''' if not gphoto_id: query = Picture.gql('ORDER BY uploaded DESC LIMIT 1') latest_picture = query.get() gphoto_id = str(latest_picture.gphoto_id) page = memcache.get(gphoto_id, namespace='pics') if page is None: pics = Picture.all().filter('gphoto_id =', int(gphoto_id)) pic = pics.get() logging.debug('Picture view: %s' % pic.gphoto_id) page = render('pic.html', {'pic':pic, 'next':get_newer(pic), 'prev':get_older(pic) }) if not memcache.add(gphoto_id, page, 86400, namespace='pics'): logging.error('Page %s not written to memcache' % gphoto_id) else: logging.debug('New memcache entry: %s' % gphoto_id) self.response.out.write(page)
def attempt_upload(): """Returns true after uploading user photo to server then to cloud. Otherwise returns false. """ lazy_load_of_upload_imports() filename_provided = pictures.save(request.files['picture']) width = to_float_from_input(request.form.get('width')) height = to_float_from_input(request.form.get('height')) name = to_clean_string_from_input(request.form.get('name'), 100) # TODO: Validate hight/width roughly match image, are positive and nonzero user_id = session.get('user_id', None) if filename_provided and width and height and user_id: picture = Picture(user_id=user_id, width=width, height=height) if name: picture.picture_name = name db.session.add(picture) db.session.flush() # Rename file after adding so that the picture_id can be used, # this may not really be neccesary to include in the file name. filename = rename_picture_on_server(filename_provided, picture.picture_id) url = move_picture_to_cloud(filename) picture.image_file = url db.session.commit() return True else: return False
def get(self): type = self.param('type') key = self.param('img_id') keyID = int(key) pic = Picture.get_by_id(keyID) if(pic): if(type == 'thumb'): content = pic.thumbnail else: content = pic.picContent self.response.headers['Content-Type'] = "image/png" self.response.out.write(content) else: self.response.out.write("No image") return
def query(self, picture: Picture) -> List[Picture]: hist = self._get_histogram(picture.get_image()) return (seq(self.db) # Calculate histogram similarity .map(lambda entry: (entry[0], self._compare_histograms_full(hist, entry[1]))) # Calculate distance to center .map(lambda entry: (entry[0], self._euclidean_distance_to_origin(entry[1]))) # Order by distance .sorted( lambda entry_res: entry_res[1], False if self.histogram_comparison_method == cv2.HISTCMP_HELLINGER else True).map(lambda entry: entry[0]) # Take first K .take(K).to_list())
def load_pictures(): """Load pictures from seed data into database""" print "Pictures" with open("seed_data/pictures.txt") as pictures: for picture in pictures: picture = picture.rstrip() picture_id, listing_id, photo = picture.split("|") picture = Picture(picture_id=picture_id, listing_id=listing_id, photo=photo) db.session.add(picture) db.session.commit()
def upload_image(): # if "file" not in request.files: # return make_response(jsonify({ # "msg": "Coudn't upload the file" # }), 400) images = request.files permission = request.form imageList = [] # keyList = sorted(images.keys()) print(permission) # print(images['is_private0']) i = 0 for key in images.keys(): file = images[key] hardcoded_privacy_key = "is_private" + str(i) hardcoded_caption_key = "caption" + str(i) is_private = permission[hardcoded_privacy_key] caption = permission[hardcoded_caption_key] print(is_private + " " + caption) if file.filename == '': return make_response(jsonify({ "msg": 'File name not found' }), 400) name = file.filename if allowed_file(file.filename): now = datetime.now() timestamp = str(datetime.timestamp(now)) filename = file.filename.rsplit('.', 1) name = filename[0] + "-" + timestamp + '.' + filename[1] file.save(os.path.join(UPLOAD_FOLDER, name)) path = 'http://127.0.0.1:5000' + url_for('static', filename=name) id = get_user_id(request.headers.get('Authorization')) if is_private == 'true': is_private = True else: is_private = False picture = Picture(id, path, is_private=is_private, picture_caption=caption) db.session.add(picture) db.session.commit() imageList.append(('http://127.0.0.1:5000' + url_for('static', filename=name))) i += 1 return make_response(jsonify({ 'imageList': imageList }), 200)
def load_pictures(seed_file_path): """Add sample pictures to database from text file. Data file is pipe seperated: picture_id | user_id | width | height | image_file | picture_name | image_attribution | public """ seed_image_folder_path = "static/img_samples/" with open(seed_file_path) as seed_file: for line in seed_file: tokens = line.rstrip().split("|") picture_id = int(tokens[0]) user_id = int(tokens[1]) width = float(tokens[2]) height = float(tokens[3]) image_raw = tokens[4].strip() image_file = (seed_image_folder_path + image_raw) if image_raw else None picture_name = tokens[5].strip() image_attribution = tokens[6].strip() public = tokens[7].strip().lower() == 'public' picture = Picture( picture_id=picture_id, user_id=user_id, width=width, height=height, image_file=image_file, picture_name=picture_name, image_attribution=image_attribution, public=public, ) db.session.add(picture) db.session.commit() # Reset seq/counter result = db.session.query(func.max(Picture.picture_id)).one() max_id = int(result[0]) query = "ALTER SEQUENCE pictures_picture_id_seq RESTART WITH :next_id" db.session.execute(query, {'next_id': max_id + 1}) db.session.commit()
def upload_photos(event_id): """Upload photos to particular event.""" # gets a file class object image = request.files['image'] # get actual file name filename = secure_filename(image.filename) # save file into my folder image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) new_photo = Picture(filename=filename, uploader_id=session['user_id'], event_id=event_id) db.session.add(new_photo) db.session.commit() return redirect('/event-page/{event_id}'.format(event_id=event_id))
def query(dataset_dir: str, query_dir: str, methods: List[AbstractMethod]): data = Data(dataset_dir) file_names = fnmatch.filter(os.listdir(query_dir), '*.jpg') print('Training...') texts_recs = [method.train(data.pictures) for method in methods] print('Querying...') query_pictures = seq(file_names).map(lambda query_name: Picture(query_dir, query_name)).to_list() results = [] for method in methods: print('\tRunning method', method.__class__.__name__) mres = [] for picture in tqdm(query_pictures, file=sys.stdout): mres.append((picture,) + get_result(method, picture)) results.append(mres) return results, texts_recs
def save_results(): """Saving the img and the text from results""" # Get form variables keyword = request.form["keywords"] text_results = request.form["twitter"] giphy_url = request.form["giphy"] block = request.form["b_text"] sentiment = request.form["sentiment"] lat = request.form["lat"] lng = request.form["long"] #Saving current user info into the db user_id = session.get("user_id") #Saving tweet data to db save_twit = Tweet(tweet_text=text_results) db.session.add(save_twit) db.session.commit() #Saving giphy data to db save_gif = Picture(giphy_url=giphy_url) db.session.add(save_gif) db.session.commit() new_keyword = Result(keywords=keyword, tweet_id=save_twit.tweet_id, giphy_id=save_gif.giphy_id, block_text=block, sentiment=sentiment, generated_at=datetime.datetime.now(), user_id=user_id, lat=lat, lng=lng) db.session.add(new_keyword) db.session.commit() return redirect("/users/%s" % user_id)
def query(self, picture: Picture, frame: Frame = None) -> List[Picture]: im = picture.get_image() if frame and frame.is_valid(): side = int(math.sqrt(frame.get_area()) * 0.8) m = frame.get_perspective_matrix( np.array([[0, side - 1], [side - 1, side - 1], [side - 1, 0], [0, 0]])) im = cv2.warpPerspective(im, m, (side, side)) # plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)) # plt.show() kp, des = self.orb.detectAndCompute(im, None) return ( seq(self.db).map(lambda p: (p[0], self.bf.match(p[2], des))). map(lambda p: (p[0], seq(p[1]).filter(lambda d: d.distance < max( THRESHOLD, seq(p[1]).map(lambda m: m.distance).min())).to_list())). map(lambda p: (p[0], len(p[1]))).filter(lambda p: p[1] > 4).sorted( lambda p: p[1], reverse=True).map(lambda p: p[0]).take(10).to_list())
def query(self, picture: Picture) -> (List[Picture], Frame): frame = get_frame_with_lines(picture.get_image()) return self.orb.query(picture), frame
def get(self): pics = Picture.all() values = {'pics':pics} self.generateBasePage('manage/pics.html', values) return
def __init__(self, directory: str): self.pictures = [] file_names = fnmatch.filter(os.listdir(directory), '*.jpg') for file_name in file_names: self.pictures.append(Picture(directory, file_name))
async def handle_incoming(request): form = await request.post() message_sid = form['MessageSid'] from_number = form['From'] num_media = int(form.get('NumMedia', 0)) message = form.get('Body', None) session = request.app['session']() user = User.get_or_create_user(session, from_number) def log(message='', event=None, **kwargs): extra = { 'user_id': user.id, 'tc_status': user.tc_status, 'message_sid': message_sid } if event is not None: extra[event] = True extra.update(kwargs) logger.info(message, extra=extra) log('Received message', event='received_message') if num_media > 0: media_files = [(form.get("MediaUrl{}".format(i), ''), form.get("MediaContentType{}".format(i), '')) for i in range(0, num_media)] for media_url, _ in media_files: # TODO upload pictures to google cloud storage and delete from twilio servers picture = Picture(source_url=media_url, create_time=datetime.now(), message_sid=message_sid) print(media_url) session.add(picture) user.add_pending(picture) session.commit() worker_loop = request.app['worker']['loop'] response = None if user.tc_status is TCStatus.NOTHING: log('New user, sending terms and conditions', 'send_tc') response = config.TC_AGREEMENT_TEXT user.tc_status = TCStatus.SENT elif user.tc_status is TCStatus.SENT: if message is not None: if message.upper() == 'YES': log('Agreed to terms and conditions', 'tc_agreed') user.tc_status = TCStatus.AGREED response = 'Thank you for agreeing to the terms and conditions.' if len(user.pending) > 0: response += " Processing your {} picture(s) now".format( len(user.pending)) asyncio.run_coroutine_threadsafe( process_pictures(session, user.pending), worker_loop) # await process_pictures(session, user.pending) else: response += " You can now send me pictures to convert" else: log('Remind to agree to terms and conditions', 'tc_remind') response = 'You must agree to the terms and conditions by responding YES before continuing.' if len(user.pending) > 0: response += " You have {} pictures pending".format( len(user.pending)) response = ' '.join(response) else: assert user.tc_status is TCStatus.AGREED if num_media > 0: log('Received new pictures', 'more_pictures', num_new_pictures=num_media) response = "Got it! Hard at work processing {} more /picture(s) for you!".format( num_media) asyncio.run_coroutine_threadsafe( process_pictures(session, user.pending), worker_loop) # await process_pictures(session, user.pending) session.commit() if not response: response = "Sorry, I didn't understand that!" mesg_resp = MessagingResponse() mesg_resp.message(response) return Response(text=str(mesg_resp), content_type='text/xml')
def post(self): action = self.param('action') picName = self.request.get('picName') if(action == 'add'): picContent = self.request.get('pic') pic = Picture() pic.picName = picName if(picContent != None): pic.picContent = db.Blob(picContent) pic.thumbnail = db.Blob(images.resize(picContent,128,128)) pic.put() pic.picLink = '/picture/show?img_id=%s&type=full'%pic.key().id() pic.put() elif(action == 'edit'): key = self.param('id') pic = Picture.get_by_id(int(key)) pic.picName = picName pic.put() self.redirect('/picture')