def displayFileDialog(self): self.inputFile = widgets.TextWidget( description='Input file name (csv)') button = widgets.ButtonWidget(description="Process file") display(self.inputFile, button) processCsvFileFn = lambda fileName: self.processCsvFile() button.on_click(processCsvFileFn)
def test_custom_description(): c = interactive(f, b=widgets.TextWidget(value='text', description='foo')) w = c.children[0] check_widget( w, cls=widgets.TextWidget, value='text', description='foo', )
def displayResultsDialog(self): self.resultWidget = widgets.SelectWidget(description='File info:', values=[]) fileDetail = widgets.TextWidget(description='Detail') self.resultLink = widgets.HTMLWidget(value=getDownloadResultLink(None)) def updateFileDetail(item, value): fileDetail.value = unicode(value) self.resultWidget.on_trait_change(updateFileDetail, 'value') display(self.resultWidget, fileDetail, self.resultLink)
def distributionSelector(self, directoryName): info = self.distributionInfo(directoryName) time = widgets.DropdownWidget(description="Time", values=info["times"], value=info["times"][-1]) line = widgets.DropdownWidget(description="Sample line", values=info["lines"]) varName = widgets.TextWidget(description="Variable Name") storeButton = widgets.ButtonWidget(description="Store to", disabled=True) def varname_change(name, value): storeButton.description = "Store to " + value if len(value) == 0: storeButton.disabled = True else: dis = False if not value[0].isalpha(): dis = True storeButton.disabled = dis varName.on_trait_change(varname_change, 'value') def store_clicked(b): l = line.value v = varName.value name = self.__getObjectName(self) store = self.__getStorageName() t = time.value print_("Storing distribution at t=", t, "on line", l, "from", directoryName, "in", self.path, "to variable", v) cmd = "%s.distribution('%s','%s','%s')" % (name, directoryName, l, t) if store: sname, sval = store create_code_cell("%s=%s('%s',lambda:%s)" % (v, sname, v, cmd), "below") val = sval(v, lambda: self.distribution(directoryName, l, t)) else: create_code_cell("%s=%s" % (v, cmd), "below") val = self.distribution(directoryName, l, t) get_ipython().push({v: val}) varName.value = "" storeButton.on_click(store_clicked) total = widgets.ContainerWidget( children=[line, time, varName, storeButton]) total.add_class("vbox") display(total)
def load_original_slides(name): original_slides = [] i = 0 #progress = widgets.IntProgressWidget(min=0, max = num_of_frames - 1, value=0) progress_text = widgets.TextWidget() #progress.set_css('background', 'black') #display(progress) display(progress_text) while True: progress_text.value = "loading %d" % i img = cv2.imread("%s/%s-%d.png" % (name, name, i)) if img is None: break original_slides.append(img) i += 1 print "load original slides", len(original_slides) return original_slides
def make_form(self, css_classes): iocont = widgets.ContainerWidget() css_classes[iocont] = ('iobject-container') add_child(iocont, widgets.HTMLWidget(value="<h3>%s</h3>" % self.name)) for inp in self.free_inputs: #We have to filter none kw... allkw = dict(description=inp.name, value=inp.value, default=inp.default) kw = { key: value for (key, value) in allkw.items() if value is not None } w = widgets.TextWidget(**kw) inp.widget = w w.traits()['value'].allow_none = True traitlets.link((inp, 'value'), (w, 'value')) def set_exec(_w): self.executed = False w.on_trait_change(set_exec, name='value') add_child(iocont, w) for out in self.free_outputs: w = widgets.HTMLWidget() out.widget = w add_child(iocont, w) w.traits()['value'].allow_none = True traitlets.link((out, 'value'), (w, 'value')) button = widgets.ButtonWidget(description="Execute %s" % self.name) def _run(b): self.executed = False self.run_sync() button.on_click(_run) add_child(iocont, button) self.widget = iocont self.widget.button = button self.has_widget = True self._toggle_executed() return iocont
def pickledDataSelector(self): pData = widgets.DropdownWidget(description="Pickled data file", values=self.sol.pickledData, value=self.sol.pickledData[0]) varName = widgets.TextWidget(description="Variable Name") storeButton = widgets.ButtonWidget(description="Store to", disabled=True) def varname_change(name, value): storeButton.description = "Store to " + value if len(value) == 0: storeButton.disabled = True else: dis = False if not value[0].isalpha(): dis = True storeButton.disabled = dis varName.on_trait_change(varname_change, 'value') def store_clicked(b): p = pData.value v = varName.value name = self.__getObjectName(self) store = self.__getStorageName() print_("Storing Pickled Data from", p, "to variable", v) cmd = "%s.pickledData('%s')" % (name, p) if store: sname, sval = store create_code_cell("%s=%s('%s',lambda:%s)" % (v, sname, v, cmd), "below") val = sval(v, lambda: self.pickledData(p)) else: create_code_cell("%s=%s" % (v, cmd), "below") val = self.pickledData(p) get_ipython().push({v: val}) varName.value = "" storeButton.on_click(store_clicked) total = widgets.ContainerWidget(children=[pData, varName, storeButton]) total.add_class("vbox") display(total)
from IPython.html import widgets from IPython.display import display txt = widgets.TextWidget() display(txt) txt.value = 'Some Text' print(txt.value) """< Some Text >"""
def sampleSelector(self, directoryName): info = self.sampleInfo(directoryName) mode = widgets.ToggleButtonsWidget(description="Mode", values=["Time", "Field"]) field = widgets.DropdownWidget(description="Field", values=info["values"]) time = widgets.DropdownWidget(description="Time", values=info["times"], value=info["times"][-1]) line = widgets.DropdownWidget(description="Sample line", values=info["lines"]) varName = widgets.TextWidget(description="Variable Name") storeButton = widgets.ButtonWidget(description="Store to", disabled=True) def mode_changed(name, value): if value == "Time": time.disabled = False field.disabled = True else: time.disabled = True field.disabled = False mode.on_trait_change(mode_changed, 'value') mode_changed('value', mode.value) def varname_change(name, value): storeButton.description = "Store to " + value if len(value) == 0: storeButton.disabled = True else: dis = False if not value[0].isalpha(): dis = True storeButton.disabled = dis varName.on_trait_change(varname_change, 'value') def store_clicked(b): l = line.value v = varName.value name = self.__getObjectName(self) store = self.__getStorageName() if mode.value == "Time": t = time.value print_("Storing fields at t=", t, "on line", l, "from", directoryName, "in", self.path, "to variable", v) cmdBase = "%s.sampleTime('%s','%s','%s')" % ( name, directoryName, l, t) if store: sname, sval = store cmd = "%s=%s('%s',lambda:%s)" % (v, sname, v, cmdBase) val = sval(v, lambda: self.sampleTime(directoryName, l, t)) else: cmd = "%s=%s" % (v, cmdBase) val = self.sampleTime(directoryName, l, t) elif mode.value == "Field": f = field.value print_("Storing fields", f, " at all times on line", l, "from", directoryName, "in", self.path, "to variable", v) cmdBase = "%s.sampleField('%s','%s','%s')" % ( name, directoryName, l, f) if store: sname, sval = store cmd = "%s=%s('%s',lambda:%s)" % (v, sname, v, cmdBase) val = sval(v, lambda: self.sampleField(directoryName, l, f)) else: cmd = "%s=%s" % (v, cmdBase) val = self.sampleField(directoryName, l, f) else: print_("Unknown mode", mode) return create_code_cell(cmd, "below") get_ipython().push({v: val}) varName.value = "" storeButton.on_click(store_clicked) total = widgets.ContainerWidget( children=[mode, line, field, time, varName, storeButton]) total.add_class("vbox") display(total)
def timelineSelector(self, directoryName): info = self.timelineInfo(directoryName) lst = [widgets.LatexWidget(value="Fields:")] fieldsSelected = set() storeButton = widgets.ButtonWidget(description="Store to", disabled=True) def make_field_toggle(fName): def f(name, value): if value: fieldsSelected.add(fName) else: try: fieldsSelected.remove(fName) except KeyError: pass # this should not happen, but hey! if len(fieldsSelected) > 0: storeButton.disabled = False else: storeButton.disabled = True return f for f in info["fields"]: w = widgets.ToggleButtonWidget(description=f) w.on_trait_change(make_field_toggle(f), 'value') lst.append(w) fields = widgets.ContainerWidget(description="Fields", children=lst) fields.add_class("hbox") varName = widgets.TextWidget(description="Variable Name") def varname_change(name, value): storeButton.description = "Store to " + value if len(value) == 0 or len(fieldsSelected) == 0: storeButton.disabled = True else: dis = False if not value[0].isalpha(): dis = True storeButton.disabled = dis varName.on_trait_change(varname_change, 'value') def store_clicked(b): v = varName.value f = list(fieldsSelected) print_("Storing", f, "from", directoryName, "in", self.path, "to variable", v) name = self.__getObjectName(self) store = self.__getStorageName() cmd = "%s.timeline('%s',%s)" % (name, directoryName, str(f)) if store: sname, sval = store create_code_cell("%s=%s('%s',lambda:%s)" % (v, sname, v, cmd), "below") val = sval(v, lambda: self.timeline(directoryName, f)) else: create_code_cell("%s=%s" % (v, cmd), "below") val = self.timeline(directoryName, f) get_ipython().push({v: val}) varName.value = "" storeButton.on_click(store_clicked) total = widgets.ContainerWidget( children=[fields, varName, storeButton]) total.add_class("vbox") display(total)
def get(): txt = widgets.TextWidget() display(txt) txt.value = 'Some Text'
def sync_video(fn, p, slides, slides_v, threshold=0.8, step=10, dark=1000, STOP=-1, debug=False): cap = cv2.VideoCapture(fn) frame_rate = cap.get(cv.CV_CAP_PROP_FPS) print "frame_rate", frame_rate num_of_frames = cap.get(cv.CV_CAP_PROP_FRAME_COUNT) frame_index = -1 last_slide = -1 last_start = -1 frame_list = [] progress = widgets.IntProgressWidget(min=0, max=num_of_frames - 1, value=0) progress_text = widgets.TextWidget() progress.set_css('background', 'black') display(progress) display(progress_text) while cap.isOpened(): frame_index += 1 ret, frame = cap.read() if not ret: break if frame_index % step == 0: if STOP != -1 and frame_index > STOP: break gray = cv2.resize( normalize(frame)[p[0]:p[2], p[1]:p[3]], (256, 256)) darklevel = np.linalg.norm(gray.reshape(-1).astype(float)) if darklevel < dark: # too dark this_slide, v, i = -1, -1, 0 else: v, i = compare_slides(gray, slides_v) this_slide = i if v > threshold else -1 if debug: if i >= 0: frame2 = frame.copy() frame2[p[0]:p[2], p[1]:p[3]] = cv2.resize( original_slides[i][q[0]:q[2], q[1]:q[3]], (p[3] - p[1], p[2] - p[0])) outp = np.concatenate( (frame2, cv2.addWeighted(frame, 0.5, frame2, 0.5, 0), frame), axis=1) display_img_array(outp, width=1200, cvt=cv2.COLOR_BGR2RGB) else: display_img_array(frame, width=400, cvt=cv2.COLOR_BGR2RGB) print v, i if frame_index % 100 == 0: progress.value = frame_index progress_text.value = "%d/%d (%.1f)" % ( frame_index, num_of_frames, 100.0 * frame_index / num_of_frames) if this_slide != last_slide: # update frame_list.append((last_start, frame_index - 1, last_slide)) # display information if last_slide >= 0: fl = frame_list[-1] t1, t2 = frame_to_time(fl[0], frame_rate), frame_to_time( fl[1], frame_rate) print fl, "=(%s, %s)" % ( t1, t2), "v=%f" % v, "dark=%d" % darklevel last_start = frame_index if debug: p1 = cv2.resize(frame, (320, 240)) if i >= 0: p2 = cv2.resize(original_slides[i], (320, 240)) p1 = np.concatenate((p1, p2), axis=1) if last_slide >= 0: display_img_array(cv2.cvtColor(p1, cv2.COLOR_BGR2RGB), width=200) last_slide = this_slide # last update frame_list.append((last_start, frame_index - 1, last_slide)) cap.release() return frame_list
def write_file(fn, p, q, outfn, original_slides, sync_result, M=20, fourcc="XVID", SKIP=None): W, H = 1440, 960 W, H = 1920, 1080 cap = cv2.VideoCapture(fn) SW, SH = int(cap.get(cv.CV_CAP_PROP_FRAME_WIDTH)), int( cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT)) p2 = (p[0] * H / SH, p[1] * W / SW, p[2] * H / SH, p[3] * W / SW) pw, ph = p2[3] - p2[1], p2[2] - p2[0] print p2, q fourcc = cv.FOURCC(*fourcc) num_of_frames = cap.get(cv.CV_CAP_PROP_FRAME_COUNT) frame_rate = cap.get(cv.CV_CAP_PROP_FPS) print "frame_rate", frame_rate sys.stdout.flush() out = cv2.VideoWriter(outfn, fourcc, frame_rate, (W, H)) frame_index = -1 last_slide = -1 last_start = -1 frame_list = [] result_index = 0 progress = widgets.IntProgressWidget(min=0, max=num_of_frames - 1, value=0) progress_text = widgets.TextWidget() progress.set_css('background', 'black') display(progress) display(progress_text) while cap.isOpened(): frame_index += 1 ret, frame = cap.read() if not ret: break while result_index < len( sync_result) and sync_result[result_index][1] < frame_index: result_index += 1 the_slide = ( -1, -1, -1 ) if result_index >= len(sync_result) else sync_result[result_index] if SKIP and the_slide[2] in SKIP: the_slide = (-1, -1, -1) original_frame = cv2.resize(frame, (W, H), interpolation=cv2.INTER_CUBIC) if the_slide[2] >= 0 and the_slide[1] - the_slide[0] > 3 * M: slide = original_slides[the_slide[2]] inner_frame = cv2.resize(slide[q[0]:q[2], q[1]:q[3]], (pw, ph), interpolation=cv2.INTER_CUBIC) d = min(frame_index - the_slide[0], the_slide[1] - frame_index) out_frame = original_frame.copy() out_frame[p2[0]:p2[2], p2[1]:p2[3]] = inner_frame if d < M: out_frame = cv2.addWeighted(out_frame, d * 1.0 / M, original_frame, 1 - d * 1.0 / M, 0) else: out_frame = original_frame out.write(out_frame) if frame_index % 100 == 0: progress.value = frame_index progress_text.value = "%d/%d (%.1f)" % (frame_index, num_of_frames, 100.0 * frame_index / num_of_frames) if frame_index % 10000 == 0: disp_frame = np.concatenate( (out_frame[:, :W / 2], original_frame[:, W / 2:]), axis=1) display_img_array(cv2.cvtColor(disp_frame, cv2.COLOR_BGR2RGB), width=200) cap.release() out.release()