def shift_old_hsv(image: QImage, dh, ds, dv): for x in range(image.width()): yield x for y in range(image.height()): color = image.pixelColor(x, y) h, s, v, a = color.getHsv() color.setHsv((h + dh + 360 * 2) % 360, inrange(s + ds, 0, 255), inrange(v + dv, 0, 255)) image.setPixel(x, y, color.rgb())
def notify(self, other: RemoteObj): """ <other> might be our predecessor """ p = self.predecessor() if p is None: self.set_predecessor(other) other_ro = get_remote(other) other_storage = other_ro.get_remote_storage() # send all my keys between self.id and other.id to other self.storage.release_tags(self.id, other_storage) self.storage.release_file_tags(self.id, other_storage) self.storage.release_files(self.id, other_storage) elif inrange(other.id, p.id, self.id): self.set_predecessor(other) other_ro = get_remote(other) other_storage = other_ro.get_remote_storage() # send all my keys between p.id and other.id to other self.storage.release_tags(p.id, other_storage) self.storage.release_file_tags(p.id, other_storage) self.storage.release_files(p.id, other_storage) elif not self.ping(p): self.set_predecessor(other) # load in storage all key from backups between other.id p.id self.storage.load_from_tags_backup(other.id, p.id) self.storage.load_from_file_tags_backup(other.id, p.id) self.storage.load_from_files_backup(other.id, p.id)
def load_from_files_backup(self, other_id: int, p_id: int): load_keys = [key for key in self.files_backups.keys() if inrange(key, other_id, p_id, (False, True))] print(f'Loading files {load_keys} from bakcup') for file in load_keys: self._del_backup_add_files(file)
def load_from_file_tags_backup(self, other_id: int, p_id: int): load_keys = [key for key in self.file_tags_backups.keys() if inrange(hash_str(key), other_id, p_id, (False, True))] print(f'Loading files {load_keys} from backup') for file in load_keys: tag_list = self.file_tags_backups.pop(file) self.file_tags[file] = tag_list
def load_from_tags_backup(self, other_id: int, p_id: int): load_keys = [key for key in self.tags_backups.keys() if inrange(key, other_id, p_id, (False, True))] print(f'Loading tags {load_keys} from bakcup') for tag in load_keys: file_list = self.tags_backups.pop(tag) self.tags[tag] = file_list
def closest_preceding_finger(self, id: int) -> RemoteObj: """ Return closest finger preceding id """ for i in reversed(range(M)): node = self.get_finger(i) if self.ping(node) and inrange(node.id, self.id, id): return node return self.get_info()
def release_file_tags(self, p_id: int, remote_storage): release_keys = [key for key in self.file_tags.keys() if inrange(hash_str(key), p_id, remote_storage.id, (False, True))] print(f'Releasing files {release_keys} to {remote_storage.id}') try: remote_storage_ro = get_remote(remote_storage) for file in release_keys: tag_list = self.file_tags.pop(file) remote_storage_ro.add_file_to_tags(file, tag_list) except Exception as ex: print(ex)
def release_tags(self, p_id: int, remote_storage): release_keys = [key for key in self.tags.keys() if inrange(key, p_id, remote_storage.id, (False, True))] print(f'Releasing tags {release_keys} to {remote_storage.id}') try: remote_storage_ro = get_remote(remote_storage) for tag in release_keys: file_list = self.tags.pop(tag) remote_storage_ro.add_tag_to_files(tag, file_list) except Exception as ex: print(ex)
def release_files(self, p_id: int, remote_storage): release_keys = [key for key in self.files.keys() if inrange(key, p_id, remote_storage.id, (False, True))] print(f'Releasing files {release_keys} to {remote_storage.id}') try: remote_storage_ro = get_remote(remote_storage) for file in release_keys: realname = self.files.pop(file) self.files_backups[file] = realname remote_storage_ro.upload_to_files(realname, self.ip, self.port) except Exception as ex: print(ex)
def stabilize(self): """ Periodically verify inmediate successor, and tell the successor about this node """ if not self.ping(self.successor_list[0]): return s = self.successor() s_ro = get_remote(s) x = s_ro.predecessor() if x is not None and inrange(x.id, self.id, s.id): self.set_successor(x) s = self.successor() get_remote(s).notify(self.get_info())
def find_predecessor(self, id: int) -> RemoteObj: """ Ask node to find id’s predecessor """ # corner case # if self.id() == id: # return self.predecessor() p = self.get_info() p_ro = get_remote(p) while not inrange(id, p.id, p_ro.successor().id, (False, True)): np = p_ro.closest_preceding_finger(id) # corner case # if p.id() == np.id(): # break p = np p_ro = get_remote(p) return p
def estimate_speed(word_lists, frame_numbers, min_speed=0.05, max_speed=0.6, height=None, **kwargs): """ Returns speed estimate of sliding ticker in pixels/frame. word_lists - Lists of words and their positions. frame_numbers - List of frame numbers corresponding to each word list. min_speed - Minimum possible speed of text as fraction of word height. max_speed = Maximum possible speed of text as fraction of word height. """ #Estimate movement speed of ticker, by looking at position of the same #word in consecutive images. estimate_sum = 0 estimate_count = 0 """ #Find a word height = - 1 for word_list in word_lists: for word in word_list: if len(word['text']) >= 3: height = word['bottom'] - word['top'] + 1 break if height != - 1: break else: raise ValueError("Cannot estimate speed if there are no words in word_lists!") """ #Iterate over every pair of neighbouring frames. for i in range(len(frame_numbers) - 1): word_list1 = word_lists[i] word_list2 = word_lists[i + 1] frame_diff = frame_numbers[i + 1] - frame_numbers[i] #Min and max allowable movement of word. min_diff = min_speed * height * frame_diff max_diff = max_speed * height * frame_diff #Iterate over all words in neighbouring frames. for word1 in word_list1: for word2 in word_list2: left_diff = word1['left'] - word2['left'] right_diff = word1['right'] - word2['right'] #Check that is same word. if not word1['text'] == word2['text']: continue #Check that text has moved a reasonable amount. if not inrange(left_diff, min_diff, max_diff): continue if not inrange(right_diff, min_diff, max_diff): continue #Check that word is long enough to probably not be coincidence. if not len(word1['text']) >= 3: continue #Calculate a speed estimate from word pair pixel_diff = word1['left'] - word2['left'] estimate = pixel_diff / frame_diff estimate_sum += estimate estimate_count += 1 return estimate_sum / estimate_count