def get_styles(self): if self.styles: return self.styles style_path = os.path.join(self.file_path, "word/styles.xml") with open(style_path, encoding="UTF-8") as f: styles = f.read() styles = BeautifulSoup(styles, "xml") self.styles = Styles(styles) return self.styles
def _write_styles_file(self): # Write the style xml file. xf_formats = self.workbook.xf_formats palette = self.workbook.palette font_count = self.workbook.font_count num_format_count = self.workbook.num_format_count border_count = self.workbook.border_count fill_count = self.workbook.fill_count custom_colors = self.workbook.custom_colors dxf_formats = self.workbook.dxf_formats styles = Styles() styles._set_style_properties([ xf_formats, palette, font_count, num_format_count, border_count, fill_count, custom_colors, dxf_formats]) styles._set_xml_writer(self._filename('xl/styles.xml')) styles._assemble_xml_file()
def add_output_box(self): text = tk.Text(self, Styles('text')) text.grid(row=3, column=0, sticky='WS') return text
def add_search_term(self): label = tk.Label(self, Styles('label', {'text': 'Search Term'})) label.grid(row=0, column=2, sticky='W') entry_search_term = tk.Entry(self, Styles('entry')) entry_search_term.grid(row=1, column=2) return entry_search_term
def add_web_address(self): label = tk.Label(self, Styles('label', {'text': 'Web Address'})) label.grid(row=0, column=0, sticky='W') entry_web_address = tk.Entry(self, Styles('entry')) entry_web_address.grid(row=1, column=0) return entry_web_address
class Docx(IdAble): def __init__(self, path): super(Docx, self).__init__() if path is None or not isinstance(path, str): raise Exception("Path is not allowed None") if not os.path.exists(TEMP_BASE_DIR): try: os.mkdir(TEMP_BASE_DIR) except FileExistsError as e: pass self.document = None self.content_types = None self.relationships = None self.numbering = None self.styles = None self.base_dir = uuid1().hex file = ZipFile(path) self.file_path = os.path.join(TEMP_BASE_DIR, self.base_dir) os.mkdir(self.file_path) file.extractall(self.file_path) file.close() self.get_document() self.get_content_types() self.get_numbering() self.get_relationships() self.get_styles() def get_numbering(self): if self.numbering: return self.numbering numbering_path = os.path.join(self.file_path, "word/numbering.xml") if not os.path.exists(numbering_path): self.numbering = Numbering() return self.numbering with open(numbering_path, encoding="UTF-8") as f: numbering = f.read() numbering = BeautifulSoup(numbering, "xml") self.numbering = Numbering(numbering) return self.numbering def get_document(self): if self.document: return self.document doc_path = os.path.join(self.file_path, "word/document.xml") with open(doc_path, encoding="UTF-8") as f: document = f.read() document = BeautifulSoup(document, "xml") self.document = Document(document) return self.document def get_relationships(self): if self.relationships: return self.relationships doc_path = os.path.join(self.file_path, "word/_rels/document.xml.rels") with open(doc_path, encoding="UTF-8") as f: doc = f.read() doc = BeautifulSoup(doc, "xml") self.relationships = Relationships(doc) return self.relationships def get_content_types(self): if self.content_types: return self.content_types content_path = os.path.join(self.file_path, "[Content_Types].xml") with open(content_path, encoding="UTF-8") as f: content_types = f.read() content_types = BeautifulSoup(content_types, "xml") self.content_types = ContentTypes(content_types) return self.content_types def get_styles(self): if self.styles: return self.styles style_path = os.path.join(self.file_path, "word/styles.xml") with open(style_path, encoding="UTF-8") as f: styles = f.read() styles = BeautifulSoup(styles, "xml") self.styles = Styles(styles) return self.styles def extract_media_files(self, path): relationships = self.get_relationships() file_mapping = relationships.get_file_mapping() template = "cp {} {}" base_dir = os.path.join(self.file_path, "word") #print(file_mapping) for file in file_mapping.keys(): from_file = os.path.join(base_dir, file) to_file = os.path.join(path, file_mapping[file]) dir_name = os.path.dirname(to_file) if not os.path.exists(dir_name): os.makedirs(dir_name) extract = template.format(from_file, to_file) os.system(extract) def merge(self, doc, page=False): if not isinstance(doc, Docx): raise Exception("merge parameter is not docx") source_content_types = doc.get_content_types() self.get_content_types().merge_content_types(source_content_types) source_relationships = doc.get_relationships() #print(source_relationships.get_file_mapping()) source_relationships.generate_id(doc.id) doc.extract_media_files(os.path.join(self.file_path, "word")) self.get_relationships().merge_relationships(source_relationships) source_styles = doc.get_styles() source_styles.generate_id(doc.id) self.styles.merge(source_styles) source_numberings = doc.get_numbering() source_numberings.generate_id(doc.num) self.numbering.merge(source_numberings) source_document = doc.get_document() source_document.generate_id(doc.id, doc.num) self.get_document().merge(source_document, page) def save(self, name): import zipfile self._save_document() self._save_content_types() self._save_relationships() self._save_numbering() self._save_styles() file = ZipFile(name, "w", compression=zipfile.ZIP_DEFLATED) for base, children, files in os.walk(self.file_path): base_name = base.split(self.base_dir)[-1] for f in files: zip_path = os.path.join(base_name, f) real_path = os.path.join(base, f) file.write(real_path, zip_path) file.close() def _save_document(self): with open(os.path.join(self.file_path, "word/document.xml"), mode="w", encoding="UTF-8") as f: f.write(str(self.document.get_dom())) def _save_content_types(self): with open(os.path.join(self.file_path, "[Content_Types].xml"), mode="w", encoding="UTF-8") as f: f.write(str(self.content_types.get_dom())) def _save_relationships(self): with open(os.path.join(self.file_path, "word/_rels/document.xml.rels"), mode="w", encoding="UTF-8") as f: f.write(str(self.relationships.get_dom())) def _save_numbering(self): numbering = self.numbering.get_dom() if not numbering: return numbering_path = os.path.join(self.file_path, "word/numbering.xml") with open(numbering_path, "w+", encoding="UTF-8") as f: f.write(str(numbering)) def _save_styles(self): with open(os.path.join(self.file_path, "word/styles.xml"), "w+", encoding="UTF-8") as f: f.write(str(self.styles.get_dom())) def append_paragraph(self, text, align="left"): self.document.append_paragraph(text, align) def append_picture(self, filepath, align="left"): if not os.path.exists(filepath): return media_dir = os.path.join(self.file_path, "word/media") if not os.path.exists(media_dir): os.mkdir(media_dir) suffix = filepath.split(".")[-1] self.content_types.append_extension(suffix) id_file = self.relationships.append_relationship(suffix) #print(id_file) file_path = os.path.join( self.file_path, "word/media/{filename}".format(filename=id_file["filename"])) os.system("cp {f_file} {t_file}".format(f_file=filepath, t_file=file_path)) img = Image.open(file_path) width, height = img.size img.close() self.document.append_picture(id_file["rid"], width * 6350, height * 6350, align) def close(self): os.system("rm -rf {0}".format(self.file_path))
def recompute_and_populate(): """ - load pickled vectorizer - transform docs - compute cosine similarity for all vector pairs - data is retrieved at rev_rollup_ct = 1 (beer level) """ vec_pkl = "src/vocab/review_vectorizer.p" was_pkl, vec = load_vec(vec_pkl) # load data for styles with feature sets # overridden until full feature table is populated styles = Styles() top_sy = [159, 84, 157, 56, 58, 9, 128, 97, 116, 140] print "Comparing the top %s styles: %s" % (len(top_sy), ", ".join(str(s) for s in top_sy)) X = styles.beer_reviews_rollup(top_sy, limit=0, rev_rollup_ct=1, shuffle=False) if was_pkl: print "Loaded pickled vectorizer." print "Feature count: %s" % len(vec.get_feature_names()) print "Transforming reviews" trans_pool = Pool(min(10, len(top_sy))) res_t = trans_pool.map(__asyncable_transform, [(vec, sy, X[X["style_id"] == sy]) for sy in top_sy]) # as style keyed dict res_t = {r[0]: {"beer_ids": r[1], "X_t": r[2]} for r in res_t} else: # exit program return 0 print "Truncating similarity table" bs = BeerSimilarity() # bs.remove_all() dim1 = sum(v["X_t"].shape[0] for k, v in res_t.iteritems()) dim2 = sum(len(v["X_t"].data) for k, v in res_t.iteritems()) print "Computing similarities and saving to db %s" % dim1 print "Nonzero elements %s" % dim2 # set style RU # will account for symmetry in the database # ru_sids = [ (top_sy[i], top_sy[j]) for i in xrange(len(top_sy)) for j in xrange(i,len(top_sy)) ] ru_sids = [(top_sy[i], top_sy[i]) for i in xrange(len(top_sy))] pool_inp = [] for ruc in ru_sids: X_t_ref = res_t[ruc[0]]["X_t"] b_id_ref = res_t[ruc[0]]["beer_ids"] X_t_comp = res_t[ruc[1]]["X_t"] b_id_comp = res_t[ruc[1]]["beer_ids"] pool_inp.append((bs, b_id_ref, X_t_ref, b_id_comp, X_t_comp, 100)) p = Pool(min(10, len(top_sy))) b_id_res = p.map(__asyncable_similarity, pool_inp) for res in b_id_res: if res[1] is not None: print "%s %s" % (", ".join(str(r) for r in res[0]), res[1])
def __init__(self, root, data, devices): """Method initiates GUI and all it's main components""" super().__init__(root) self.root = root self.root.geometry('500x400') self.styles = Styles() # make GUI self.main_frame = Frame(self.root, height=400, width=500) self.main_frame.pack_propagate(0) self.main_frame.pack() self.left_frame = Frame(self.main_frame, relief='groove', bd=3, height=400, width=100) self.right_frame = Frame(self.main_frame, relief='groove', bd=3, height=400, width=500) self.right_top_frame = Frame(self.right_frame, relief='groove', bd=1, height=200, width=350) self.right_bottom_frame = Frame(self.right_frame, relief='groove', bd=1, height=200, width=350) self.left_frame.pack(anchor='n', side=LEFT, fill='x') self.right_frame.pack(anchor='n', side=RIGHT) self.right_top_frame.pack() self.right_bottom_frame.pack() self.button_add_device = Button(self.left_frame, text='Add Device', relief='groove', command=self.add_device).pack( side=TOP, expand=True, fill='x') # data export self.data = data self.start_recording_button = Button( self.right_bottom_frame, text='Start Recording', relief='groove', command=self.start_recording).pack(side=LEFT) self.stop_recording_button = Button( self.right_bottom_frame, text='Stop Recording', relief='groove', command=self.stop_recording).pack() # IoT devices - menu self.devices = devices self.iot_dev_name_var = StringVar() self.iot_dev_name_var.set( self.devices.list_of_devices[0].serial_number) self.iot_dev_name_var.trace( 'w', lambda *pargs: self.callback_iot_dev_name_var()) self.radio_buttons_init() # other objects self.ani = None self.main_frame.pack_propagate(0) self.root.resizable(0, 0)
def recompute_and_populate(): """ - load pickled vectorizer - transform docs - compute cosine similarity for all vector pairs - data is retrieved at rev_rollup_ct = 1 (beer level) """ vec_pkl = "src/vocab/review_vectorizer.p" was_pkl, vec = load_vec(vec_pkl) # load data for styles with feature sets # overridden until full feature table is populated styles = Styles() top_sy = [159, 84, 157, 56, 58, 9, 128, 97, 116, 140] print 'Comparing the top %s styles: %s' % (len(top_sy), ', '.join( str(s) for s in top_sy)) X = styles.beer_reviews_rollup(top_sy, limit=0, rev_rollup_ct=1, shuffle=False) if was_pkl: print "Loaded pickled vectorizer." print "Feature count: %s" % len(vec.get_feature_names()) print "Transforming reviews" trans_pool = Pool(min(10, len(top_sy))) res_t = trans_pool.map(__asyncable_transform, [(vec, sy, X[X['style_id'] == sy]) for sy in top_sy]) # as style keyed dict res_t = {r[0]: {'beer_ids': r[1], 'X_t': r[2]} for r in res_t} else: # exit program return 0 print 'Truncating similarity table' bs = BeerSimilarity() # bs.remove_all() dim1 = sum(v['X_t'].shape[0] for k, v in res_t.iteritems()) dim2 = sum(len(v['X_t'].data) for k, v in res_t.iteritems()) print 'Computing similarities and saving to db %s' % dim1 print 'Nonzero elements %s' % dim2 # set style RU # will account for symmetry in the database # ru_sids = [ (top_sy[i], top_sy[j]) for i in xrange(len(top_sy)) for j in xrange(i,len(top_sy)) ] ru_sids = [(top_sy[i], top_sy[i]) for i in xrange(len(top_sy))] pool_inp = [] for ruc in ru_sids: X_t_ref = res_t[ruc[0]]['X_t'] b_id_ref = res_t[ruc[0]]['beer_ids'] X_t_comp = res_t[ruc[1]]['X_t'] b_id_comp = res_t[ruc[1]]['beer_ids'] pool_inp.append((bs, b_id_ref, X_t_ref, b_id_comp, X_t_comp, 100)) p = Pool(min(10, len(top_sy))) b_id_res = p.map(__asyncable_similarity, pool_inp) for res in b_id_res: if res[1] is not None: print '%s %s' % (', '.join(str(r) for r in res[0]), res[1])
# Description: # Modified to support streaming out with webcams, and not just raw JPEGs. # Most of the code credits to Miguel Grinberg, except that I made a small tweak. Thanks! # Credits: http://blog.miguelgrinberg.com/post/video-streaming-with-flask # # Usage: # 1. Install Python dependencies: cv2, flask. (wish that pip install works like a charm) # 2. Run "python main.py". # 3. Navigate the browser to the local webpage. from flask import Flask, render_template, Response, request from camera import VideoCamera from styles import Styles import json app = Flask(__name__) styles = Styles() def generate_video(camera): while True: frame = camera.get_frame() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') def init_styles(): styles.set_eyebrows({"r": 0, "g": 0, "b": 255, "opacity": 128}) styles.set_eyeliner({"r": 0, "g": 255, "b": 0, "opacity": 110}) styles.set_lips({"r": 255, "g": 0, "b": 0, "opacity": 128})