def applylensdistortion(input_file, output_file, k, bordertype='fit', interpolation='linear', padmethod='symmetric', ftype=2): model = lambda r, k: r * (1 / (1 + k * r)) if int(ftype) == 2: model = lambda r, k: r * (1 / (1 + k * r * r)) elif int(ftype) == 3: model = lambda r, k: r * (1 + k * r) elif int(ftype) == 4: model = lambda r, k: r * (1 + k * r * r) if tool_set.fileType(input_file) == 'image': applytoimage(input_file, output_file, model, k, bordertype=bordertype, interpolation=interpolation, padmethod=padmethod, ftype=ftype) else: applytovideo(input_file, output_file, model, k, bordertype=bordertype, interpolation=interpolation, padmethod=padmethod, ftype=ftype)
def test_filetype(self): self.assertEquals(tool_set.fileType(self.locateFile('images/hat.jpg')), 'image') self.assertEquals(tool_set.fileType(self.locateFile('images/sample.json')), 'text') f = open('test.log', 'w+') f.close() self.addFileToRemove('test.log') self.assertEquals(tool_set.fileType(self.locateFile('test.log')), 'text') self.assertEquals(tool_set.fileType(self.locateFile('tests/videos/sample1.mov')), 'video') self.assertEquals(tool_set.fileType('foo.dng.zip'), 'zip') self.assertEquals(tool_set.fileType('foo.jpg.zip'), 'zip') self.assertEquals(tool_set.fileType('foo.png.zip'), 'zip') self.assertEquals(tool_set.fileType('foo.oh.zip'), 'collection') self.assertEquals(tool_set.fileType('foo.newgate.zip'), 'collection')
def check_mandatory(edge, opInfo, graph, frm, to): """ Check mandatory parameters. Check optional parameters condition on mandatory parameter values. 'inputmaskname' is treated special since it has historically been placed outside the arguments list. :param edge: :param opInfo: :param graph: :param frm: :param to: :return: list of (Severity, str) @type edge: dict @type op: Operation @type graph: ImageGraph @type frm: str @type to: str @rtype: list of (Severity, str) """ if opInfo.name == 'Donor': return [] if opInfo.category == 'Bad': return [ ValidationMessage(Severity.ERROR, frm, to, opInfo.name + ' is not a valid operation', 'Mandatory', None) ] if opInfo.name != 'Donor' else [] args = edge['arguments'] if 'arguments' in edge else [] frm_file = graph.get_image(frm)[1] frm_file_type = fileType(frm_file) missing = [param for param in opInfo.mandatoryparameters.keys() if (param not in args or len(str(args[param])) == 0) and \ ('source' not in opInfo.mandatoryparameters[param] or opInfo.mandatoryparameters[param][ 'source'] == frm_file_type)] for param_name, param_definition in opInfo.optionalparameters.iteritems(): if 'rule' in param_definition: if param_name not in args: for dep_param_name, dep_param_values in param_definition[ 'rule'].iteritems(): if len(dep_param_values) > 0 and \ getValue(args, dep_param_name, defaultValue=dep_param_values[0]) in dep_param_values: missing.append(param_name) missing = set(missing) inputmaskrequired = 'inputmaskname' in missing if inputmaskrequired: filename = getValue(edge, 'inputmaskname', defaultValue='') if len(filename) > 0 and os.path.exists( os.path.join(graph.dir, filename)): missing.remove('inputmaskname') return [ ValidationMessage(Severity.ERROR, frm, to, 'Mandatory parameter ' + m + ' is missing', 'Mandatory', None) for m in missing ]
def getVideoMetaItem(self, source, attribute, default=None, audio=False): """ Featch meta data, overwriting any keys from the cache in the instance graph's node identified by source. :param source: source node id :param with_frames: :param show_streams: :param media_types: :return: """ node_meta = self.__get_cache_from_graph(source) matched_value = None match_codec = 'video' if not audio else 'audio' for item in node_meta: if getValue(item, 'codec_type') == match_codec and matched_value is None: matched_value = getValue(item,attribute) if matched_value is not None: return matched_value source_file = self.graph.get_image_path(source) if fileType(source_file) not in ['audio','video']: return default return ffmpeg_api.get_frame_attribute(source_file, attribute, default=default, audio=audio)
def check_input_mask(edge, op, graph, frm, to): inputmaskname = edge[ 'inputmaskname'] if 'inputmaskname' in edge else None if inputmaskname is not None and len(inputmaskname) > 0 and \ not os.path.exists(os.path.join(graph.dir, inputmaskname)): return [ ValidationMessage( Severity.ERROR, frm, to, "Input mask file {} is missing".format(inputmaskname), 'Input Mask', repairMask) ] if inputmaskname is not None and len(inputmaskname) > 0 and \ os.path.exists(os.path.join(graph.dir, inputmaskname)): ft = fileType(os.path.join(graph.dir, inputmaskname)) if ft == 'audio': return [] inputmask = openImage(os.path.join(graph.dir, inputmaskname)) if inputmask is None: return [ ValidationMessage( Severity.ERROR, frm, to, "Input mask file {} is missing".format(inputmaskname), 'Input Mask', repairMask if ft == 'image' else None) ] inputmask = inputmask.to_mask().to_array() mask = openImageFile(os.path.join( graph.dir, edge['maskname'])).invert().to_array() if inputmask.shape != mask.shape: return [ ValidationMessage( Severity.ERROR, frm, to, 'input mask name parameter has an invalid size', 'Input Mask', repairMask if ft == 'image' else None) ] return []
def appliesTo(self, filename): return fileType(filename) == 'image'
def add(self): tkMessageBox.showinfo("Select Form", "Select the PDF of the signed release form.", master=self) form = tkFileDialog.askopenfilename(filetypes=[ ("Adobe Acrobat Document (PDF)", "*.pdf") ], initialdir=os.path.expanduser("~"), title="Add Release Form") path, name = os.path.split(os.path.splitext(form)[0]) name = name.title() if len(name.split(" ")) not in [2, 3]: tkMessageBox.showerror( "Error", "Release forms must be named under the name of the individual that signed " "it (ex. John Smith's release form would be \"John Smith.pdf\")", master=self) return pdf_page_count = PyPDF2.PdfFileReader(form).getNumPages() if pdf_page_count == 1: tkMessageBox.showinfo( "Select Image", "Select an image of the individual whose form was previously " "selected.", master=self) images = [ tkFileDialog.askopenfilename(filetypes=tool_set.imagefiletypes, initialdir=path, title="Add Image", master=self) ] else: tkMessageBox.showinfo( "Select Image Directory", "Select the directory containing an image for each person " "in the release form collection.") # Continuously retry until we get the right count or just give up retry = True while retry: image_dir = tkFileDialog.askdirectory( title="Select Image Directory", initialdir=os.path.expanduser("~"), master=self) images = [ os.path.join(image_dir, x) for x in os.listdir(image_dir) if fileType(os.path.join(image_dir, x)) == "image" ] # check count if len(images) != pdf_page_count: retry = tkMessageBox.askretrycancel( "PDF/Image Mismatch", "The image directory must contain one image for every " "release form. {0} release forms were found while {1} " "images were found.".format(pdf_page_count, len(images)), master=self) if not retry: return else: retry = False person_dir = os.path.join(self.formdir, name) if not os.path.exists(person_dir): os.mkdir(person_dir) shutil.copy2(form, person_dir) for im in images: shutil.copy2(im, person_dir) self.listvar.append(name) self.available.get_listbox().insert(END, name) return
def run_graph_suite(self, graph, external=None, status_cb=None): """ Run the validation suite including rules determined by operation definitions associated with each edge in the graph. :param graph: :param gopLoader: :param preferences: :param external: :return: list fo ValidationMessage @type graph: ImageGraph @type gopLoader: GroupOperationsLoader @type preferences: MaskGenLoader @rtype: list of ValidationMessage """ if status_cb is None: if ('log.validation' not in self.preferences or self.preferences['log.validation'] == 'yes'): status_cb = logStatus else: status_cb = ignoreStatus nodeSet = set(graph.get_nodes()) nodecount = len(nodeSet) edgecount = len(graph.get_edges()) if nodecount == 0: return [] total_errors = [] finalNodes = [] upgrades = graph.getDataItem('jt_upgrades', default_value=[]) if graph.getVersion( ) not in upgrades or graph.getVersion() != current_version(): total_errors.append( ValidationMessage( Severity.ERROR, '', '', 'The journal was not upgraded due to an error. Try saving and reopening the journal', 'Graph', None)) # check for disconnected nodes # check to see if predecessors > 1 consist of donors status_cb(ValidationStatus('Connectivity', 'graph', 0)) for node in graph.get_nodes(): if not graph.has_neighbors(node): total_errors.append( ValidationMessage( Severity.ERROR, str(node), str(node), str(node) + ' is not connected to other nodes', 'Graph', None)) predecessors = graph.predecessors(node) if len(predecessors) == 1 and graph.get_edge( predecessors[0], node)['op'] == 'Donor': total_errors.append( ValidationMessage( Severity.ERROR, str(predecessors[0]), str(node), str(node) + ' donor links must coincide with another link to the same destintion node', 'Graph', None)) successors = graph.successors(node) if len(successors) == 0: finalNodes.append(node) status_cb(ValidationStatus('Project Type', 'Graph', 4)) # check project type project_type = graph.get_project_type() matchedType = [ node for node in finalNodes if fileType( os.path.join(graph.dir, graph.get_node(node)['file'])) == project_type ] if len(matchedType) == 0 and len(finalNodes) > 0: graph.setDataItem( 'projecttype', fileType( os.path.join(graph.dir, graph.get_node(finalNodes[0])['file']))) status_cb(ValidationStatus('Graph Cuts', 'Graph', 5)) # check graph cuts for found in graph.findRelationsToNode(nodeSet.pop()): if found in nodeSet: nodeSet.remove(found) # check graph cuts for node in nodeSet: total_errors.append( ValidationMessage( Severity.ERROR, str(node), str(node), str(node) + ' is part of an unconnected subgraph', 'Graph', None)) status_cb(ValidationStatus('File Check', 'Graph', 6)) # check all files accounted for for file_error_tuple in graph.file_check(): total_errors.append( ValidationMessage(Severity.ERROR, file_error_tuple[0], file_error_tuple[1], file_error_tuple[2], 'Graph', None)) status_cb(ValidationStatus('Cycle Check', 'Graph', 8)) # check cycles cycleNode = graph.getCycleNode() if cycleNode is not None: total_errors.append( ValidationMessage(Severity.ERROR, str(cycleNode), str(cycleNode), "Graph has a cycle", 'Graph', None)) status_cb(ValidationStatus('Final Node Check', 'Duplicates', 9)) finalfiles = set() duplicates = dict() for node in finalNodes: filename = graph.get_node(node)['file'] if filename in finalfiles and filename not in duplicates: duplicates[filename] = node finalfiles.add(filename) # check duplicate final end nodes if len(duplicates) > 0: for filename, node in duplicates.iteritems(): total_errors.append( ValidationMessage( Severity.ERROR, str(node), str(node), "Duplicate final end node file %s" % filename, 'Graph', None)) validation_callback = ValidationCallback(advance_percent=90.0 / (nodecount + edgecount), start_percent=10.0, status_cb=status_cb) valiation_apis = ValidationAPIComposite( self.preferences, external=external, status_cb=validation_callback.update_state) validation_callback.advance_percent = 90.0 / ( (nodecount + edgecount) * (1 + len(valiation_apis.instances)) + len(valiation_apis.instances)) total_errors.extend(valiation_apis.check_graph(graph)) for node in graph.get_nodes(): total_errors.extend(valiation_apis.check_node(node, graph)) validation_callback.update_state( ValidationStatus('Internal', 'node {}'.format(node), 0)) for error in run_node_rules(graph, node, external=external, preferences=self.preferences): if type(error) != tuple: error = (Severity.ERROR, str(error)) total_errors.append( ValidationMessage(error[0], str(node), str(node), error[1], 'Node', None if len(error) == 2 else error[2])) for frm, to in graph.get_edges(): edge = graph.get_edge(frm, to) op = edge['op'] total_errors.extend(valiation_apis.check_edge(op, graph, frm, to)) validation_callback.update_state( ValidationStatus('Internal', 'edge {}:{}'.format(frm, to), 0)) errors = run_all_edge_rules( self.gopLoader.getOperationWithGroups(op, fake=True), self.rules[op] if op in self.rules else [], graph, frm, to) if len(errors) > 0: total_errors.extend(errors) validation_callback.status_cb( ValidationStatus('Validation', 'Complete', 100.0)) return total_errors