def savejob(id, data): job = session.query(Job).get(id) for path in job.paths: session.delete(path) for pi in job.predicate_instances: for pa in pi.predicate_annotations: session.delete(pa) session.delete(pi) for s in job.sentences: for sa in s.annotations: session.delete(sa) session.delete(s) session.commit() paths = readpaths(data["tracks"]) for path in paths: job.paths.append(path) for pi in readpredicates(data["predicates"], paths): job.predicate_instances.append(pi) for s in readsentences(data['sentences']): job.sentences.append(s) session.add(job) session.commit()
def __call__(self, args): video = session.query(Video).filter(Video.slug == args.slug) if not video.count(): print "Video {0} does not exist!".format(args.slug) return video = video.one() query = session.query(Path) query = query.join(Job) query = query.join(Segment) query = query.filter(Segment.video == video) numpaths = query.count() if numpaths and not args.force: print ("Video has {0} paths. Use --force to delete." .format(numpaths)) return for segment in video.segments: for job in segment.jobs: if job.published and not job.completed: hitid = job.disable() print "Disabled {0}".format(hitid) session.delete(video) session.commit() print "Deleted video and associated data."
def vatic_turkic_delete(args): video = session.query(Video).filter(Video.slug == args.slug) if not video.count(): print "Video {0} does not exist!".format(args.slug) raise ValueError("Video {0} does not exist!".format(args.slug)) video = video.one() query = session.query(Path) query = query.join(Job) query = query.join(Segment) query = query.filter(Segment.video == video) numpaths = query.count() if numpaths and not args.force: print ("Video has {0} paths. Use --force to delete." .format(numpaths)) return False, numpaths for segment in video.segments: for job in segment.jobs: if job.published and not job.completed: hitid = job.disable() print "Disabled {0}".format(hitid) session.delete(video) session.commit() print "Deleted video and associated data." return True, -1
def savejob(id, tracks): job = session.query(Job).get(id) for path in job.paths: session.delete(path) session.commit() for path in readpaths(tracks): job.paths.append(path) session.add(job) session.commit()
def savejob1(id, data): # data contain comment, orientation, tracks job = session.query(Job).get(id) # seperate three parts #job.actionstart = data[0][0] #job.actionstop = data[1][0] #job.orientation = data[2][0] job.comment = data[3][0] if job.comment == "null": job.comment = "NULL" tracks = data[4] # delete old path in the database for path in job.paths: session.delete(path) session.commit() for path in readpaths(tracks): job.paths.append(path) session.add(job) session.commit()
def __call__(self, args, group): print "Checking integrity..." # read first frame to get sizes path = Video.getframepath(0, args.location) try: im = Image.open(path) except IOError: print "Cannot read {0}".format(path) return width, height = im.size print "Searching for last frame..." # search for last frame toplevel = max(int(x) for x in os.listdir(args.location)) secondlevel = max( int(x) for x in os.listdir("{0}/{1}".format(args.location, toplevel))) maxframes = max( int(os.path.splitext(x)[0]) for x in os.listdir("{0}/{1}/{2}".format(args.location, toplevel, secondlevel))) + 1 print "Found {0} frames.".format(maxframes) # can we read the last frame? path = Video.getframepath(maxframes - 1, args.location) try: im = Image.open(path) except IOError: print "Cannot read {0}".format(path) return # check last frame sizes if im.size[0] != width and im.size[1] != height: print "First frame dimensions differs from last frame" return if session.query(Video).filter(Video.slug == args.slug).count(): print "Video {0} already exists!".format(args.slug) print "updating labels for {0}".format(args.slug) # j: add in update label function video = session.query(Video).filter( Video.slug == args.slug).first() # check if such label has any paths associated with it for label in video.labels: if not session.query(Path).filter( Path.labelid == label.id).count(): print 'No path associated. deleted label {} {}'.format( label.id, label.text) session.delete(label) else: print 'Path associated exists. keep label {} {}'.format( label.id, label.text) existing_labels = [label.text for label in video.labels] labelcache = {} attributecache = {} lastlabel = None for labeltext in args.labels: if labeltext[0] == "~": if lastlabel is None: print "Cannot assign an attribute without a label!" return labeltext = labeltext[1:] attribute = Attribute(text=labeltext) session.add(attribute) lastlabel.attributes.append(attribute) attributecache[labeltext] = attribute else: if labeltext in existing_labels: print 'label: {} already in video'.format(label) continue label = Label(text=labeltext) print 'add label: {}'.format(label) session.add(label) video.labels.append(label) labelcache[labeltext] = label lastlabel = label session.commit() return if args.train_with: if args.for_training: print "A training video cannot require training" return print "Looking for training video..." trainer = session.query(Video) trainer = trainer.filter(Video.slug == args.train_with) if not trainer.count(): print("Training video {0} does not exist!".format( args.train_with)) return trainer = trainer.one() else: trainer = None homographydir = os.path.abspath(os.path.join("homographies", args.slug)) if not os.path.isdir(homographydir): os.makedirs(homographydir) if args.homography is not None: shutil.copy(args.homography, os.path.join(homographydir, "homography.npy")) else: np.save(os.path.join(homographydir, "homography.npy"), np.identity(3)) # create video video = Video(slug=args.slug, location=os.path.realpath(args.location), width=width, height=height, totalframes=maxframes, skip=args.skip, perobjectbonus=args.per_object_bonus, completionbonus=args.completion_bonus, trainwith=trainer, isfortraining=args.for_training, blowradius=args.blow_radius, homographylocation=homographydir, pointmode=args.pointmode) session.add(video) print "Binding labels and attributes..." # create labels and attributes labelcache = {} attributecache = {} lastlabel = None for labeltext in args.labels: if labeltext[0] == "~": if lastlabel is None: print "Cannot assign an attribute without a label!" return labeltext = labeltext[1:] attribute = Attribute(text=labeltext) session.add(attribute) lastlabel.attributes.append(attribute) attributecache[labeltext] = attribute else: label = Label(text=labeltext) print 'add label: {}'.format(label) session.add(label) video.labels.append(label) labelcache[labeltext] = label lastlabel = label print "Creating symbolic link..." symlink = "public/frames/{0}".format(video.slug) try: os.remove(symlink) except: pass os.symlink(video.location, symlink) print "Creating segments..." # create shots and jobs if args.for_training: segment = Segment(video=video) if args.for_training_start: segment.start = args.for_training_start if segment.start < 0: segment.start = 0 else: segment.start = 0 if args.for_training_stop: segment.stop = args.for_training_stop if segment.stop > video.totalframes - 1: segment.stop = video.totalframes - 1 else: segment.stop = video.totalframes - 1 job = Job(segment=segment, group=group, ready=False) session.add(segment) session.add(job) elif args.use_frames: with open(args.use_frames) as useframes: for line in useframes: ustart, ustop = line.split() ustart, ustop = int(ustart), int(ustop) validlength = float(ustop - ustart) numsegments = math.ceil(validlength / args.length) segmentlength = math.ceil(validlength / numsegments) for start in range(ustart, ustop, int(segmentlength)): stop = min(start + segmentlength + args.overlap + 1, ustop) segment = Segment(start=start, stop=stop, video=video) job = Job(segment=segment, group=group) session.add(segment) session.add(job) else: startframe = args.start_frame stopframe = args.stop_frame if not stopframe: stopframe = video.totalframes - 1 for start in range(startframe, stopframe, args.length): stop = min(start + args.length + args.overlap + 1, stopframe) segment = Segment(start=start, stop=stop, video=video) job = Job(segment=segment, group=group) session.add(segment) session.add(job) if args.per_object_bonus: group.schedules.append( PerObjectBonus(amount=args.per_object_bonus)) if args.completion_bonus: group.schedules.append( CompletionBonus(amount=args.completion_bonus)) session.add(group) if args.for_training and args.for_training_data: print("Loading training ground truth annotations from {0}".format( args.for_training_data)) with open(args.for_training_data, "r") as file: pathcache = {} for line in file: (id, xtl, ytl, xbr, ybr, frame, outside, occluded, generated, label) = line.split(" ") if int(generated): continue if id not in pathcache: print "Imported new path {0}".format(id) label = labelcache[label.strip()[1:-1]] pathcache[id] = Path(job=job, label=label) box = Box(path=pathcache[id]) box.xtl = int(xtl) box.ytl = int(ytl) box.xbr = int(xbr) box.ybr = int(ybr) box.frame = int(frame) box.outside = int(outside) box.occluded = int(occluded) pathcache[id].boxes.append(box) elif args.run_initial_tracking: # Add initial paths path_dict = get_paths(video) session.commit() if args.for_training: if args.for_training and args.for_training_data: print "Video and ground truth loaded." else: print "Video loaded and ready for ground truth:" print "" print "\t{0}".format(job.offlineurl(config.localhost)) print "" print "Visit this URL to provide training with ground truth." else: print "Video loaded and ready for publication."
def savejob(id, tracks): job = session.query(Job).get(id) # Update current job for path in job.paths: session.delete(path) session.commit() for path in readpaths(tracks): logger.info(path) job.paths.append(path) session.add(job) session.commit() # Update neigboring segments video = job.segment.video prevseg, nextseg = video.getsegmentneighbors(job.segment) mergesegments = [s for s in [prevseg, job.segment, nextseg] if s is not None] updatesegments = [s for s in [prevseg, nextseg] if s is not None] # Create list of merged boxes with given label and userid labeledboxes = [] for boxes, paths in merge.merge(mergesegments, threshold=0.8): path = paths[0] for p in paths: if p.job.segmentid == job.segmentid: path = p break labeledboxes.append((path.label, path.userid, boxes)) # Remove paths in neighboring segments for segment in updatesegments: for path in segment.paths: session.delete(path) session.commit() # Add merged paths to neighboring segments for label, userid, boxes in labeledboxes: frames = sorted([box.frame for box in boxes]) for segment in updatesegments: for job in segment.jobs: path = Path() path.label = label path.userid = userid addedbox = False for box in boxes: if segment.start <= box.frame <= segment.stop: newbox = Box(path=path) newbox.frombox(box) if not box.lost: addedbox = True # Some segments and paths might not overlap if addedbox: # Add in first frame if it's missing if (frames[0] < segment.start < frames[-1] and segment.start not in frames): newbox = Box(path=path) newbox.generated = False newbox.frombox( [box for box in LinearFill(boxes) if box.frame == segment.start][0] ) job.paths.append(path) session.add(job) session.commit()
def savejob(id, tracks): job = session.query(Job).get(id) # Update current job for path in job.paths: session.delete(path) session.commit() for path in readpaths(tracks): logger.info(path) job.paths.append(path) session.add(job) session.commit() # Update neigboring segments video = job.segment.video prevseg, nextseg = video.getsegmentneighbors(job.segment) mergesegments = [ s for s in [prevseg, job.segment, nextseg] if s is not None ] updatesegments = [s for s in [prevseg, nextseg] if s is not None] # Create list of merged boxes with given label and userid labeledboxes = [] for boxes, paths in merge.merge(mergesegments, threshold=0.8): path = paths[0] for p in paths: if p.job.segmentid == job.segmentid: path = p break labeledboxes.append((path.label, path.userid, boxes)) # Remove paths in neighboring segments for segment in updatesegments: for path in segment.paths: session.delete(path) session.commit() # Add merged paths to neighboring segments for label, userid, boxes in labeledboxes: frames = sorted([box.frame for box in boxes]) for segment in updatesegments: for job in segment.jobs: path = Path() path.label = label path.userid = userid addedbox = False for box in boxes: if segment.start <= box.frame <= segment.stop: newbox = Box(path=path) newbox.frombox(box) if not box.lost: addedbox = True # Some segments and paths might not overlap if addedbox: # Add in first frame if it's missing if (frames[0] < segment.start < frames[-1] and segment.start not in frames): newbox = Box(path=path) newbox.generated = False newbox.frombox([ box for box in LinearFill(boxes) if box.frame == segment.start ][0]) job.paths.append(path) session.add(job) session.commit()
def addtracks( id, base_path='/media/leo/24DDF7874B2D4C94/FLASH_ALPHA', fname="/media/leo/24DDF7874B2D4C94/FLASH_ALPHA/401/test/401/mse0.1_track0.01_output.txt", resize=True): ''' Function for adding pre-existing tracks to the database Inputs :fname: string, path to track file Returns None ''' job = session.query(Job).get(id) logger.debug(job) labels = session.query(Label).all() slug = session.query(Video).get(id) code = slug[0:3] path = os.path.join(base_path, slug, "test", slug) for f in os.listdir(path): if "output" in f: fname = f fname = os.path.join(path, fname) # Print all labels print("There are %d labels in the track" % (len(labels))) for l in labels: print("Label id %d corresponds to %s" % (int(l.id), l.text)) # Add labels face_label = Label(text="Face") tv_label = Label(text="TV") #session.add(face_label) #session.add(tv_label) place = Attribute(text="Placeholder") gaze = Attribute(text="Gaze") no_gaze = Attribute(text="No-Gaze") uncertain = Attribute(text="Uncertain") out = Attribute(text="Out-of-Frame") attribs = [place, gaze, no_gaze, uncertain, out] for a in attribs: face_label.attributes.append(a) tv_label.attributes.append(Attribute(text="On")) if job.paths == None or len(job.paths) == 0: print("Adding tracks from file %s" % (fname)) else: print("Tracks already exist for this segment, no need to load!") print(len(job.paths)) print([j.id for j in job.paths]) for path in job.paths: session.delete(path) session.commit() #return info = np.loadtxt(fname, dtype=int) """ info[:,0] ==> frame number info[:,1] ==> left info[:,2] ==> top info[:,3] ==> right info[:,4] ==> bottom info[:,5] ==> faceID info[:,6] ==> pause flag """ """ Even with an actual label, it still says the label is none in readpath """ face_label = session.query(Label).all()[0] print(face_label.text) print(face_label.id) num_faces = np.max(info[:, 5]) scale = 1 if resize: scale = .375 print("There are %d unique faces detected." % (num_faces)) thresh = 10 for newID in range(num_faces): print(newID) if newID == thresh: break path = Path(job=job, label=session.query(Label).get(82)) # uncomment below only if necessary #path.label = 100 # label is some constant integer value #path.label = Label(text = "Face") path.userid = newID # userid is the label ID lines = info[np.where(info[:, 5] == newID)[0], :] boxes = [] # need to scale within range 1920x1080 -> 720x405 # # need to add attributes # need to add include callback? # need to include that these are of class Face for i in range(lines.shape[0]): box = Box(path=path) box.xtl = max(int(lines[i, 1]), 0) * scale box.ytl = max(int(lines[i, 2]), 0) * scale box.xbr = max(int(lines[i, 3]), 0) * scale box.ybr = max(int(lines[i, 4]), 0) * scale box.occluded = int(0) box.outside = int(0) box.generated = int(1) box.frame = int(lines[i, 0]) logger.debug("Received box {0}".format(str(box.getbox()))) """ # TODO: May need to include this if attributes load funny attributes = {} for label in video.labels: attributes[label.id] = dict((a.id, a.text) for a in label.attributes) for attributeid, timeline in attributes.items(): attribute = session.query(Attribute).get(attributeid) for frame, value in timeline.items(): aa = AttributeAnnotation() aa.attribute = attribute aa.frame = frame aa.value = value path.attributes.append(aa) """ job.paths.append(path) #print("Appended path %d"%(newID)) #for p in job.paths: # session.delete(p) session.add(job) session.commit() return min(thresh, num_faces)