class GenderKernel(scannerpy.Kernel): def __init__(self, config, protobufs): self.protobufs = protobufs self.rc = RudeCarnie(model_dir=model_dir) def close(self): pass def execute(self, columns): global i [img, bboxes] = columns img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) [h, w] = img.shape[:2] bboxes = readers.bboxes(bboxes, self.protobufs) imgs = [ img[int(h * bbox.y1):int(h * bbox.y2), int(w * bbox.x1):int(w * bbox.x2)] for bbox in bboxes ] for img in imgs: cv2.imwrite('/app/tmp/{:05d}.jpg'.format(i), img) i += 1 genders = self.rc.get_gender_batch(imgs) outputs = [ struct.pack('=cf', label, score) for [label, score] in genders ] assert (len(outputs) == len(imgs)) return [''.join(outputs)]
class DetectGender(scannerpy.Kernel): def __init__(self, config): from carnie_helper import RudeCarnie self.config = config self.rc = RudeCarnie(model_dir=config.args['model_dir']) def execute(self, frame: FrameType, bboxes: bytes) -> bytes: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) [h, w] = frame.shape[:2] bboxes = readers.bboxes(bboxes, self.config.protobufs) frames = [ frame[int(bbox.y1 * h):int(bbox.y2 * h), int(bbox.x1 * w):int(bbox.x2 * w)] for bbox in bboxes ] genders = self.rc.get_gender_batch(frames) return pickle.dumps(genders)
class DetectGender(scannerpy.Kernel): def fetch_resources(self): download_temp_file(MODEL_FILE, untar=True) def setup_with_resources(self): from carnie_helper import RudeCarnie self.rc = RudeCarnie(model_dir=os.path.join(temp_directory(), '21936')) def execute(self, frame: FrameType, bboxes: BboxList) -> Any: frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) [h, w] = frame.shape[:2] frames = [ frame[int(bbox.y1 * h):int(bbox.y2 * h), int(bbox.x1 * w):int(bbox.x2 * w)] for bbox in bboxes ] genders = self.rc.get_gender_batch(frames) return genders