def get_data(self, host_dict): if not self.manual: if len(self.input_data) > 0: return self.input_data.pop(0) else: skip_remaining = False allow_remark_faces = False while len(self.input_data) > 0: data = self.input_data[0] filename, faces = data is_frame_done = False go_to_prev_frame = False # Can we mark an image that already has a marked face? if allow_remark_faces: allow_remark_faces = False # If there was already a face then lock the rectangle to it until the mouse is clicked if len(faces) > 0: self.rect, self.landmarks = faces.pop() self.rect_locked = True self.redraw_needed = True faces.clear() self.rect_size = (self.rect[2] - self.rect[0]) / 2 self.x = (self.rect[0] + self.rect[2]) / 2 self.y = (self.rect[1] + self.rect[3]) / 2 if len(faces) == 0: if self.cache_original_image[0] == filename: self.original_image = self.cache_original_image[1] else: self.original_image = cv2_imread(filename) self.cache_original_image = (filename, self.original_image) (h, w, c) = self.original_image.shape self.view_scale = 1.0 if self.manual_window_size == 0 else self.manual_window_size / ( h * (16.0 / 9.0)) if self.cache_image[0] == (h, w, c) + (self.view_scale, filename): self.image = self.cache_image[1] else: self.image = cv2.resize(self.original_image, (int( w * self.view_scale), int(h * self.view_scale)), interpolation=cv2.INTER_LINEAR) self.cache_image = ((h, w, c) + (self.view_scale, filename), self.image) (h, w, c) = self.image.shape sh = (0, 0, w, min(100, h)) if self.cache_text_lines_img[0] == sh: self.text_lines_img = self.cache_text_lines_img[1] else: self.text_lines_img = (image_utils.get_draw_text_lines( self.image, sh, [ 'Match landmarks with face exactly. Click to confirm/unconfirm selection', '[Enter] - confirm face landmarks and continue', '[Space] - confirm as unmarked frame and continue', '[Mouse wheel] - change rect', '[,] [.]- prev frame, next frame. [Q] - skip remaining frames', '[h] - hide this help' ], (1, 1, 1)) * 255).astype(np.uint8) self.cache_text_lines_img = (sh, self.text_lines_img) while True: new_x = self.x new_y = self.y new_rect_size = self.rect_size mouse_events = io.get_mouse_events(self.wnd_name) for ev in mouse_events: (x, y, ev, flags) = ev if ev == io.EVENT_MOUSEWHEEL and not self.rect_locked: mod = 1 if flags > 0 else -1 diff = 1 if new_rect_size <= 40 else np.clip( new_rect_size / 10, 1, 10) new_rect_size = max(5, new_rect_size + diff * mod) elif ev == io.EVENT_LBUTTONDOWN: self.rect_locked = not self.rect_locked self.redraw_needed = True elif not self.rect_locked: new_x = np.clip(x, 0, w - 1) / self.view_scale new_y = np.clip(y, 0, h - 1) / self.view_scale key_events = io.get_key_events(self.wnd_name) key, = key_events[-1] if len(key_events) > 0 else (0, ) if key == ord('\r') or key == ord('\n'): faces.append([(self.rect), self.landmarks]) is_frame_done = True break elif key == ord(' '): is_frame_done = True break elif key == ord('.'): allow_remark_faces = True # Only save the face if the rect is still locked if self.rect_locked: faces.append([(self.rect), self.landmarks]) is_frame_done = True break elif key == ord(',') and len(self.result) > 0: # Only save the face if the rect is still locked if self.rect_locked: faces.append([(self.rect), self.landmarks]) go_to_prev_frame = True break elif key == ord('q'): skip_remaining = True break elif key == ord('h'): self.hide_help = not self.hide_help break if self.x != new_x or \ self.y != new_y or \ self.rect_size != new_rect_size or \ self.redraw_needed: self.x = new_x self.y = new_y self.rect_size = new_rect_size self.rect = (int(self.x - self.rect_size), int(self.y - self.rect_size), int(self.x + self.rect_size), int(self.y + self.rect_size)) return [filename, [self.rect]] io.process_messages(0.0001) else: is_frame_done = True if is_frame_done: self.result.append(data) self.input_data.pop(0) io.progress_bar_inc(1) self.redraw_needed = True self.rect_locked = False elif go_to_prev_frame: self.input_data.insert(0, self.result.pop()) io.progress_bar_inc(-1) allow_remark_faces = True self.redraw_needed = True self.rect_locked = False elif skip_remaining: if self.rect_locked: faces.append([(self.rect), self.landmarks]) while len(self.input_data) > 0: self.result.append(self.input_data.pop(0)) io.progress_bar_inc(1) return None
def onHostGetData(self, host_dict): if not self.manual: if len(self.input_data) > 0: return self.input_data.pop(0) else: skip_remaining = False allow_remark_faces = False while len(self.input_data) > 0: data = self.input_data[0] filename, faces = data is_frame_done = False go_to_prev_frame = False # Can we mark an image that already has a marked face? if allow_remark_faces: allow_remark_faces = False # If there was already a face then lock the rectangle to it until the mouse is clicked if len(faces) > 0: prev_rect = faces.pop()[0] self.param['rect_locked'] = True faces.clear() self.param['rect_size'] = (prev_rect[2] - prev_rect[0]) / 2 self.param['x'] = ((prev_rect[0] + prev_rect[2]) / 2) * self.view_scale self.param['y'] = ((prev_rect[1] + prev_rect[3]) / 2) * self.view_scale if len(faces) == 0: self.original_image = cv2_imread(filename) (h, w, c) = self.original_image.shape self.view_scale = 1.0 if self.manual_window_size == 0 else self.manual_window_size / ( w if w > h else h) self.original_image = cv2.resize( self.original_image, (int(w * self.view_scale), int(h * self.view_scale)), interpolation=cv2.INTER_LINEAR) (h, w, c) = self.original_image.shape self.text_lines_img = (image_utils.get_draw_text_lines( self.original_image, (0, 0, self.original_image.shape[1], min(100, self.original_image.shape[0])), [ 'Match landmarks with face exactly. Click to confirm/unconfirm selection', '[Enter] - confirm face landmarks and continue', '[Space] - confirm as unmarked frame and continue', '[Mouse wheel] - change rect', '[,] [.]- prev frame, next frame', '[Q] - skip remaining frames' ], (1, 1, 1)) * 255).astype(np.uint8) while True: key = cv2.waitKey(1) & 0xFF if key == ord('\r') or key == ord('\n'): faces.append([(self.rect), self.landmarks]) is_frame_done = True break elif key == ord(' '): is_frame_done = True break elif key == ord('.'): allow_remark_faces = True # Only save the face if the rect is still locked if self.param['rect_locked']: faces.append([(self.rect), self.landmarks]) is_frame_done = True break elif key == ord(',') and len(self.result) > 0: # Only save the face if the rect is still locked if self.param['rect_locked']: faces.append([(self.rect), self.landmarks]) go_to_prev_frame = True break elif key == ord('q'): skip_remaining = True break new_param_x = np.clip(self.param['x'], 0, w - 1) / self.view_scale new_param_y = np.clip(self.param['y'], 0, h - 1) / self.view_scale new_param_rect_size = self.param['rect_size'] if self.param_x != new_param_x or \ self.param_y != new_param_y or \ self.param_rect_size != new_param_rect_size or \ self.param['redraw_needed']: self.param_x = new_param_x self.param_y = new_param_y self.param_rect_size = new_param_rect_size self.rect = (int(self.param_x - self.param_rect_size), int(self.param_y - self.param_rect_size), int(self.param_x + self.param_rect_size), int(self.param_y + self.param_rect_size)) return [filename, [self.rect]] else: is_frame_done = True if is_frame_done: self.result.append(data) self.input_data.pop(0) self.inc_progress_bar(1) self.param['redraw_needed'] = True self.param['rect_locked'] = False elif go_to_prev_frame: self.input_data.insert(0, self.result.pop()) self.inc_progress_bar(-1) allow_remark_faces = True self.param['redraw_needed'] = True self.param['rect_locked'] = False elif skip_remaining: while len(self.input_data) > 0: self.result.append(self.input_data.pop(0)) self.inc_progress_bar(1) return None
def get_data(self, host_dict): if not self.manual: if len(self.input_data) > 0: return self.input_data.pop(0) else: need_remark_face = False redraw_needed = False while len(self.input_data) > 0: data = self.input_data[0] filename, data_rects, data_landmarks = data.filename, data.rects, data.landmarks is_frame_done = False if need_remark_face: # need remark image from input data that already has a marked face? need_remark_face = False if len( data_rects ) != 0: # If there was already a face then lock the rectangle to it until the mouse is clicked self.rect = data_rects.pop() self.landmarks = data_landmarks.pop() data_rects.clear() data_landmarks.clear() redraw_needed = True self.rect_locked = True self.rect_size = (self.rect[2] - self.rect[0]) / 2 self.x = (self.rect[0] + self.rect[2]) / 2 self.y = (self.rect[1] + self.rect[3]) / 2 if len(data_rects) == 0: if self.cache_original_image[0] == filename: self.original_image = self.cache_original_image[1] else: self.original_image = cv2_imread(filename) self.cache_original_image = (filename, self.original_image) (h, w, c) = self.original_image.shape self.view_scale = 1.0 if self.manual_window_size == 0 else self.manual_window_size / ( h * (16.0 / 9.0)) if self.cache_image[0] == (h, w, c) + (self.view_scale, filename): self.image = self.cache_image[1] else: self.image = cv2.resize(self.original_image, (int( w * self.view_scale), int(h * self.view_scale)), interpolation=cv2.INTER_LINEAR) self.cache_image = ((h, w, c) + (self.view_scale, filename), self.image) (h, w, c) = self.image.shape sh = (0, 0, w, min(100, h)) if self.cache_text_lines_img[0] == sh: self.text_lines_img = self.cache_text_lines_img[1] else: self.text_lines_img = (image_utils.get_draw_text_lines( self.image, sh, [ '[Mouse click] - lock/unlock selection', '[Mouse wheel] - change rect', '[Enter] / [Space] - confirm / skip frame', '[,] [.]- prev frame, next frame. [Q] - skip remaining frames', '[a] - accuracy on/off (more fps)', '[h] - hide this help' ], (1, 1, 1)) * 255).astype(np.uint8) self.cache_text_lines_img = (sh, self.text_lines_img) while True: io.process_messages(0.0001) new_x = self.x new_y = self.y new_rect_size = self.rect_size mouse_events = io.get_mouse_events(self.wnd_name) for ev in mouse_events: (x, y, ev, flags) = ev if ev == io.EVENT_MOUSEWHEEL and not self.rect_locked: mod = 1 if flags > 0 else -1 diff = 1 if new_rect_size <= 40 else np.clip( new_rect_size / 10, 1, 10) new_rect_size = max(5, new_rect_size + diff * mod) elif ev == io.EVENT_LBUTTONDOWN: self.rect_locked = not self.rect_locked self.extract_needed = True elif not self.rect_locked: new_x = np.clip(x, 0, w - 1) / self.view_scale new_y = np.clip(y, 0, h - 1) / self.view_scale key_events = io.get_key_events(self.wnd_name) key, = key_events[-1] if len(key_events) > 0 else (0, ) if key == ord('\r') or key == ord('\n'): #confirm frame is_frame_done = True data_rects.append(self.rect) data_landmarks.append(self.landmarks) break elif key == ord(' '): #confirm skip frame is_frame_done = True break elif key == ord(',') and len(self.result) > 0: #go prev frame if self.rect_locked: self.rect_locked = False # Only save the face if the rect is still locked data_rects.append(self.rect) data_landmarks.append(self.landmarks) self.input_data.insert(0, self.result.pop()) io.progress_bar_inc(-1) need_remark_face = True break elif key == ord('.'): #go next frame if self.rect_locked: self.rect_locked = False # Only save the face if the rect is still locked data_rects.append(self.rect) data_landmarks.append(self.landmarks) need_remark_face = True is_frame_done = True break elif key == ord('q'): #skip remaining if self.rect_locked: self.rect_locked = False data_rects.append(self.rect) data_landmarks.append(self.landmarks) while len(self.input_data) > 0: self.result.append(self.input_data.pop(0)) io.progress_bar_inc(1) break elif key == ord('h'): self.hide_help = not self.hide_help break elif key == ord('a'): self.landmarks_accurate = not self.landmarks_accurate break if self.x != new_x or \ self.y != new_y or \ self.rect_size != new_rect_size or \ self.extract_needed or \ redraw_needed: self.x = new_x self.y = new_y self.rect_size = new_rect_size self.rect = (int(self.x - self.rect_size), int(self.y - self.rect_size), int(self.x + self.rect_size), int(self.y + self.rect_size)) if redraw_needed: redraw_needed = False return ExtractSubprocessor.Data( filename, landmarks_accurate=self.landmarks_accurate) else: return ExtractSubprocessor.Data( filename, rects=[self.rect], landmarks_accurate=self.landmarks_accurate) else: is_frame_done = True if is_frame_done: self.result.append(data) self.input_data.pop(0) io.progress_bar_inc(1) self.extract_needed = True self.rect_locked = False return None
def manual_pass(extracted_faces, image_size, face_type): extract_processes = start_processes( get_devices_for_type('landmarks', False), 'landmarks', image_size, face_type, False, None, None) if len(extract_processes) == 0: if (type == 'rects' or type == 'landmarks'): print( 'You have no capable GPUs. Try to close programs which can consume VRAM, and run again.' ) return [] p = extract_processes[0] wnd_name = 'Manual pass' cv2.namedWindow(wnd_name) view_scale_to = 0 #1368 param = {'x': 0, 'y': 0, 'rect_size': 5} def onMouse(event, x, y, flags, param): if event == cv2.EVENT_MOUSEWHEEL: mod = 1 if flags > 0 else -1 param['rect_size'] = max(5, param['rect_size'] + 10 * mod) else: param['x'] = x param['y'] = y cv2.setMouseCallback(wnd_name, onMouse, param) for data in extracted_faces: filename = data[0] faces = data[1] if len(faces) == 0: original_image = cv2.imread(filename) (h, w, c) = original_image.shape view_scale = 1.0 if view_scale_to == 0 else view_scale_to / ( w if w > h else h) original_image = cv2.resize( original_image, (int(w * view_scale), int(h * view_scale)), interpolation=cv2.INTER_LINEAR) text_lines_img = (image_utils.get_draw_text_lines( original_image, (0, 0, original_image.shape[1], min(100, original_image.shape[0])), [ 'Match landmarks with face exactly.', '[Enter] - confirm frame', '[Space] - skip frame', '[Mouse wheel] - change rect' ], (1, 1, 1)) * 255).astype(np.uint8) landmarks = None param_x = -1 param_y = -1 param_rect_size = -1 while True: key = cv2.waitKey(1) & 0xFF if key == ord('\r') or key == ord('\n'): faces.append([(rect), landmarks]) break elif key == ord(' '): break if param_x != param['x'] / view_scale or \ param_y != param['y'] / view_scale or \ param_rect_size != param['rect_size']: param_x = param['x'] / view_scale param_y = param['y'] / view_scale param_rect_size = param['rect_size'] rect = (param_x - param_rect_size, param_y - param_rect_size, param_x + param_rect_size, param_y + param_rect_size) p['sq'].put({'op': 'extract', 'data': [filename, [rect]]}) while True: if not p['cq'].empty(): obj = p['cq'].get() obj_op = obj['op'] if obj_op == 'extract_success': result = obj['result'] landmarks = result[1][0][1] image = cv2.addWeighted( original_image, 1.0, text_lines_img, 1.0, 0) view_rect = (np.array(rect) * view_scale).astype( np.int).tolist() view_landmarks = (np.array(landmarks) * view_scale).astype( np.int).tolist() facelib.LandmarksProcessor.draw_rect_landmarks( image, view_rect, view_landmarks, image_size, face_type) cv2.imshow(wnd_name, image) elif obj_op == 'error': print(obj['message']) break cv2.destroyAllWindows() for p in extract_processes[:]: p['process'].terminate() p['process'].join() return extracted_faces
def onHostGetData(self): if not self.manual: if len(self.input_data) > 0: return self.input_data.pop(0) else: while len(self.input_data) > 0: data = self.input_data[0] filename, faces = data is_frame_done = False if len(faces) == 0: self.original_image = cv2.imread(filename) (h, w, c) = self.original_image.shape self.view_scale = 1.0 if self.manual_window_size == 0 else self.manual_window_size / ( w if w > h else h) self.original_image = cv2.resize( self.original_image, (int(w * self.view_scale), int(h * self.view_scale)), interpolation=cv2.INTER_LINEAR) (h, w, c) = self.original_image.shape self.text_lines_img = (image_utils.get_draw_text_lines( self.original_image, (0, 0, self.original_image.shape[1], min(100, self.original_image.shape[0])), [ 'Match landmarks with face exactly.', '[Enter] - confirm frame', '[Space] - skip frame', '[Mouse wheel] - change rect' ], (1, 1, 1)) * 255).astype(np.uint8) while True: key = cv2.waitKey(1) & 0xFF if key == ord('\r') or key == ord('\n'): faces.append([(self.rect), self.landmarks]) is_frame_done = True break elif key == ord(' '): is_frame_done = True break new_param_x = self.param['x'] / self.view_scale new_param_y = self.param['y'] / self.view_scale new_param_rect_size = self.param['rect_size'] new_param_x = np.clip(new_param_x, 0, w - 1) new_param_y = np.clip(new_param_y, 0, h - 1) if self.param_x != new_param_x or \ self.param_y != new_param_y or \ self.param_rect_size != new_param_rect_size: self.param_x = new_param_x self.param_y = new_param_y self.param_rect_size = new_param_rect_size self.rect = (self.param_x - self.param_rect_size, self.param_y - self.param_rect_size, self.param_x + self.param_rect_size, self.param_y + self.param_rect_size) return [filename, [self.rect]] else: is_frame_done = True if is_frame_done: self.result.append(data) self.input_data.pop(0) self.inc_progress_bar(1) return None