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)]
def handle(self, *args, **options): with open(options['path']) as f: paths = [s.strip() for s in f.readlines()] model_dir = '/app/deps/rude-carnie/inception_gender_checkpoint' rc = RudeCarnie(model_dir=model_dir) for path in paths: self.handle_video(path, rc)
def handle(self, *args, **options): with open(options['path']) as f: paths = [s.strip() for s in f.readlines()] model_dir = '/app/deps/rude-carnie/inception_gender_checkpoint' rc = RudeCarnie(model_dir=model_dir) for path in paths: confident = 0 if path == '': return video = Video.objects.filter(path=path).get() labelset = video.detected_labelset() tracks = Track.objects.filter(first_frame__labelset=labelset).all() for track in tracks: if track.gender != '0': print 'skipping_track', track.id continue faces = Face.objects.filter(track=track) print track.id print("len of faces for path {}, is {}".format( path, len(faces))) imgs = [ './assets/thumbnails/{}_{}.jpg'.format(labelset.id, f.id) for f in faces ] best = rc.get_gender(imgs) # Update each of the faces. male_sum = 0.0 female_sum = 0.0 for i, face in enumerate(faces): if best[i] is None: # couldn't get gender output for some reason continue if best[i][0] == 'M': male_sum += best[i][1] elif best[i][0] == 'F': female_sum += best[i][1] track.gender = 'M' if male_sum > female_sum else 'F' track.save()
def handle(self, *args, **options): with open(options['path']) as f: paths = [s.strip() for s in f.readlines()] model_dir = '/app/deps/rude-carnie/inception_gender_checkpoint' for i, path in enumerate(paths): confident = 0 if path == '': return rc = RudeCarnie(model_dir=model_dir) video = Video.objects.filter(path=path).get() labelset = video.detected_labelset() faces = Face.objects.filter(frame__labelset=labelset).all() print(("len of faces for path {}, is {}".format(path, len(faces)))) faces = [f for f in faces if f.bbox.x2 - f.bbox.x1 >= .05] imgs = [ './assets/thumbnails/{}_{}.jpg'.format(video.id, f.id) for f in faces ] best = rc.get_gender(imgs, single_look=True) for comb in zip(imgs, best): print(('{} : {}'.format(comb[0], comb[1]))) # Update each of the faces. for i, face in enumerate(faces): if best[i] is None: # couldn't get gender output for some reason continue if best[i][1] < 0.90: # gender detector not confident. face.gender = 'U' else: face.gender = best[i][0] confident += 1 face.save() print(('confident = {}, not confident = {}'.format( confident, len(faces) - confident)))
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
def __init__(self, config, protobufs): self.protobufs = protobufs self.rc = RudeCarnie(model_dir=model_dir)
def __init__(self, config): from carnie_helper import RudeCarnie self.config = config self.rc = RudeCarnie(model_dir=config.args['model_dir'])
def setup_with_resources(self): from carnie_helper import RudeCarnie self.rc = RudeCarnie(model_dir=os.path.join(temp_directory(), '21936'))
from carnie_helper import RudeCarnie import os import random def load_imgs(img_directory): imgs = [] for root, subdirs, files in os.walk(img_directory): for file in files: if os.path.splitext(file)[1].lower() in ('.jpg', '.jpeg'): imgs.append(os.path.join(root, file)) return imgs rc = RudeCarnie() data_dir = '/usr/src/app/assets/thumbnails' files = load_imgs(data_dir) files = random.sample(files, 30) assert len(files) != 0, 'test' best = rc.get_gender(files) for comb in zip(files, best): print('{} : {}'.format(comb[0], comb[1]))